mirror of
https://github.com/rancher/norman.git
synced 2025-08-22 09:05:50 +00:00
Various mapper changes
This commit is contained in:
parent
c2d17541be
commit
57a671459b
@ -18,7 +18,7 @@ func (e Access) ToInternal(data map[string]interface{}) {
|
|||||||
|
|
||||||
func (e Access) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
|
func (e Access) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
|
||||||
for name, access := range e.Fields {
|
for name, access := range e.Fields {
|
||||||
if err := validateField(name, schema); err != nil {
|
if err := ValidateField(name, schema); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,8 +9,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type AnnotationField struct {
|
type AnnotationField struct {
|
||||||
Field string
|
Field string
|
||||||
Object bool
|
Object bool
|
||||||
|
IgnoreDefinition bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e AnnotationField) FromInternal(data map[string]interface{}) {
|
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 {
|
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)
|
||||||
}
|
}
|
||||||
|
61
types/mapper/base64.go
Normal file
61
types/mapper/base64.go
Normal file
@ -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
|
||||||
|
}
|
@ -6,7 +6,7 @@ import (
|
|||||||
"github.com/rancher/norman/types"
|
"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 {
|
if _, ok := schema.ResourceFields[field]; !ok {
|
||||||
return fmt.Errorf("field %s missing on schema %s", field, schema.ID)
|
return fmt.Errorf("field %s missing on schema %s", field, schema.ID)
|
||||||
}
|
}
|
||||||
|
27
types/mapper/condition.go
Normal file
27
types/mapper/condition.go
Normal file
@ -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)
|
||||||
|
}
|
@ -43,7 +43,7 @@ func (e *Embed) ToInternal(data map[string]interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *Embed) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
|
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 err != nil {
|
||||||
if e.Optional {
|
if e.Optional {
|
||||||
return nil
|
return nil
|
||||||
|
@ -44,5 +44,5 @@ func (e Enum) ToInternal(data map[string]interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e Enum) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
|
func (e Enum) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
|
||||||
return validateField(e.Field, schema)
|
return ValidateField(e.Field, schema)
|
||||||
}
|
}
|
||||||
|
51
types/mapper/json_encode.go
Normal file
51
types/mapper/json_encode.go
Normal file
@ -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)
|
||||||
|
}
|
@ -24,5 +24,5 @@ func (e LabelField) ToInternal(data map[string]interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e LabelField) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
|
func (e LabelField) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
|
||||||
return validateField(e.Field, schema)
|
return ValidateField(e.Field, schema)
|
||||||
}
|
}
|
||||||
|
7
types/mapper/log.go
Normal file
7
types/mapper/log.go
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package mapper
|
||||||
|
|
||||||
|
import "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
|
var (
|
||||||
|
log = logrus.WithField("component", "norman/mapper")
|
||||||
|
)
|
@ -7,7 +7,9 @@ import (
|
|||||||
func NewMetadataMapper() types.Mapper {
|
func NewMetadataMapper() types.Mapper {
|
||||||
return types.Mappers{
|
return types.Mappers{
|
||||||
Drop{"generateName"},
|
Drop{"generateName"},
|
||||||
Move{From: "selfLink", To: "resourcePath"},
|
//Move{From: "selfLink", To: "resourcePath"},
|
||||||
|
Drop{"selfLink"},
|
||||||
|
//Drop{"ownerReferences"},
|
||||||
Move{From: "uid", To: "uuid"},
|
Move{From: "uid", To: "uuid"},
|
||||||
Drop{"resourceVersion"},
|
Drop{"resourceVersion"},
|
||||||
Drop{"generation"},
|
Drop{"generation"},
|
||||||
|
@ -25,7 +25,7 @@ func (r ReadOnly) ModifySchema(schema *types.Schema, schemas *types.Schemas) err
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := validateField(r.Field, schema); err != nil {
|
if err := ValidateField(r.Field, schema); err != nil {
|
||||||
if r.Optional {
|
if r.Optional {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -10,40 +10,59 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type SetValue struct {
|
type SetValue struct {
|
||||||
From, To string
|
Field, To string
|
||||||
Value interface{}
|
Value interface{}
|
||||||
IfEq interface{}
|
IfEq interface{}
|
||||||
|
IgnoreDefinition bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s SetValue) FromInternal(data map[string]interface{}) {
|
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 {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if v == s.IfEq {
|
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{}) {
|
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 {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if v == s.Value {
|
if s.IfEq == nil {
|
||||||
values.PutValue(data, s.IfEq, strings.Split(s.From, "/")...)
|
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 {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if !ok {
|
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
|
return nil
|
||||||
|
@ -47,7 +47,7 @@ func (s SliceToMap) ToInternal(data map[string]interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s SliceToMap) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
|
func (s SliceToMap) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
|
||||||
err := validateField(s.Field, schema)
|
err := ValidateField(s.Field, schema)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
36
types/mapper/untyped_move.go
Normal file
36
types/mapper/untyped_move.go
Normal file
@ -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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user