Skip to content

Commit

Permalink
chore: add OgenExtentions type and fix testes
Browse files Browse the repository at this point in the history
  • Loading branch information
prgres committed Jun 30, 2024
1 parent 8220aaf commit e8c39e0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
13 changes: 9 additions & 4 deletions entoas/annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,14 @@ type (
// Skip specifies that the field will be ignored in spec.
Skip bool
// Extensions has map of OpenApi extenions
Extensions ogen.Extensions
Extensions OgenExtensions
}
OgenExtensions ogen.Extensions
// OperationConfig holds meta information about a REST operation.
OperationConfig struct {
Policy Policy
Groups serialization.Groups
Extensions ogen.Extensions
Extensions OgenExtensions
}
// OpenApiExtension holds meta information about OpenApi extension
OpenApiExtension struct {
Expand All @@ -67,6 +68,10 @@ type (
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 @@ -80,7 +85,7 @@ func OperationGroups(gs ...string) OperationConfigOption {
func OperationExtentions(ext ...OpenApiExtension) OperationConfigOption {
return func(c *OperationConfig) {
if c.Extensions == nil {
c.Extensions = ogen.Extensions{}
c.Extensions = OgenExtensions{}
}

for _, e := range ext {
Expand All @@ -90,7 +95,7 @@ func OperationExtentions(ext ...OpenApiExtension) OperationConfigOption {
}

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

for _, e := range ext {
exts[e.Name] = yaml.Node{Kind: yaml.ScalarNode, Value: e.Value}
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
12 changes: 6 additions & 6 deletions entoas/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func paths(g *gen.Graph, spec *ogen.Spec) error {
return err
}

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

// Create operation.
if contains(ops, OpCreate) {
Expand Down Expand Up @@ -307,7 +307,7 @@ func createOp(spec *ogen.Spec, n *gen.Type, allowClientUUIDs bool) (*ogen.Operat
if err := ant.Decode(n.Annotations[ant.Name()]); err != nil {
return nil, err
}
op.Common.Extensions = ant.Create.Extensions
op.Common.Extensions = ant.Create.Extensions.Schema()

return op, nil
}
Expand Down Expand Up @@ -345,7 +345,7 @@ func readOp(spec *ogen.Spec, n *gen.Type) (*ogen.Operation, error) {
if err := ant.Decode(n.Annotations[ant.Name()]); err != nil {
return nil, err
}
op.Common.Extensions = ant.Read.Extensions
op.Common.Extensions = ant.Read.Extensions.Schema()

return op, nil
}
Expand Down Expand Up @@ -422,7 +422,7 @@ func updateOp(spec *ogen.Spec, n *gen.Type) (*ogen.Operation, error) {
if err := ant.Decode(n.Annotations[ant.Name()]); err != nil {
return nil, err
}
op.Common.Extensions = ant.Update.Extensions
op.Common.Extensions = ant.Update.Extensions.Schema()

return op, nil
}
Expand Down Expand Up @@ -455,7 +455,7 @@ func deleteOp(spec *ogen.Spec, n *gen.Type) (*ogen.Operation, error) {
if err := ant.Decode(n.Annotations[ant.Name()]); err != nil {
return nil, err
}
op.Common.Extensions = ant.Delete.Extensions
op.Common.Extensions = ant.Delete.Extensions.Schema()

return op, nil
}
Expand Down Expand Up @@ -507,7 +507,7 @@ func listOp(spec *ogen.Spec, n *gen.Type) (*ogen.Operation, error) {
if err := ant.Decode(n.Annotations[ant.Name()]); err != nil {
return nil, err
}
op.Common.Extensions = ant.List.Extensions
op.Common.Extensions = ant.List.Extensions.Schema()

return op, nil
}
Expand Down

0 comments on commit e8c39e0

Please sign in to comment.