mirror of
https://lab.forgefriends.org/friendlyforgeformat/gof3.git
synced 2025-10-06 02:32:41 +02:00
refactor GetChildFromExample into GetIDFromName
the practical use case is to convert a name into an ID rather than a more general conversion from an arbitrary example
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,5 +1,6 @@
|
||||
*~
|
||||
f3-cli
|
||||
coverage.out
|
||||
coverage.html
|
||||
tests/*.out
|
||||
format/schemas/.gitignore
|
||||
|
1
Makefile
1
Makefile
@@ -25,6 +25,7 @@ $(EXECUTABLE): $(GO_SOURCES)
|
||||
coverage:
|
||||
serials=1 tests/setup-forgejo.sh
|
||||
GOF3_FORGEJO_PORT=8781 go test -cover -coverprofile coverage.out -coverpkg lab.forgefriends.org/friendlyforgeformat/gof3/... lab.forgefriends.org/friendlyforgeformat/gof3/...
|
||||
go tool cover -html=coverage.out -o coverage.html
|
||||
uncover coverage.out
|
||||
|
||||
.PHONY: deps-backend
|
||||
|
@@ -16,6 +16,7 @@ import (
|
||||
|
||||
type Interface interface {
|
||||
GetID() string
|
||||
GetName() string
|
||||
SetID(id string)
|
||||
IsNil() bool
|
||||
GetReferences() References
|
||||
@@ -81,6 +82,7 @@ type Common struct {
|
||||
}
|
||||
|
||||
func (c *Common) GetID() string { return c.Index.GetID() }
|
||||
func (c *Common) GetName() string { return c.GetID() }
|
||||
func (c *Common) SetID(id string) { c.Index.SetID(id) }
|
||||
func (c *Common) IsNil() bool { return c.Index.IsNil() }
|
||||
func (c *Common) GetReferences() References { return NewReferences() }
|
||||
|
@@ -15,6 +15,10 @@ type Project struct {
|
||||
HasWiki bool `json:"has_wiki"`
|
||||
}
|
||||
|
||||
func (o *Project) GetName() string {
|
||||
return o.Name
|
||||
}
|
||||
|
||||
func (o *Project) GetReferences() References {
|
||||
references := o.Common.GetReferences()
|
||||
if o.Forked != nil {
|
||||
|
@@ -13,6 +13,10 @@ type User struct {
|
||||
IsAdmin bool `json:"admin"`
|
||||
}
|
||||
|
||||
func (o *User) GetName() string {
|
||||
return o.Name
|
||||
}
|
||||
|
||||
func (o User) GetCmpIgnoreFields() []string {
|
||||
return append(o.Common.GetCmpIgnoreFields(), ".Password")
|
||||
}
|
||||
|
@@ -40,9 +40,8 @@ func (o *users) ListPage(ctx context.Context, page int) generic.ChildrenSlice {
|
||||
return f3_tree.ConvertListed(ctx, o.GetNode(), f3_tree.ConvertToAny(userFounds...)...)
|
||||
}
|
||||
|
||||
func (o *user) GetChildFromExample(ctx context.Context, example generic.NodeInterface) generic.NodeID {
|
||||
username := string(example.GetID())
|
||||
user, resp, err := o.getClient().GetUserInfo(username)
|
||||
func (o *users) GetIDFromName(ctx context.Context, name string) generic.NodeID {
|
||||
user, resp, err := o.getClient().GetUserInfo(name)
|
||||
if resp.StatusCode == 404 {
|
||||
return generic.NilID
|
||||
}
|
||||
|
@@ -27,6 +27,15 @@ func ConvertToAny[T any](s ...T) []any {
|
||||
return a
|
||||
}
|
||||
|
||||
func ConvertNativeChild(ctx context.Context, tree generic.TreeInterface, parent generic.NodeInterface, kind generic.Kind, nativeChild any) generic.NodeInterface {
|
||||
child := tree.Factory(ctx, kind)
|
||||
child.SetParent(parent)
|
||||
childDriver := child.GetDriver().(ForgeDriverInterface)
|
||||
childDriver.SetNative(nativeChild)
|
||||
child.SetID(generic.NodeID(childDriver.GetNativeID()))
|
||||
return child
|
||||
}
|
||||
|
||||
func ConvertListed(ctx context.Context, node generic.NodeInterface, nativeChildren ...any) generic.ChildrenSlice {
|
||||
children := generic.NewChildrenSlice(len(nativeChildren))
|
||||
|
||||
@@ -35,13 +44,7 @@ func ConvertListed(ctx context.Context, node generic.NodeInterface, nativeChildr
|
||||
kind := f3Tree.GetChildrenKind(node.GetKind())
|
||||
|
||||
for _, nativeChild := range nativeChildren {
|
||||
childNode := tree.Factory(ctx, kind)
|
||||
childNode.SetParent(node)
|
||||
childNodeDriver := childNode.GetDriver().(ForgeDriverInterface)
|
||||
childNodeDriver.SetNative(nativeChild)
|
||||
childNode.SetID(generic.NodeID(childNodeDriver.GetNativeID()))
|
||||
|
||||
children = append(children, childNode)
|
||||
children = append(children, ConvertNativeChild(ctx, tree, node, kind, nativeChild))
|
||||
}
|
||||
return children
|
||||
}
|
||||
|
@@ -6,6 +6,7 @@ package generic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"lab.forgefriends.org/friendlyforgeformat/gof3/f3"
|
||||
@@ -26,7 +27,7 @@ type NodeDriverInterface interface {
|
||||
GetTreeDriver() TreeDriverInterface
|
||||
|
||||
ListPage(context.Context, int) ChildrenSlice
|
||||
GetChildFromExample(context.Context, NodeInterface) NodeID
|
||||
GetIDFromName(context.Context, string) NodeID
|
||||
|
||||
Equals(context.Context, NodeInterface) bool
|
||||
|
||||
@@ -72,8 +73,8 @@ func (o *NullDriver) ListPage(context.Context, int) ChildrenSlice {
|
||||
panic(fmt.Errorf("ListPage %s", o.GetNode().GetKind()))
|
||||
}
|
||||
|
||||
func (o *NullDriver) GetChildFromExample(ctx context.Context, example NodeInterface) NodeID {
|
||||
return example.GetID()
|
||||
func (o *NullDriver) GetIDFromName(ctx context.Context, name string) NodeID {
|
||||
panic(errors.New(name))
|
||||
}
|
||||
|
||||
func (o *NullDriver) Equals(context.Context, NodeInterface) bool {
|
||||
|
@@ -44,6 +44,7 @@ type NodeAccessorsInterface interface {
|
||||
|
||||
type NodeTreeInterface interface {
|
||||
GetChild(NodeID) NodeInterface
|
||||
GetIDFromName(context.Context, string) NodeID
|
||||
SetChild(NodeInterface) NodeInterface
|
||||
DeleteChild(NodeID) NodeInterface
|
||||
|
||||
@@ -61,7 +62,7 @@ type NodeTreeInterface interface {
|
||||
type NodeDriverProxyInterface interface {
|
||||
MapIDInterface
|
||||
ListPage(context.Context, int) ChildrenSlice
|
||||
GetChildFromExample(context.Context, NodeInterface) NodeID
|
||||
GetIDFromName(context.Context, string) NodeID
|
||||
|
||||
Equals(context.Context, NodeInterface) bool
|
||||
|
||||
|
@@ -199,8 +199,8 @@ func (o *Node) DeleteChild(id NodeID) NodeInterface {
|
||||
return NilNode
|
||||
}
|
||||
|
||||
func (o *Node) GetChildFromExample(ctx context.Context, example NodeInterface) NodeID {
|
||||
return o.GetDriver().GetChildFromExample(ctx, example)
|
||||
func (o *Node) GetIDFromName(ctx context.Context, name string) NodeID {
|
||||
return o.GetDriver().GetIDFromName(ctx, name)
|
||||
}
|
||||
|
||||
func (o *Node) Get(ctx context.Context) NodeInterface {
|
||||
@@ -306,8 +306,8 @@ func (o *Node) Apply(ctx context.Context, parentPath, path Path, options *ApplyO
|
||||
}
|
||||
|
||||
child := o.GetChild(path[0].GetID())
|
||||
if child == NilNode && options.search == ApplySearchByExample {
|
||||
if childID := o.GetChildFromExample(ctx, path[0]); childID != NilID {
|
||||
if child == NilNode && options.search == ApplySearchByName {
|
||||
if childID := o.GetIDFromName(ctx, string(path[0].GetID())); childID != NilID {
|
||||
child = o.GetChild(childID)
|
||||
}
|
||||
}
|
||||
|
@@ -161,26 +161,22 @@ func TestNodeList(t *testing.T) {
|
||||
assert.True(t, childThree.GetIsNil())
|
||||
}
|
||||
|
||||
type nodeGetChildFromExample struct {
|
||||
type nodeGetIDFromName struct {
|
||||
Node
|
||||
name string
|
||||
}
|
||||
|
||||
func (o *nodeGetChildFromExample) GetChildFromExample(ctx context.Context, example NodeInterface) NodeID {
|
||||
other, ok := example.(*nodeGetChildFromExample)
|
||||
if !ok {
|
||||
return NilID
|
||||
}
|
||||
func (o *nodeGetIDFromName) GetIDFromName(ctx context.Context, name string) NodeID {
|
||||
for _, child := range o.GetChildren() {
|
||||
if child.(*nodeGetChildFromExample).name == other.name {
|
||||
if child.(*nodeGetIDFromName).name == name {
|
||||
return child.GetID()
|
||||
}
|
||||
}
|
||||
return NilID
|
||||
}
|
||||
|
||||
func newNodeGetChildFromExample(id, name string) NodeInterface {
|
||||
node := &nodeGetChildFromExample{}
|
||||
func newNodeGetIDFromName(id, name string) NodeInterface {
|
||||
node := &nodeGetIDFromName{}
|
||||
node.Init(node)
|
||||
node.SetIsNil(false)
|
||||
node.SetID(NodeID(id))
|
||||
@@ -188,25 +184,18 @@ func newNodeGetChildFromExample(id, name string) NodeInterface {
|
||||
return node
|
||||
}
|
||||
|
||||
func TestNodeGetChildFromExample(t *testing.T) {
|
||||
func TestNodeGetIDFromName(t *testing.T) {
|
||||
id := "1243"
|
||||
name := "NAME"
|
||||
node := newNodeGetChildFromExample(id, name)
|
||||
node := newNodeGetIDFromName(id, name)
|
||||
|
||||
ctx := context.Background()
|
||||
parent := newNodeGetChildFromExample("parent", "PARENT")
|
||||
r := parent.GetChildFromExample(ctx, NilNode)
|
||||
assert.EqualValues(t, NilID, r)
|
||||
|
||||
parent := newNodeGetIDFromName("parent", "PARENT")
|
||||
parent.SetChild(node)
|
||||
r = parent.GetChildFromExample(ctx, node)
|
||||
assert.True(t, r == NodeID(id))
|
||||
|
||||
badExample := newNodeGetChildFromExample("4567", "OTHERNAME")
|
||||
r = parent.GetChildFromExample(ctx, badExample)
|
||||
r := parent.GetIDFromName(ctx, "OTHERNAME")
|
||||
assert.EqualValues(t, NilID, r)
|
||||
|
||||
goodExample := newNodeGetChildFromExample("4567", name)
|
||||
r = parent.GetChildFromExample(ctx, goodExample)
|
||||
r = parent.GetIDFromName(ctx, name)
|
||||
assert.True(t, r == NodeID(id))
|
||||
}
|
||||
|
@@ -32,8 +32,8 @@ const (
|
||||
type ApplySearch bool
|
||||
|
||||
const (
|
||||
ApplySearchByExample ApplySearch = true
|
||||
ApplySearchByID ApplySearch = false
|
||||
ApplySearchByName ApplySearch = true
|
||||
ApplySearchByID ApplySearch = false
|
||||
)
|
||||
|
||||
type ApplyOptions struct {
|
||||
|
@@ -265,12 +265,11 @@ func (o *Driver) ListPage(ctx context.Context, page int) generic.ChildrenSlice {
|
||||
return children
|
||||
}
|
||||
|
||||
func (o *Driver) GetChildFromExample(ctx context.Context, example generic.NodeInterface) generic.NodeID {
|
||||
func (o *Driver) GetIDFromName(ctx context.Context, content string) generic.NodeID {
|
||||
node := o.GetNode()
|
||||
o.Trace("'%s'", node.GetCurrentPath())
|
||||
storage := o.getStorage(node, node.GetCurrentPath())
|
||||
|
||||
content := string(example.GetID())
|
||||
if storage != nil {
|
||||
for id, child := range storage.children {
|
||||
if child.f.Content == content {
|
||||
|
@@ -131,7 +131,7 @@ func TestApplyVisitID(t *testing.T) {
|
||||
assert.False(t, called)
|
||||
}
|
||||
|
||||
func TestApplyVisitFromExample(t *testing.T) {
|
||||
func TestApplyVisitByName(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
tree := NewMemoryTree(ctx, "T")
|
||||
@@ -165,7 +165,7 @@ func TestApplyVisitFromExample(t *testing.T) {
|
||||
assert.EqualValues(t, actual, expected)
|
||||
collected = append(collected, path.String())
|
||||
}
|
||||
assert.True(t, tree.Apply(ctx, path, generic.NewApplyOptions(collect, generic.ApplyLastNode, generic.ApplySearchByExample)))
|
||||
assert.True(t, tree.Apply(ctx, path, generic.NewApplyOptions(collect, generic.ApplyLastNode, generic.ApplySearchByName)))
|
||||
if assert.EqualValues(t, 1, len(collected)) {
|
||||
assert.EqualValues(t, testCase.expected, collected[0])
|
||||
}
|
||||
|
Reference in New Issue
Block a user