mirror of
https://github.com/rancher/norman.git
synced 2025-09-18 16:35:19 +00:00
Schema leak (#522)
* remove s.schemas in doRemoveSchema * add tests for addSchema * use slices pkg to search for candidates --------- Co-authored-by: joshmeranda <joshua.meranda@gmail.com>
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
@@ -84,6 +85,10 @@ func (s *Schemas) doRemoveSchema(schema Schema) *Schemas {
|
||||
s.removeEmbed(&schema)
|
||||
}
|
||||
|
||||
s.schemas = slices.DeleteFunc(s.schemas, func(candidate *Schema) bool {
|
||||
return candidate.ID == schema.ID
|
||||
})
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
@@ -136,11 +141,12 @@ func (s *Schemas) doAddSchema(schema Schema, replace bool) *Schemas {
|
||||
schemas[schema.ID] = &schema
|
||||
|
||||
if replace {
|
||||
for i, candidate := range s.schemas {
|
||||
if candidate.ID == schema.ID {
|
||||
s.schemas[i] = &schema
|
||||
break
|
||||
}
|
||||
i := slices.IndexFunc(s.schemas, func(candidate *Schema) bool {
|
||||
return candidate.ID == schema.ID
|
||||
})
|
||||
|
||||
if i >= 0 {
|
||||
s.schemas[i] = &schema
|
||||
}
|
||||
} else {
|
||||
s.schemas = append(s.schemas, &schema)
|
||||
|
76
types/schemas_test.go
Normal file
76
types/schemas_test.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestSchemas(t *testing.T) {
|
||||
version := APIVersion{
|
||||
Group: "meta.cattle.io",
|
||||
Version: "v1",
|
||||
Path: "/shire",
|
||||
}
|
||||
|
||||
s := NewSchemas().
|
||||
AddSchema(Schema{
|
||||
ID: "baggins",
|
||||
PluralName: "bagginses",
|
||||
Version: version,
|
||||
CollectionMethods: []string{},
|
||||
ResourceMethods: []string{},
|
||||
ResourceFields: map[string]Field{},
|
||||
}).
|
||||
AddSchema(Schema{
|
||||
ID: "hobbit",
|
||||
PluralName: "hobbits",
|
||||
Embed: true,
|
||||
EmbedType: "baggins",
|
||||
Version: version,
|
||||
CollectionMethods: []string{},
|
||||
ResourceMethods: []string{},
|
||||
ResourceFields: map[string]Field{
|
||||
"breakfasts": {Type: "int"},
|
||||
"name": {Type: "string"},
|
||||
},
|
||||
})
|
||||
|
||||
expected := []*Schema{
|
||||
{
|
||||
ID: "hobbit",
|
||||
PluralName: "hobbits",
|
||||
Embed: true,
|
||||
EmbedType: "baggins",
|
||||
Version: version,
|
||||
CollectionMethods: []string{},
|
||||
ResourceMethods: []string{},
|
||||
ResourceFields: map[string]Field{
|
||||
"breakfasts": {Type: "int"},
|
||||
"name": {Type: "string"},
|
||||
},
|
||||
CodeName: "Hobbit",
|
||||
CodeNamePlural: "Hobbits",
|
||||
BaseType: "hobbit",
|
||||
Type: "/meta/schemas/schema",
|
||||
},
|
||||
{
|
||||
ID: "baggins",
|
||||
PluralName: "bagginses",
|
||||
Version: version,
|
||||
CollectionMethods: []string{},
|
||||
ResourceMethods: []string{},
|
||||
ResourceFields: map[string]Field{
|
||||
"breakfasts": {Type: "int"},
|
||||
"name": {Type: "string"},
|
||||
},
|
||||
CodeName: "Baggins",
|
||||
CodeNamePlural: "Bagginses",
|
||||
BaseType: "baggins",
|
||||
Type: "/meta/schemas/schema",
|
||||
},
|
||||
}
|
||||
actual := s.Schemas()
|
||||
|
||||
assert.ElementsMatch(t, expected, actual)
|
||||
}
|
Reference in New Issue
Block a user