Skip to content

Commit

Permalink
fix: define nested and non-nested directives
Browse files Browse the repository at this point in the history
  • Loading branch information
ggicci committed Nov 13, 2023
1 parent a1fee3a commit 21dcd8f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ func shouldResolveNestedDirectives(r *Resolver, ctx context.Context) bool {
if r.IsLeaf() {
return false // leaves have no children
}
if len(r.Directives) == 0 {
return true // go deeper if no directives on current field
}
if ctx != nil && ctx.Value(ckResolveNestedDirectives) != nil {
return ctx.Value(ckResolveNestedDirectives).(bool)
}
Expand Down
24 changes: 24 additions & 0 deletions resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,30 @@ func TestIterate_CallbackFail(t *testing.T) {
assert.Equal(t, nil, resolver.Lookup("CSRFToken").Context.Value(ckHello))
}

func TestWithNestedDirectivesEnabled_definitionOfNestedDirectives(t *testing.T) {
type CreateUserRequest struct {
ApiVersion string `owl:"form=api_version"`
User
}

ns, tracker := createNsForTracking()
resolver, err := owl.New(CreateUserRequest{}, owl.WithNamespace(ns))
assert.NoError(t, err)
resolver.Resolve(owl.WithNestedDirectivesEnabled(false))

assert.Equal(t, []*owl.Directive{
owl.NewDirective("form", "api_version"),

// Below are NOT nested directives. Because the field User has no directives.
// If we added any directive to User, then the directives below will be nested directives.
// Ex: User `owl:"form=user"`
owl.NewDirective("form", "name"),
owl.NewDirective("form", "gender"),
owl.NewDirective("default", "unknown"),
owl.NewDirective("form", "birthday"),
}, tracker.Executed.ExecutedDirectives(), "tell the difference between nested and non-nested directives")
}

func TestTreeDebugLayout(t *testing.T) {
var (
tree *owl.Resolver
Expand Down

0 comments on commit 21dcd8f

Please sign in to comment.