Skip to content

Commit

Permalink
Merge pull request #1870 from josephschorr/resolver-comments
Browse files Browse the repository at this point in the history
Include doc comments in resolver generated source
  • Loading branch information
josephschorr authored Apr 17, 2024
2 parents dd40abe + 6039368 commit ae33744
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
11 changes: 9 additions & 2 deletions pkg/development/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"github.com/authzed/spicedb/pkg/caveats"
"github.com/authzed/spicedb/pkg/namespace"
core "github.com/authzed/spicedb/pkg/proto/core/v1"
"github.com/authzed/spicedb/pkg/schemadsl/compiler"
"github.com/authzed/spicedb/pkg/schemadsl/dslshape"
Expand Down Expand Up @@ -143,9 +144,15 @@ func (r *Resolver) ReferenceAtPosition(source input.Source, position input.Posit
ColumnPosition: int(def.SourcePosition.ZeroIndexedColumnPosition),
}

targetSourceCode := fmt.Sprintf("definition %s {\n\t// ...\n}", def.Name)
docComment := ""
comments := namespace.GetComments(def.Metadata)
if len(comments) > 0 {
docComment = strings.Join(comments, "\n") + "\n"
}

targetSourceCode := fmt.Sprintf("%sdefinition %s {\n\t// ...\n}", docComment, def.Name)
if len(def.Relation) == 0 {
targetSourceCode = fmt.Sprintf("definition %s {}", def.Name)
targetSourceCode = fmt.Sprintf("%sdefinition %s {}", docComment, def.Name)
}

return &SchemaReference{
Expand Down
50 changes: 50 additions & 0 deletions pkg/development/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,56 @@ definition document {
column: 5,
expectedReference: nil,
},
{
name: "reference to commented type",
schema: `
/** user is a user */
definition user {}
definition resource {
relation viewer: user
}
`,
line: 5,
column: 22,
expectedReference: &SchemaReference{
Source: "test",
Position: input.Position{LineNumber: 5, ColumnPosition: 22},
Text: "user",
ReferenceType: 1,
ReferenceMarkdown: "definition user",
TargetSource: &testSource,
TargetPosition: &input.Position{LineNumber: 2, ColumnPosition: 4},
TargetSourceCode: "/** user is a user */\ndefinition user {}",
TargetNamePositionOffset: 11,
},
},
{
name: "reference to commented relation",
schema: `
definition user {}
definition resource {
// viewer is a relation for viewing
relation viewer: user
permission view = viewer
}
`,
line: 7,
column: 25,
expectedReference: &SchemaReference{
Source: "test",
Position: input.Position{LineNumber: 7, ColumnPosition: 25},
Text: "viewer",
ReferenceType: ReferenceTypeRelation,
ReferenceMarkdown: "relation viewer",
TargetSource: &testSource,
TargetPosition: &input.Position{LineNumber: 5, ColumnPosition: 5},
TargetSourceCode: "// viewer is a relation for viewing\nrelation viewer: user\n",
TargetNamePositionOffset: 9,
},
},
}

for _, tc := range tcs {
Expand Down

0 comments on commit ae33744

Please sign in to comment.