gof3/tree/generic/driver_node.go
Twenty Panda 05f8b56e88
All checks were successful
/ backend-checks (pull_request) Successful in 6m49s
generic: refactor NodeID to encapsulate string/int conversions
2024-06-27 11:00:20 +02:00

107 lines
2.8 KiB
Go

// Copyright Earl Warren <contact@earl-warren.org>
// Copyright Loïc Dachary <loic@dachary.org>
// SPDX-License-Identifier: MIT
package generic
import (
"context"
"errors"
"fmt"
"code.forgejo.org/f3/gof3/v3/f3"
"code.forgejo.org/f3/gof3/v3/logger"
)
type NodeDriverInterface interface {
logger.MessageInterface
MapIDInterface
IsNull() bool
GetNode() NodeInterface
SetNode(NodeInterface)
SetTreeDriver(treeDriver TreeDriverInterface)
GetTreeDriver() TreeDriverInterface
ListPage(context.Context, int) ChildrenSlice
GetIDFromName(context.Context, string) NodeID
Equals(context.Context, NodeInterface) bool
Get(context.Context) bool
Put(context.Context) NodeID
Patch(context.Context)
Delete(context.Context)
NewFormat() f3.Interface
FromFormat(f3.Interface)
ToFormat() f3.Interface
LookupMappedID(NodeID) NodeID
String() string
}
type NullDriver struct {
logger.Logger
node NodeInterface
mapped NodeID
treeDriver TreeDriverInterface
}
func NewNullDriver() NodeDriverInterface {
return &NullDriver{}
}
func (o *NullDriver) IsNull() bool { return true }
func (o *NullDriver) SetNode(node NodeInterface) { o.node = node }
func (o *NullDriver) GetNode() NodeInterface { return o.node }
func (o *NullDriver) GetMappedID() NodeID {
if o.mapped == nil {
return NilID
}
return o.mapped
}
func (o *NullDriver) SetMappedID(mapped NodeID) { o.mapped = mapped }
func (o *NullDriver) SetTreeDriver(treeDriver TreeDriverInterface) {
o.treeDriver = treeDriver
if treeDriver != nil {
o.SetLogger(treeDriver)
}
}
func (o *NullDriver) GetTreeDriver() TreeDriverInterface { return o.treeDriver }
func (o *NullDriver) ListPage(context.Context, int) ChildrenSlice {
panic(fmt.Errorf("ListPage %s", o.GetNode().GetKind()))
}
func (o *NullDriver) GetIDFromName(ctx context.Context, name string) NodeID {
panic(errors.New(name))
}
func (o *NullDriver) Equals(context.Context, NodeInterface) bool {
panic(fmt.Errorf("Equals %s", o.GetNode().GetKind()))
}
func (o *NullDriver) Get(context.Context) bool { panic(fmt.Errorf("Get %s", o.GetNode().GetKind())) }
func (o *NullDriver) Put(context.Context) NodeID {
panic(fmt.Errorf("Put %s", o.GetNode().GetKind()))
}
func (o *NullDriver) Patch(context.Context) {
panic(fmt.Errorf("Patch %s", o.GetNode().GetKind()))
}
func (o *NullDriver) Delete(context.Context) { panic(fmt.Errorf("Delete %s", o.GetNode().GetKind())) }
func (o *NullDriver) NewFormat() f3.Interface {
panic(fmt.Errorf("NewFormat %s", o.GetNode().GetKind()))
}
func (o *NullDriver) FromFormat(f3.Interface) {
panic(fmt.Errorf("FromFormat %s", o.GetNode().GetKind()))
}
func (o *NullDriver) ToFormat() f3.Interface { panic(fmt.Errorf("ToFormat %s", o.GetNode().GetKind())) }
func (o *NullDriver) LookupMappedID(NodeID) NodeID { return NilID }
func (o *NullDriver) String() string { return "" }