Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Extention annotation and add support for x-gen-operation-group ant #590

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
62 changes: 62 additions & 0 deletions entoas/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,40 +195,51 @@ 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
}

// Create operation.
if contains(ops, OpCreate) {
path(spec, root).Post, err = createOp(spec, n, cfg.AllowClientUUIDs)
if err != nil {
return err
}
path(spec, root).Common.Extensions = ant.Extensions.Schema()
}
// Read operation.
if contains(ops, OpRead) {
path(spec, root+"/{id}").Get, err = readOp(spec, n)
if err != nil {
return err
}
path(spec, root+"/{id}").Common.Extensions = ant.Extensions.Schema()
}
// Update operation.
if contains(ops, OpUpdate) {
path(spec, root+"/{id}").Patch, err = updateOp(spec, n)
if err != nil {
return err
}
path(spec, root+"/{id}").Common.Extensions = ant.Extensions.Schema()
}
// Delete operation.
if contains(ops, OpDelete) {
path(spec, root+"/{id}").Delete, err = deleteOp(spec, n)
if err != nil {
return err
}
path(spec, root+"/{id}").Common.Extensions = ant.Extensions.Schema()
}
// List operation.
if contains(ops, OpList) {
path(spec, root).Get, err = listOp(spec, n)
if err != nil {
return err
}
path(spec, root).Common.Extensions = ant.Extensions.Schema()
}
// Sub-Resource operations.
for _, e := range n.Edges {
Expand All @@ -243,13 +254,15 @@ func paths(g *gen.Graph, spec *ogen.Spec) error {
if err != nil {
return err
}
path(spec, subRoot).Common.Extensions = ant.Extensions.Schema()
}
// List operation.
if contains(ops, OpList) {
path(spec, subRoot).Get, err = listEdgeOp(spec, n, e)
if err != nil {
return err
}
path(spec, subRoot).Common.Extensions = ant.Extensions.Schema()
}
}
}
Expand Down Expand Up @@ -294,6 +307,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 +345,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 @@ -359,6 +386,13 @@ func readEdgeOp(spec *ogen.Spec, n *gen.Type, e *gen.Edge) (*ogen.Operation, err
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 +429,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 +462,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 +514,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 Expand Up @@ -512,6 +567,13 @@ func listEdgeOp(spec *ogen.Spec, n *gen.Type, e *gen.Edge) (*ogen.Operation, err
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
32 changes: 16 additions & 16 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
module entgo.io/contrib

go 1.21
go 1.21.0

toolchain go1.21.6
toolchain go1.22.4

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/yaml v0.4.6
github.com/go-openapi/inflect v0.19.0
github.com/google/uuid v1.6.0
github.com/hashicorp/go-multierror v1.1.1
github.com/jhump/protoreflect v1.10.1
github.com/mattn/go-sqlite3 v1.14.16
github.com/mitchellh/mapstructure v1.5.0
github.com/ogen-go/ogen v0.56.1
github.com/ogen-go/ogen v1.2.3-0.20240627205112-01e42b79fbda
github.com/oklog/ulid/v2 v2.0.2
github.com/stoewer/go-strcase v1.2.0
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,11 +37,11 @@ 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/errors v0.7.1 // indirect
github.com/go-faster/jx v1.1.0 // indirect
github.com/go-faster/yamlx v0.4.1 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/go-cmp v0.6.0 // indirect
Expand All @@ -58,11 +59,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