Skip to content

Commit 5ea4f92

Browse files
authored
Merge pull request #2 from prgres/entoas/feat-extentions
feat: add Extention annotation and add support for x-gen-operation-group ant ent#590
2 parents cd4926c + e8c39e0 commit 5ea4f92

File tree

5 files changed

+161
-67
lines changed

5 files changed

+161
-67
lines changed

entoas/annotation.go

+55-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ import (
2020
"entgo.io/contrib/entoas/serialization"
2121
"entgo.io/ent/entc/gen"
2222
"entgo.io/ent/schema"
23+
yaml "github.com/go-faster/yaml"
2324
"github.com/ogen-go/ogen"
2425
)
2526

27+
const XOgenOperationGroup = "x-ogen-operation-group"
28+
2629
type (
2730
// Annotation annotates fields and edges with metadata for spec generation.
2831
Annotation struct {
@@ -46,16 +49,29 @@ type (
4649
ReadOnly bool
4750
// Skip specifies that the field will be ignored in spec.
4851
Skip bool
52+
// Extensions has map of OpenApi extenions
53+
Extensions OgenExtensions
4954
}
55+
OgenExtensions ogen.Extensions
5056
// OperationConfig holds meta information about a REST operation.
5157
OperationConfig struct {
52-
Policy Policy
53-
Groups serialization.Groups
58+
Policy Policy
59+
Groups serialization.Groups
60+
Extensions OgenExtensions
61+
}
62+
// OpenApiExtension holds meta information about OpenApi extension
63+
OpenApiExtension struct {
64+
Name string
65+
Value string
5466
}
5567
// OperationConfigOption allows managing OperationConfig using functional arguments.
5668
OperationConfigOption func(*OperationConfig)
5769
)
5870

71+
func (o OgenExtensions) Schema() ogen.Extensions {
72+
return ogen.Extensions(o)
73+
}
74+
5975
// Groups returns a OperationConfigOption that adds the given serialization groups to a OperationConfig.
6076
func Groups(gs ...string) Annotation {
6177
return Annotation{Groups: gs}
@@ -66,6 +82,43 @@ func OperationGroups(gs ...string) OperationConfigOption {
6682
return func(c *OperationConfig) { c.Groups = gs }
6783
}
6884

85+
func OperationExtentions(ext ...OpenApiExtension) OperationConfigOption {
86+
return func(c *OperationConfig) {
87+
if c.Extensions == nil {
88+
c.Extensions = OgenExtensions{}
89+
}
90+
91+
for _, e := range ext {
92+
c.Extensions[e.Name] = yaml.Node{Kind: yaml.ScalarNode, Value: e.Value}
93+
}
94+
}
95+
}
96+
97+
func Extensions(ext ...OpenApiExtension) Annotation {
98+
exts := OgenExtensions{}
99+
100+
for _, e := range ext {
101+
exts[e.Name] = yaml.Node{Kind: yaml.ScalarNode, Value: e.Value}
102+
}
103+
104+
return Annotation{Extensions: exts}
105+
}
106+
107+
func OperationExtentionGroup(group string) OperationConfigOption {
108+
return OperationExtentions(OpenApiExtensionOperationGroup(group))
109+
}
110+
111+
func ExtentionOperationGroup(group string) Annotation {
112+
return Extensions(OpenApiExtensionOperationGroup(group))
113+
}
114+
115+
func OpenApiExtensionOperationGroup(group string) OpenApiExtension {
116+
return OpenApiExtension{
117+
Name: XOgenOperationGroup,
118+
Value: group,
119+
}
120+
}
121+
69122
// OperationPolicy returns a OperationConfigOption that sets the Policy of a OperationConfig to the given one.
70123
func OperationPolicy(p Policy) OperationConfigOption {
71124
return func(c *OperationConfig) { c.Policy = p }

entoas/annotation_test.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,19 @@ func TestAnnotation(t *testing.T) {
3636
require.Equal(t, serialization.Groups{"create", "groups"}, a.Groups)
3737

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

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

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

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

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

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

5959
a = a.Merge(b).(Annotation).Merge(c).(Annotation)
60+
// a.List.Extensions = ogen.Extensions{
61+
// "zxc": yaml.Node{Kind: yaml.ScalarNode, Value: "ccc"},
62+
// }
6063
ex := Annotation{
6164
Example: "example",
6265
Schema: ogen.Binary(),
6366
List: OperationConfig{
6467
Groups: serialization.Groups{"list", "groups"},
6568
Policy: PolicyExpose,
69+
// Extensions: ogen.Extensions{
70+
// "zxc": yaml.Node{Kind: yaml.ScalarNode, Value: "ccc"},
71+
// },
6672
},
6773
}
6874
require.Equal(t, ex, a)
@@ -89,6 +95,7 @@ func TestAnnotation(t *testing.T) {
8995
require.NotNil(t, ac)
9096
ac, err = SchemaAnnotation(&gen.Type{Annotations: gen.Annotations{a.Name(): ex}})
9197
require.NoError(t, err)
98+
9299
require.Equal(t, &ex, ac)
93100

94101
ac, err = FieldAnnotation(new(gen.Field))

entoas/generator.go

+43
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,14 @@ func paths(g *gen.Graph, spec *ogen.Spec) error {
195195
}
196196
// root for all operations on this node.
197197
root := "/" + rules.Pluralize(strcase.KebabCase(n.Name))
198+
199+
ant := &Annotation{}
200+
if err := ant.Decode(n.Annotations[ant.Name()]); err != nil {
201+
return err
202+
}
203+
204+
path(spec, root).Common.Extensions = ant.Extensions.Schema()
205+
198206
// Create operation.
199207
if contains(ops, OpCreate) {
200208
path(spec, root).Post, err = createOp(spec, n, cfg.AllowClientUUIDs)
@@ -294,6 +302,13 @@ func createOp(spec *ogen.Spec, n *gen.Type, allowClientUUIDs bool) (*ogen.Operat
294302
spec.RefResponse(strconv.Itoa(http.StatusConflict)),
295303
spec.RefResponse(strconv.Itoa(http.StatusInternalServerError)),
296304
)
305+
306+
ant := &Annotation{}
307+
if err := ant.Decode(n.Annotations[ant.Name()]); err != nil {
308+
return nil, err
309+
}
310+
op.Common.Extensions = ant.Create.Extensions.Schema()
311+
297312
return op, nil
298313
}
299314

@@ -325,6 +340,13 @@ func readOp(spec *ogen.Spec, n *gen.Type) (*ogen.Operation, error) {
325340
spec.RefResponse(strconv.Itoa(http.StatusNotFound)),
326341
spec.RefResponse(strconv.Itoa(http.StatusInternalServerError)),
327342
)
343+
344+
ant := &Annotation{}
345+
if err := ant.Decode(n.Annotations[ant.Name()]); err != nil {
346+
return nil, err
347+
}
348+
op.Common.Extensions = ant.Read.Extensions.Schema()
349+
328350
return op, nil
329351
}
330352

@@ -395,6 +417,13 @@ func updateOp(spec *ogen.Spec, n *gen.Type) (*ogen.Operation, error) {
395417
spec.RefResponse(strconv.Itoa(http.StatusNotFound)),
396418
spec.RefResponse(strconv.Itoa(http.StatusInternalServerError)),
397419
)
420+
421+
ant := &Annotation{}
422+
if err := ant.Decode(n.Annotations[ant.Name()]); err != nil {
423+
return nil, err
424+
}
425+
op.Common.Extensions = ant.Update.Extensions.Schema()
426+
398427
return op, nil
399428
}
400429

@@ -421,6 +450,13 @@ func deleteOp(spec *ogen.Spec, n *gen.Type) (*ogen.Operation, error) {
421450
spec.RefResponse(strconv.Itoa(http.StatusNotFound)),
422451
spec.RefResponse(strconv.Itoa(http.StatusInternalServerError)),
423452
)
453+
454+
ant := &Annotation{}
455+
if err := ant.Decode(n.Annotations[ant.Name()]); err != nil {
456+
return nil, err
457+
}
458+
op.Common.Extensions = ant.Delete.Extensions.Schema()
459+
424460
return op, nil
425461
}
426462

@@ -466,6 +502,13 @@ func listOp(spec *ogen.Spec, n *gen.Type) (*ogen.Operation, error) {
466502
spec.RefResponse(strconv.Itoa(http.StatusNotFound)),
467503
spec.RefResponse(strconv.Itoa(http.StatusInternalServerError)),
468504
)
505+
506+
ant := &Annotation{}
507+
if err := ant.Decode(n.Annotations[ant.Name()]); err != nil {
508+
return nil, err
509+
}
510+
op.Common.Extensions = ant.List.Extensions.Schema()
511+
469512
return op, nil
470513
}
471514

go.mod

+17-16
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
module entgo.io/contrib
22

3-
go 1.21
3+
go 1.21.0
4+
5+
toolchain go1.22.4
46

5-
toolchain go1.21.6
67

78
require (
89
entgo.io/ent v0.13.0
910
github.com/99designs/gqlgen v0.17.48
1011
github.com/AlekSi/pointer v1.1.0
1112
github.com/alecthomas/kong v0.7.0
13+
github.com/go-faster/yamlx v0.4.1
1214
github.com/go-openapi/inflect v0.19.0
1315
github.com/google/uuid v1.6.0
1416
github.com/hashicorp/go-multierror v1.1.1
@@ -21,11 +23,11 @@ require (
2123
github.com/stretchr/testify v1.9.0
2224
github.com/vektah/gqlparser/v2 v2.5.12
2325
github.com/vmihailenco/msgpack/v5 v5.0.0-beta.9
24-
go.uber.org/multierr v1.9.0
25-
go.uber.org/zap v1.24.0
26-
golang.org/x/exp v0.0.0-20221230185412-738e83a70c30
26+
go.uber.org/multierr v1.11.0
27+
go.uber.org/zap v1.27.0
28+
golang.org/x/exp v0.0.0-20230725093048-515e97ebf090
2729
golang.org/x/sync v0.7.0
28-
golang.org/x/tools v0.21.0
30+
golang.org/x/tools v0.22.0
2931
google.golang.org/grpc v1.52.3
3032
google.golang.org/protobuf v1.34.2
3133
)
@@ -36,12 +38,12 @@ require (
3638
github.com/agnivade/levenshtein v1.1.1 // indirect
3739
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
3840
github.com/davecgh/go-spew v1.1.1 // indirect
39-
github.com/dlclark/regexp2 v1.7.0 // indirect
40-
github.com/fatih/color v1.13.0 // indirect
41+
github.com/dlclark/regexp2 v1.11.0 // indirect
42+
github.com/fatih/color v1.17.0 // indirect
4143
github.com/ghodss/yaml v1.0.0 // indirect
42-
github.com/go-faster/errors v0.6.1 // indirect
43-
github.com/go-faster/jx v0.40.0 // indirect
44-
github.com/go-faster/yamlx v0.4.1 // indirect
44+
github.com/go-faster/errors v0.7.1 // indirect
45+
github.com/go-faster/jx v1.1.0 // indirect
46+
github.com/go-faster/yaml v0.4.6 // indirect
4547
github.com/golang/protobuf v1.5.4 // indirect
4648
github.com/google/go-cmp v0.6.0 // indirect
4749
github.com/gorilla/websocket v1.5.0 // indirect
@@ -58,11 +60,10 @@ require (
5860
github.com/stretchr/objx v0.5.2 // indirect
5961
github.com/vmihailenco/tagparser v0.1.2 // indirect
6062
github.com/zclconf/go-cty v1.8.0 // indirect
61-
go.uber.org/atomic v1.10.0 // indirect
62-
golang.org/x/mod v0.17.0 // indirect
63-
golang.org/x/net v0.25.0 // indirect
64-
golang.org/x/sys v0.20.0 // indirect
65-
golang.org/x/text v0.15.0 // indirect
63+
golang.org/x/mod v0.18.0 // indirect
64+
golang.org/x/net v0.26.0 // indirect
65+
golang.org/x/sys v0.21.0 // indirect
66+
golang.org/x/text v0.16.0 // indirect
6667
google.golang.org/genproto v0.0.0-20221118155620-16455021b5e6 // indirect
6768
gopkg.in/yaml.v2 v2.4.0 // indirect
6869
gopkg.in/yaml.v3 v3.0.1 // indirect

0 commit comments

Comments
 (0)