From b4a72e59f76a1cc669ab20eba18c85ddbd8925b4 Mon Sep 17 00:00:00 2001 From: Josh Meranda Date: Mon, 25 Nov 2024 13:41:25 -0500 Subject: [PATCH] Schema leak (#522) * remove s.schemas in doRemoveSchema * add tests for addSchema * use slices pkg to search for candidates --------- Co-authored-by: joshmeranda --- types/schemas.go | 16 ++++++--- types/schemas_test.go | 76 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 5 deletions(-) create mode 100644 types/schemas_test.go diff --git a/types/schemas.go b/types/schemas.go index 30819bba..ac1f7e89 100644 --- a/types/schemas.go +++ b/types/schemas.go @@ -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) diff --git a/types/schemas_test.go b/types/schemas_test.go new file mode 100644 index 00000000..2bffa661 --- /dev/null +++ b/types/schemas_test.go @@ -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) +}