From 4cfba4d6852e3047bc801e6afc7ad6a939039b0e Mon Sep 17 00:00:00 2001 From: Liam Stanley Date: Sat, 27 Apr 2024 06:10:05 +0000 Subject: [PATCH 1/2] fix: entoas.GetConfig incorrectly checks nil configuration --- entoas/extension.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entoas/extension.go b/entoas/extension.go index 5c155ec3b..5a988e6f9 100644 --- a/entoas/extension.go +++ b/entoas/extension.go @@ -234,7 +234,7 @@ func (c *Config) Decode(o interface{}) error { // GetConfig loads the entoas.Config from the given *gen.Config object. func GetConfig(cfg *gen.Config) (*Config, error) { c := &Config{} - if cfg == nil && cfg.Annotations == nil && cfg.Annotations[c.Name()] == nil { + if cfg != nil && cfg.Annotations != nil && cfg.Annotations[c.Name()] == nil { return nil, errors.New("entoas extension configuration not found") } return c, c.Decode(cfg.Annotations[c.Name()]) From 41ded50d34c2ef5c30ea83403db4a27c3eca0068 Mon Sep 17 00:00:00 2001 From: Liam Stanley Date: Sat, 27 Apr 2024 06:15:10 +0000 Subject: [PATCH 2/2] lint: entoas early return for code simplification addl: use new recommended file perm syntax --- entoas/extension.go | 2 +- entoas/generator.go | 95 ++++++++++++++++++++++----------------------- 2 files changed, 48 insertions(+), 49 deletions(-) diff --git a/entoas/extension.go b/entoas/extension.go index 5a988e6f9..b747efb16 100644 --- a/entoas/extension.go +++ b/entoas/extension.go @@ -213,7 +213,7 @@ func (ex *Extension) generate(next gen.Generator) gen.Generator { _, err = ex.out.Write(b) return err } - return os.WriteFile(filepath.Join(g.Target, "openapi.json"), b, 0644) + return os.WriteFile(filepath.Join(g.Target, "openapi.json"), b, 0o644) }) } diff --git a/entoas/generator.go b/entoas/generator.go index 0a2d6f8ee..08ff52f02 100644 --- a/entoas/generator.go +++ b/entoas/generator.go @@ -610,32 +610,32 @@ func NodeOperations(n *gen.Type) ([]Operation, error) { return []Operation{OpCreate, OpRead, OpUpdate, OpDelete, OpList}, nil } return nil, nil - } else { - // An operation gets exposed if it is either - // - annotated with PolicyExpose or - // - not annotated with PolicyExclude and the DefaultPolicy is PolicyExpose. - if err := ant.Decode(n.Annotations[ant.Name()]); err != nil { - return nil, err - } - var ops []Operation - for op, opn := range map[Operation]OperationConfig{ - OpCreate: ant.Create, - OpRead: ant.Read, - OpUpdate: ant.Update, - OpDelete: ant.Delete, - OpList: ant.List, - } { - // If the operation is explicitly annotated to be exposed do so. - if opn.Policy == PolicyExpose || (opn.Policy == PolicyNone && c.DefaultPolicy == PolicyExpose) { - ops = append(ops, op) - continue - } + } + + // An operation gets exposed if it is either + // - annotated with PolicyExpose or + // - not annotated with PolicyExclude and the DefaultPolicy is PolicyExpose. + if err := ant.Decode(n.Annotations[ant.Name()]); err != nil { + return nil, err + } + var ops []Operation + for op, opn := range map[Operation]OperationConfig{ + OpCreate: ant.Create, + OpRead: ant.Read, + OpUpdate: ant.Update, + OpDelete: ant.Delete, + OpList: ant.List, + } { + // If the operation is explicitly annotated to be exposed do so. + if opn.Policy == PolicyExpose || (opn.Policy == PolicyNone && c.DefaultPolicy == PolicyExpose) { + ops = append(ops, op) + continue } - sort.Slice(ops, func(i, j int) bool { - return ops[i] < ops[j] - }) - return ops, nil } + sort.Slice(ops, func(i, j int) bool { + return ops[i] < ops[j] + }) + return ops, nil } // EdgeOperations returns the list of operations to expose for this edge. @@ -650,36 +650,35 @@ func EdgeOperations(e *gen.Edge) ([]Operation, error) { if c.DefaultPolicy == PolicyExpose { if e.Unique { return []Operation{OpRead}, nil - } else { - return []Operation{OpList}, nil } + return []Operation{OpList}, nil } return nil, nil + } + + // An edge-operation gets exposed if it is either + // - annotated with PolicyExpose or + // - not annotated with PolicyExclude and the DefaultPolicy is PolicyExpose. + if err := ant.Decode(e.Annotations[ant.Name()]); err != nil { + return nil, err + } + var ops []Operation + m := make(map[Operation]OperationConfig) + if e.Unique { + m[OpRead] = ant.Read } else { - // An edge-operation gets exposed if it is either - // - annotated with PolicyExpose or - // - not annotated with PolicyExclude and the DefaultPolicy is PolicyExpose. - if err := ant.Decode(e.Annotations[ant.Name()]); err != nil { - return nil, err - } - var ops []Operation - m := make(map[Operation]OperationConfig) - if e.Unique { - m[OpRead] = ant.Read - } else { - m[OpList] = ant.List - } - for op, opn := range m { - if opn.Policy == PolicyExpose || (opn.Policy == PolicyNone && c.DefaultPolicy == PolicyExpose) { - ops = append(ops, op) - continue - } + m[OpList] = ant.List + } + for op, opn := range m { + if opn.Policy == PolicyExpose || (opn.Policy == PolicyNone && c.DefaultPolicy == PolicyExpose) { + ops = append(ops, op) + continue } - sort.Slice(ops, func(i, j int) bool { - return ops[i] < ops[j] - }) - return ops, nil } + sort.Slice(ops, func(i, j int) bool { + return ops[i] < ops[j] + }) + return ops, nil } // reqBody returns the request body for the given node and operation.