From 57a671459b9b5af732e0cd3a02d1e8092822e46b Mon Sep 17 00:00:00 2001 From: Darren Shepherd Date: Fri, 22 Dec 2017 23:15:29 -0700 Subject: [PATCH] Various mapper changes --- types/mapper/access.go | 2 +- types/mapper/annotation_field.go | 10 ++++-- types/mapper/base64.go | 61 ++++++++++++++++++++++++++++++++ types/mapper/check.go | 2 +- types/mapper/condition.go | 27 ++++++++++++++ types/mapper/embed.go | 2 +- types/mapper/enum.go | 2 +- types/mapper/json_encode.go | 51 ++++++++++++++++++++++++++ types/mapper/label_field.go | 2 +- types/mapper/log.go | 7 ++++ types/mapper/metadata.go | 4 ++- types/mapper/read_only.go | 2 +- types/mapper/set_value.go | 39 ++++++++++++++------ types/mapper/slice_to_map.go | 2 +- types/mapper/untyped_move.go | 36 +++++++++++++++++++ 15 files changed, 228 insertions(+), 21 deletions(-) create mode 100644 types/mapper/base64.go create mode 100644 types/mapper/condition.go create mode 100644 types/mapper/json_encode.go create mode 100644 types/mapper/log.go create mode 100644 types/mapper/untyped_move.go diff --git a/types/mapper/access.go b/types/mapper/access.go index 14043c58..21cace8a 100644 --- a/types/mapper/access.go +++ b/types/mapper/access.go @@ -18,7 +18,7 @@ func (e Access) ToInternal(data map[string]interface{}) { func (e Access) ModifySchema(schema *types.Schema, schemas *types.Schemas) error { for name, access := range e.Fields { - if err := validateField(name, schema); err != nil { + if err := ValidateField(name, schema); err != nil { return err } diff --git a/types/mapper/annotation_field.go b/types/mapper/annotation_field.go index 94d68010..9bbf924c 100644 --- a/types/mapper/annotation_field.go +++ b/types/mapper/annotation_field.go @@ -9,8 +9,9 @@ import ( ) type AnnotationField struct { - Field string - Object bool + Field string + Object bool + IgnoreDefinition bool } func (e AnnotationField) FromInternal(data map[string]interface{}) { @@ -41,5 +42,8 @@ func (e AnnotationField) ToInternal(data map[string]interface{}) { } func (e AnnotationField) ModifySchema(schema *types.Schema, schemas *types.Schemas) error { - return validateField(e.Field, schema) + if e.IgnoreDefinition { + return nil + } + return ValidateField(e.Field, schema) } diff --git a/types/mapper/base64.go b/types/mapper/base64.go new file mode 100644 index 00000000..bc0ceb4d --- /dev/null +++ b/types/mapper/base64.go @@ -0,0 +1,61 @@ +package mapper + +import ( + "encoding/base64" + "strings" + + "github.com/rancher/norman/types" + "github.com/rancher/norman/types/convert" + "github.com/rancher/norman/types/values" +) + +type Base64 struct { + Field string + IgnoreDefinition bool + Separator string +} + +func (m Base64) FromInternal(data map[string]interface{}) { + if v, ok := values.RemoveValue(data, strings.Split(m.Field, m.getSep())...); ok { + str := convert.ToString(v) + if str == "" { + return + } + + newData, err := base64.StdEncoding.DecodeString(str) + if err != nil { + log.Errorf("failed to base64 decode data") + } + + values.PutValue(data, string(newData), strings.Split(m.Field, m.getSep())...) + } +} + +func (m Base64) ToInternal(data map[string]interface{}) { + if v, ok := values.RemoveValue(data, strings.Split(m.Field, m.getSep())...); ok { + str := convert.ToString(v) + if str == "" { + return + } + + newData := base64.StdEncoding.EncodeToString([]byte(str)) + values.PutValue(data, newData, strings.Split(m.Field, m.getSep())...) + } +} + +func (m Base64) ModifySchema(s *types.Schema, schemas *types.Schemas) error { + if !m.IgnoreDefinition { + if err := ValidateField(m.Field, s); err != nil { + return err + } + } + + return nil +} + +func (m Base64) getSep() string { + if m.Separator == "" { + return "/" + } + return m.Separator +} diff --git a/types/mapper/check.go b/types/mapper/check.go index 30e7cefa..b31bbc07 100644 --- a/types/mapper/check.go +++ b/types/mapper/check.go @@ -6,7 +6,7 @@ import ( "github.com/rancher/norman/types" ) -func validateField(field string, schema *types.Schema) error { +func ValidateField(field string, schema *types.Schema) error { if _, ok := schema.ResourceFields[field]; !ok { return fmt.Errorf("field %s missing on schema %s", field, schema.ID) } diff --git a/types/mapper/condition.go b/types/mapper/condition.go new file mode 100644 index 00000000..bcd6d5dd --- /dev/null +++ b/types/mapper/condition.go @@ -0,0 +1,27 @@ +package mapper + +import ( + "github.com/rancher/norman/types" +) + +type Condition struct { + Field string + Value interface{} + Mapper types.Mapper +} + +func (m Condition) FromInternal(data map[string]interface{}) { + if data[m.Field] == m.Value { + m.Mapper.FromInternal(data) + } +} + +func (m Condition) ToInternal(data map[string]interface{}) { + if data[m.Field] == m.Value { + m.Mapper.ToInternal(data) + } +} + +func (m Condition) ModifySchema(s *types.Schema, schemas *types.Schemas) error { + return m.Mapper.ModifySchema(s, schemas) +} diff --git a/types/mapper/embed.go b/types/mapper/embed.go index 94e7b9c7..86415f1a 100644 --- a/types/mapper/embed.go +++ b/types/mapper/embed.go @@ -43,7 +43,7 @@ func (e *Embed) ToInternal(data map[string]interface{}) { } func (e *Embed) ModifySchema(schema *types.Schema, schemas *types.Schemas) error { - err := validateField(e.Field, schema) + err := ValidateField(e.Field, schema) if err != nil { if e.Optional { return nil diff --git a/types/mapper/enum.go b/types/mapper/enum.go index 96d2e69f..29b1cdb8 100644 --- a/types/mapper/enum.go +++ b/types/mapper/enum.go @@ -44,5 +44,5 @@ func (e Enum) ToInternal(data map[string]interface{}) { } func (e Enum) ModifySchema(schema *types.Schema, schemas *types.Schemas) error { - return validateField(e.Field, schema) + return ValidateField(e.Field, schema) } diff --git a/types/mapper/json_encode.go b/types/mapper/json_encode.go new file mode 100644 index 00000000..af728366 --- /dev/null +++ b/types/mapper/json_encode.go @@ -0,0 +1,51 @@ +package mapper + +import ( + "strings" + + "encoding/json" + + "github.com/rancher/norman/types" + "github.com/rancher/norman/types/convert" + "github.com/rancher/norman/types/values" +) + +type JSONEncode struct { + Field string + IgnoreDefinition bool + Separator string +} + +func (m JSONEncode) FromInternal(data map[string]interface{}) { + if v, ok := values.RemoveValue(data, strings.Split(m.Field, m.getSep())...); ok { + obj := map[string]interface{}{} + if err := json.Unmarshal([]byte(convert.ToString(v)), &obj); err == nil { + values.PutValue(data, obj, strings.Split(m.Field, m.getSep())...) + } else { + log.Errorf("Failed to unmarshal json field: %v", err) + } + } +} + +func (m JSONEncode) ToInternal(data map[string]interface{}) { + if v, ok := values.RemoveValue(data, strings.Split(m.Field, m.getSep())...); ok && v != nil { + if bytes, err := json.Marshal(v); err == nil { + values.PutValue(data, string(bytes), strings.Split(m.Field, m.getSep())...) + } + } +} + +func (m JSONEncode) getSep() string { + if m.Separator == "" { + return "/" + } + return m.Separator +} + +func (m JSONEncode) ModifySchema(s *types.Schema, schemas *types.Schemas) error { + if m.IgnoreDefinition { + return nil + } + + return ValidateField(m.Field, s) +} diff --git a/types/mapper/label_field.go b/types/mapper/label_field.go index 1ffe5458..da8852a1 100644 --- a/types/mapper/label_field.go +++ b/types/mapper/label_field.go @@ -24,5 +24,5 @@ func (e LabelField) ToInternal(data map[string]interface{}) { } func (e LabelField) ModifySchema(schema *types.Schema, schemas *types.Schemas) error { - return validateField(e.Field, schema) + return ValidateField(e.Field, schema) } diff --git a/types/mapper/log.go b/types/mapper/log.go new file mode 100644 index 00000000..0743c403 --- /dev/null +++ b/types/mapper/log.go @@ -0,0 +1,7 @@ +package mapper + +import "github.com/sirupsen/logrus" + +var ( + log = logrus.WithField("component", "norman/mapper") +) diff --git a/types/mapper/metadata.go b/types/mapper/metadata.go index 2e06845d..c857047c 100644 --- a/types/mapper/metadata.go +++ b/types/mapper/metadata.go @@ -7,7 +7,9 @@ import ( func NewMetadataMapper() types.Mapper { return types.Mappers{ Drop{"generateName"}, - Move{From: "selfLink", To: "resourcePath"}, + //Move{From: "selfLink", To: "resourcePath"}, + Drop{"selfLink"}, + //Drop{"ownerReferences"}, Move{From: "uid", To: "uuid"}, Drop{"resourceVersion"}, Drop{"generation"}, diff --git a/types/mapper/read_only.go b/types/mapper/read_only.go index 71054496..311e933b 100644 --- a/types/mapper/read_only.go +++ b/types/mapper/read_only.go @@ -25,7 +25,7 @@ func (r ReadOnly) ModifySchema(schema *types.Schema, schemas *types.Schemas) err return nil } - if err := validateField(r.Field, schema); err != nil { + if err := ValidateField(r.Field, schema); err != nil { if r.Optional { return nil } diff --git a/types/mapper/set_value.go b/types/mapper/set_value.go index 58558693..81253c7e 100644 --- a/types/mapper/set_value.go +++ b/types/mapper/set_value.go @@ -10,40 +10,59 @@ import ( ) type SetValue struct { - From, To string - Value interface{} - IfEq interface{} + Field, To string + Value interface{} + IfEq interface{} + IgnoreDefinition bool } func (s SetValue) FromInternal(data map[string]interface{}) { - v, ok := values.GetValue(data, strings.Split(s.From, "/")...) + if s.IfEq == nil { + values.PutValue(data, s.Value, strings.Split(s.getTo(), "/")...) + return + } + + v, ok := values.GetValue(data, strings.Split(s.Field, "/")...) if !ok { return } if v == s.IfEq { - values.PutValue(data, s.Value, strings.Split(s.To, "/")...) + values.PutValue(data, s.Value, strings.Split(s.getTo(), "/")...) } } +func (s SetValue) getTo() string { + if s.To == "" { + return s.Field + } + return s.To +} + func (s SetValue) ToInternal(data map[string]interface{}) { - v, ok := values.GetValue(data, strings.Split(s.To, "/")...) + v, ok := values.GetValue(data, strings.Split(s.getTo(), "/")...) if !ok { return } - if v == s.Value { - values.PutValue(data, s.IfEq, strings.Split(s.From, "/")...) + if s.IfEq == nil { + values.RemoveValue(data, strings.Split(s.Field, "/")...) + } else if v == s.Value { + values.PutValue(data, s.IfEq, strings.Split(s.Field, "/")...) } } func (s SetValue) ModifySchema(schema *types.Schema, schemas *types.Schemas) error { - _, _, _, ok, err := getField(schema, schemas, s.To) + if s.IgnoreDefinition { + return nil + } + + _, _, _, ok, err := getField(schema, schemas, s.getTo()) if err != nil { return err } if !ok { - return fmt.Errorf("failed to find defined field for %s on schemas %s", s.To, schema.ID) + return fmt.Errorf("failed to find defined field for %s on schemas %s", s.getTo(), schema.ID) } return nil diff --git a/types/mapper/slice_to_map.go b/types/mapper/slice_to_map.go index 64886563..78c47cd3 100644 --- a/types/mapper/slice_to_map.go +++ b/types/mapper/slice_to_map.go @@ -47,7 +47,7 @@ func (s SliceToMap) ToInternal(data map[string]interface{}) { } func (s SliceToMap) ModifySchema(schema *types.Schema, schemas *types.Schemas) error { - err := validateField(s.Field, schema) + err := ValidateField(s.Field, schema) if err != nil { return err } diff --git a/types/mapper/untyped_move.go b/types/mapper/untyped_move.go new file mode 100644 index 00000000..4b1a4910 --- /dev/null +++ b/types/mapper/untyped_move.go @@ -0,0 +1,36 @@ +package mapper + +import ( + "strings" + + "github.com/rancher/norman/types" + "github.com/rancher/norman/types/values" +) + +type UntypedMove struct { + From, To string + Separator string +} + +func (m UntypedMove) FromInternal(data map[string]interface{}) { + if v, ok := values.RemoveValue(data, strings.Split(m.From, m.getSep())...); ok { + values.PutValue(data, v, strings.Split(m.To, m.getSep())...) + } +} + +func (m UntypedMove) ToInternal(data map[string]interface{}) { + if v, ok := values.RemoveValue(data, strings.Split(m.To, m.getSep())...); ok { + values.PutValue(data, v, strings.Split(m.From, m.getSep())...) + } +} + +func (m UntypedMove) getSep() string { + if m.Separator == "" { + return "/" + } + return m.Separator +} + +func (m UntypedMove) ModifySchema(s *types.Schema, schemas *types.Schemas) error { + return nil +}