Skip to content

Commit

Permalink
Merge pull request #2 from prgres/entoas/feat-extentions
Browse files Browse the repository at this point in the history
feat: add Extention annotation and add support for x-gen-operation-group ant ent#590
  • Loading branch information
prgres authored Jun 30, 2024
2 parents cd4926c + e8c39e0 commit 5ea4f92
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 67 deletions.
57 changes: 55 additions & 2 deletions entoas/annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ import (
"entgo.io/contrib/entoas/serialization"
"entgo.io/ent/entc/gen"
"entgo.io/ent/schema"
yaml "github.com/go-faster/yaml"
"github.com/ogen-go/ogen"
)

const XOgenOperationGroup = "x-ogen-operation-group"

type (
// Annotation annotates fields and edges with metadata for spec generation.
Annotation struct {
Expand All @@ -46,16 +49,29 @@ type (
ReadOnly bool
// Skip specifies that the field will be ignored in spec.
Skip bool
// Extensions has map of OpenApi extenions
Extensions OgenExtensions
}
OgenExtensions ogen.Extensions
// OperationConfig holds meta information about a REST operation.
OperationConfig struct {
Policy Policy
Groups serialization.Groups
Policy Policy
Groups serialization.Groups
Extensions OgenExtensions
}
// OpenApiExtension holds meta information about OpenApi extension
OpenApiExtension struct {
Name string
Value string
}
// OperationConfigOption allows managing OperationConfig using functional arguments.
OperationConfigOption func(*OperationConfig)
)

func (o OgenExtensions) Schema() ogen.Extensions {
return ogen.Extensions(o)
}

// Groups returns a OperationConfigOption that adds the given serialization groups to a OperationConfig.
func Groups(gs ...string) Annotation {
return Annotation{Groups: gs}
Expand All @@ -66,6 +82,43 @@ func OperationGroups(gs ...string) OperationConfigOption {
return func(c *OperationConfig) { c.Groups = gs }
}

func OperationExtentions(ext ...OpenApiExtension) OperationConfigOption {
return func(c *OperationConfig) {
if c.Extensions == nil {
c.Extensions = OgenExtensions{}
}

for _, e := range ext {
c.Extensions[e.Name] = yaml.Node{Kind: yaml.ScalarNode, Value: e.Value}
}
}
}

func Extensions(ext ...OpenApiExtension) Annotation {
exts := OgenExtensions{}

for _, e := range ext {
exts[e.Name] = yaml.Node{Kind: yaml.ScalarNode, Value: e.Value}
}

return Annotation{Extensions: exts}
}

func OperationExtentionGroup(group string) OperationConfigOption {
return OperationExtentions(OpenApiExtensionOperationGroup(group))
}

func ExtentionOperationGroup(group string) Annotation {
return Extensions(OpenApiExtensionOperationGroup(group))
}

func OpenApiExtensionOperationGroup(group string) OpenApiExtension {
return OpenApiExtension{
Name: XOgenOperationGroup,
Value: group,
}
}

// OperationPolicy returns a OperationConfigOption that sets the Policy of a OperationConfig to the given one.
func OperationPolicy(p Policy) OperationConfigOption {
return func(c *OperationConfig) { c.Policy = p }
Expand Down
17 changes: 12 additions & 5 deletions entoas/annotation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ func TestAnnotation(t *testing.T) {
require.Equal(t, serialization.Groups{"create", "groups"}, a.Groups)

a = CreateOperation(OperationGroups("create", "groups"), OperationPolicy(PolicyExpose))
require.Equal(t, OperationConfig{PolicyExpose, serialization.Groups{"create", "groups"}}, a.Create)
require.Equal(t, OperationConfig{Policy: PolicyExpose, Groups: serialization.Groups{"create", "groups"}}, a.Create)

a = ReadOperation(OperationGroups("read", "groups"), OperationPolicy(PolicyExpose))
require.Equal(t, OperationConfig{PolicyExpose, serialization.Groups{"read", "groups"}}, a.Read)
require.Equal(t, OperationConfig{Policy: PolicyExpose, Groups: serialization.Groups{"read", "groups"}}, a.Read)

a = UpdateOperation(OperationGroups("update", "groups"), OperationPolicy(PolicyExpose))
require.Equal(t, OperationConfig{PolicyExpose, serialization.Groups{"update", "groups"}}, a.Update)
require.Equal(t, OperationConfig{Policy: PolicyExpose, Groups: serialization.Groups{"update", "groups"}}, a.Update)

a = DeleteOperation(OperationGroups("delete", "groups"), OperationPolicy(PolicyExpose))
require.Equal(t, OperationConfig{PolicyExpose, serialization.Groups{"delete", "groups"}}, a.Delete)
require.Equal(t, OperationConfig{Policy: PolicyExpose, Groups: serialization.Groups{"delete", "groups"}}, a.Delete)

a = ListOperation(OperationGroups("list", "groups"), OperationPolicy(PolicyExpose))
require.Equal(t, OperationConfig{PolicyExpose, serialization.Groups{"list", "groups"}}, a.List)
require.Equal(t, OperationConfig{Policy: PolicyExpose, Groups: serialization.Groups{"list", "groups"}}, a.List)

b := Example("example")
require.Equal(t, "example", b.Example)
Expand All @@ -57,12 +57,18 @@ func TestAnnotation(t *testing.T) {
require.Equal(t, ogen.Binary(), c.Schema)

a = a.Merge(b).(Annotation).Merge(c).(Annotation)
// a.List.Extensions = ogen.Extensions{
// "zxc": yaml.Node{Kind: yaml.ScalarNode, Value: "ccc"},
// }
ex := Annotation{
Example: "example",
Schema: ogen.Binary(),
List: OperationConfig{
Groups: serialization.Groups{"list", "groups"},
Policy: PolicyExpose,
// Extensions: ogen.Extensions{
// "zxc": yaml.Node{Kind: yaml.ScalarNode, Value: "ccc"},
// },
},
}
require.Equal(t, ex, a)
Expand All @@ -89,6 +95,7 @@ func TestAnnotation(t *testing.T) {
require.NotNil(t, ac)
ac, err = SchemaAnnotation(&gen.Type{Annotations: gen.Annotations{a.Name(): ex}})
require.NoError(t, err)

require.Equal(t, &ex, ac)

ac, err = FieldAnnotation(new(gen.Field))
Expand Down
43 changes: 43 additions & 0 deletions entoas/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,14 @@ func paths(g *gen.Graph, spec *ogen.Spec) error {
}
// root for all operations on this node.
root := "/" + rules.Pluralize(strcase.KebabCase(n.Name))

ant := &Annotation{}
if err := ant.Decode(n.Annotations[ant.Name()]); err != nil {
return err
}

path(spec, root).Common.Extensions = ant.Extensions.Schema()

// Create operation.
if contains(ops, OpCreate) {
path(spec, root).Post, err = createOp(spec, n, cfg.AllowClientUUIDs)
Expand Down Expand Up @@ -294,6 +302,13 @@ func createOp(spec *ogen.Spec, n *gen.Type, allowClientUUIDs bool) (*ogen.Operat
spec.RefResponse(strconv.Itoa(http.StatusConflict)),
spec.RefResponse(strconv.Itoa(http.StatusInternalServerError)),
)

ant := &Annotation{}
if err := ant.Decode(n.Annotations[ant.Name()]); err != nil {
return nil, err
}
op.Common.Extensions = ant.Create.Extensions.Schema()

return op, nil
}

Expand Down Expand Up @@ -325,6 +340,13 @@ func readOp(spec *ogen.Spec, n *gen.Type) (*ogen.Operation, error) {
spec.RefResponse(strconv.Itoa(http.StatusNotFound)),
spec.RefResponse(strconv.Itoa(http.StatusInternalServerError)),
)

ant := &Annotation{}
if err := ant.Decode(n.Annotations[ant.Name()]); err != nil {
return nil, err
}
op.Common.Extensions = ant.Read.Extensions.Schema()

return op, nil
}

Expand Down Expand Up @@ -395,6 +417,13 @@ func updateOp(spec *ogen.Spec, n *gen.Type) (*ogen.Operation, error) {
spec.RefResponse(strconv.Itoa(http.StatusNotFound)),
spec.RefResponse(strconv.Itoa(http.StatusInternalServerError)),
)

ant := &Annotation{}
if err := ant.Decode(n.Annotations[ant.Name()]); err != nil {
return nil, err
}
op.Common.Extensions = ant.Update.Extensions.Schema()

return op, nil
}

Expand All @@ -421,6 +450,13 @@ func deleteOp(spec *ogen.Spec, n *gen.Type) (*ogen.Operation, error) {
spec.RefResponse(strconv.Itoa(http.StatusNotFound)),
spec.RefResponse(strconv.Itoa(http.StatusInternalServerError)),
)

ant := &Annotation{}
if err := ant.Decode(n.Annotations[ant.Name()]); err != nil {
return nil, err
}
op.Common.Extensions = ant.Delete.Extensions.Schema()

return op, nil
}

Expand Down Expand Up @@ -466,6 +502,13 @@ func listOp(spec *ogen.Spec, n *gen.Type) (*ogen.Operation, error) {
spec.RefResponse(strconv.Itoa(http.StatusNotFound)),
spec.RefResponse(strconv.Itoa(http.StatusInternalServerError)),
)

ant := &Annotation{}
if err := ant.Decode(n.Annotations[ant.Name()]); err != nil {
return nil, err
}
op.Common.Extensions = ant.List.Extensions.Schema()

return op, nil
}

Expand Down
33 changes: 17 additions & 16 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
module entgo.io/contrib

go 1.21
go 1.21.0

toolchain go1.22.4

toolchain go1.21.6

require (
entgo.io/ent v0.13.0
github.com/99designs/gqlgen v0.17.48
github.com/AlekSi/pointer v1.1.0
github.com/alecthomas/kong v0.7.0
github.com/go-faster/yamlx v0.4.1
github.com/go-openapi/inflect v0.19.0
github.com/google/uuid v1.6.0
github.com/hashicorp/go-multierror v1.1.1
Expand All @@ -21,11 +23,11 @@ require (
github.com/stretchr/testify v1.9.0
github.com/vektah/gqlparser/v2 v2.5.12
github.com/vmihailenco/msgpack/v5 v5.0.0-beta.9
go.uber.org/multierr v1.9.0
go.uber.org/zap v1.24.0
golang.org/x/exp v0.0.0-20221230185412-738e83a70c30
go.uber.org/multierr v1.11.0
go.uber.org/zap v1.27.0
golang.org/x/exp v0.0.0-20230725093048-515e97ebf090
golang.org/x/sync v0.7.0
golang.org/x/tools v0.21.0
golang.org/x/tools v0.22.0
google.golang.org/grpc v1.52.3
google.golang.org/protobuf v1.34.2
)
Expand All @@ -36,12 +38,12 @@ require (
github.com/agnivade/levenshtein v1.1.1 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dlclark/regexp2 v1.7.0 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/dlclark/regexp2 v1.11.0 // indirect
github.com/fatih/color v1.17.0 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-faster/errors v0.6.1 // indirect
github.com/go-faster/jx v0.40.0 // indirect
github.com/go-faster/yamlx v0.4.1 // indirect
github.com/go-faster/errors v0.7.1 // indirect
github.com/go-faster/jx v1.1.0 // indirect
github.com/go-faster/yaml v0.4.6 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
Expand All @@ -58,11 +60,10 @@ require (
github.com/stretchr/objx v0.5.2 // indirect
github.com/vmihailenco/tagparser v0.1.2 // indirect
github.com/zclconf/go-cty v1.8.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/mod v0.18.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/genproto v0.0.0-20221118155620-16455021b5e6 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading

0 comments on commit 5ea4f92

Please sign in to comment.