Skip to content

Commit 8220aaf

Browse files
committed
feat: add Extention annotation and add annotation for x-gen-operation-group ant
1 parent bbd64e9 commit 8220aaf

File tree

4 files changed

+144
-62
lines changed

4 files changed

+144
-62
lines changed

entoas/annotation.go

+50-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,11 +49,19 @@ 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 ogen.Extensions
4954
}
5055
// OperationConfig holds meta information about a REST operation.
5156
OperationConfig struct {
52-
Policy Policy
53-
Groups serialization.Groups
57+
Policy Policy
58+
Groups serialization.Groups
59+
Extensions ogen.Extensions
60+
}
61+
// OpenApiExtension holds meta information about OpenApi extension
62+
OpenApiExtension struct {
63+
Name string
64+
Value string
5465
}
5566
// OperationConfigOption allows managing OperationConfig using functional arguments.
5667
OperationConfigOption func(*OperationConfig)
@@ -66,6 +77,43 @@ func OperationGroups(gs ...string) OperationConfigOption {
6677
return func(c *OperationConfig) { c.Groups = gs }
6778
}
6879

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

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
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
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
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
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
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
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)