Skip to content

Commit

Permalink
feat: expose Resolver.Copy
Browse files Browse the repository at this point in the history
  • Loading branch information
ggicci committed Dec 30, 2023
1 parent e3644c4 commit f3c29a4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
17 changes: 15 additions & 2 deletions directive.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,17 @@ func ParseDirective(directive string) (*Directive, error) {
return NewDirective(name, argv...), nil
}

func isValidDirectiveName(name string) bool {
return reDirectiveName.MatchString(name)
// Copy creates a copy of the directive. The copy is a deep copy.
func (d *Directive) Copy() *Directive {
return NewDirective(d.Name, d.Argv...)
}

// String returns the string representation of the directive.
func (d *Directive) String() string {
if len(d.Argv) == 0 {
return d.Name
}
return d.Name + "=" + strings.Join(d.Argv, ",")
}

// DirectiveExecutor is the interface that wraps the Execute method.
Expand Down Expand Up @@ -108,3 +117,7 @@ type DirectiveRuntime struct {
// and if in dirA we set a value of "foo" to "bar", then in dirB we can get the value of "foo" as "bar".
Context context.Context
}

func isValidDirectiveName(name string) bool {
return reDirectiveName.MatchString(name)
}
8 changes: 8 additions & 0 deletions directive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,11 @@ func TestParseDirective(t *testing.T) {
assert.ErrorIs(t, err, testcase.err)
}
}

func TestDirective_String(t *testing.T) {
d := owl.NewDirective("form", "page", "page_index")
assert.Equal(t, "form=page,page_index", d.String())

d = owl.NewDirective("required")
assert.Equal(t, "required", d.String())
}
23 changes: 18 additions & 5 deletions resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func New(structValue interface{}, opts ...Option) (*Resolver, error) {
if err != nil {
return nil, err
}
tree = tree.copy()
tree = tree.Copy()

// Apply options, build the context for each resolver.
defaultOpts := []Option{WithNamespace(defaultNS)}
Expand All @@ -67,15 +67,28 @@ func New(structValue interface{}, opts ...Option) (*Resolver, error) {
return tree, nil
}

func (r *Resolver) copy() *Resolver {
// Copy returns a copy of the resolver tree. The copy is a deep copy, which
// means the children are also copied.
func (r *Resolver) Copy() *Resolver {
resolverCopy := new(Resolver)
*resolverCopy = *r
resolverCopy.Context = context.Background()

// Copy the children.
// Copy index and path.
resolverCopy.Index = make([]int, len(r.Index))
copy(resolverCopy.Index, r.Index)
resolverCopy.Path = make([]string, len(r.Path))
copy(resolverCopy.Path, r.Path)

// Copy the directives.
resolverCopy.Directives = make([]*Directive, len(r.Directives))
for i, d := range r.Directives {
resolverCopy.Directives[i] = d.Copy()
}

// Copy the children and set the parent.
resolverCopy.Children = make([]*Resolver, len(r.Children))
for i, child := range r.Children {
resolverCopy.Children[i] = child.copy()
resolverCopy.Children[i] = child.Copy()
resolverCopy.Children[i].Parent = resolverCopy
}
return resolverCopy
Expand Down
4 changes: 2 additions & 2 deletions resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func TestNew_SkipFieldsHavingNoDirectives(t *testing.T) {
Index: []int{},
LookupPath: "",
NumFields: 3,
Directives: nil,
Directives: []*owl.Directive{},
Leaf: false,
},
{
Expand All @@ -206,7 +206,7 @@ func TestNew_SkipFieldsHavingNoDirectives(t *testing.T) {
Index: []int{3}, // Pagination is the 4th field, Hidden is the 3rd field.
LookupPath: "Pagination",
NumFields: 2,
Directives: nil,
Directives: []*owl.Directive{},
Leaf: false,
},
{
Expand Down

0 comments on commit f3c29a4

Please sign in to comment.