Skip to content

Commit

Permalink
feat: add Extention annotation and add annotation for x-gen-operation…
Browse files Browse the repository at this point in the history
…-group ant
  • Loading branch information
prgres committed Jun 27, 2024
1 parent bbd64e9 commit 8220aaf
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 62 deletions.
52 changes: 50 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,11 +49,19 @@ type (
ReadOnly bool
// Skip specifies that the field will be ignored in spec.
Skip bool
// Extensions has map of OpenApi extenions
Extensions ogen.Extensions
}
// OperationConfig holds meta information about a REST operation.
OperationConfig struct {
Policy Policy
Groups serialization.Groups
Policy Policy
Groups serialization.Groups
Extensions ogen.Extensions
}
// OpenApiExtension holds meta information about OpenApi extension
OpenApiExtension struct {
Name string
Value string
}
// OperationConfigOption allows managing OperationConfig using functional arguments.
OperationConfigOption func(*OperationConfig)
Expand All @@ -66,6 +77,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 = ogen.Extensions{}
}

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

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

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

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

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

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

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

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

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 8220aaf

Please sign in to comment.