mirror of
https://github.com/rancher/norman.git
synced 2025-09-17 23:59:36 +00:00
Change ToInternal to return error
This commit is contained in:
@@ -13,7 +13,8 @@ type Access struct {
|
||||
func (e Access) FromInternal(data map[string]interface{}) {
|
||||
}
|
||||
|
||||
func (e Access) ToInternal(data map[string]interface{}) {
|
||||
func (e Access) ToInternal(data map[string]interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e Access) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
|
||||
|
@@ -36,7 +36,7 @@ func (e AnnotationField) FromInternal(data map[string]interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
func (e AnnotationField) ToInternal(data map[string]interface{}) {
|
||||
func (e AnnotationField) ToInternal(data map[string]interface{}) error {
|
||||
v, ok := data[e.Field]
|
||||
if ok {
|
||||
if e.Object || e.List {
|
||||
@@ -47,6 +47,7 @@ func (e AnnotationField) ToInternal(data map[string]interface{}) {
|
||||
values.PutValue(data, convert.ToString(v), "annotations", "field.cattle.io/"+e.Field)
|
||||
}
|
||||
values.RemoveValue(data, e.Field)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e AnnotationField) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
|
||||
|
35
types/mapper/apigroup.go
Normal file
35
types/mapper/apigroup.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package mapper
|
||||
|
||||
import (
|
||||
"github.com/rancher/norman/types"
|
||||
"github.com/rancher/norman/types/convert"
|
||||
)
|
||||
|
||||
type APIGroup struct {
|
||||
apiVersion string
|
||||
kind string
|
||||
}
|
||||
|
||||
func (a *APIGroup) FromInternal(data map[string]interface{}) {
|
||||
}
|
||||
|
||||
func (a *APIGroup) ToInternal(data map[string]interface{}) error {
|
||||
_, ok := data["apiVersion"]
|
||||
if !ok && data != nil {
|
||||
data["apiVersion"] = a.apiVersion
|
||||
}
|
||||
|
||||
_, ok = data["kind"]
|
||||
if !ok && data != nil {
|
||||
data["kind"] = a.kind
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *APIGroup) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
|
||||
a.apiVersion = schema.Version.Group + "/" + schema.Version.Version
|
||||
a.kind = convert.Capitalize(schema.ID)
|
||||
|
||||
return nil
|
||||
}
|
@@ -31,16 +31,18 @@ func (m Base64) FromInternal(data map[string]interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
func (m Base64) ToInternal(data map[string]interface{}) {
|
||||
func (m Base64) ToInternal(data map[string]interface{}) error {
|
||||
if v, ok := values.RemoveValue(data, strings.Split(m.Field, m.getSep())...); ok {
|
||||
str := convert.ToString(v)
|
||||
if str == "" {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
|
||||
newData := base64.StdEncoding.EncodeToString([]byte(str))
|
||||
values.PutValue(data, newData, strings.Split(m.Field, m.getSep())...)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m Base64) ModifySchema(s *types.Schema, schemas *types.Schemas) error {
|
||||
|
@@ -20,10 +20,12 @@ func (b *BatchMove) FromInternal(data map[string]interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
func (b *BatchMove) ToInternal(data map[string]interface{}) {
|
||||
func (b *BatchMove) ToInternal(data map[string]interface{}) error {
|
||||
errors := types.Errors{}
|
||||
for i := len(b.moves) - 1; i >= 0; i-- {
|
||||
b.moves[i].ToInternal(data)
|
||||
errors.Add(b.moves[i].ToInternal(data))
|
||||
}
|
||||
return errors.Err()
|
||||
}
|
||||
|
||||
func (b *BatchMove) ModifySchema(s *types.Schema, schemas *types.Schemas) error {
|
||||
|
@@ -12,7 +12,8 @@ type ChangeType struct {
|
||||
func (c ChangeType) FromInternal(data map[string]interface{}) {
|
||||
}
|
||||
|
||||
func (c ChangeType) ToInternal(data map[string]interface{}) {
|
||||
func (c ChangeType) ToInternal(data map[string]interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c ChangeType) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
|
||||
|
@@ -16,10 +16,11 @@ func (m Condition) FromInternal(data map[string]interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
func (m Condition) ToInternal(data map[string]interface{}) {
|
||||
func (m Condition) ToInternal(data map[string]interface{}) error {
|
||||
if data[m.Field] == m.Value {
|
||||
m.Mapper.ToInternal(data)
|
||||
return m.Mapper.ToInternal(data)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m Condition) ModifySchema(s *types.Schema, schemas *types.Schemas) error {
|
||||
|
@@ -20,15 +20,17 @@ func (c Copy) FromInternal(data map[string]interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
func (c Copy) ToInternal(data map[string]interface{}) {
|
||||
func (c Copy) ToInternal(data map[string]interface{}) error {
|
||||
if data == nil {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
t, tok := data[c.To]
|
||||
_, fok := data[c.From]
|
||||
if tok && !fok {
|
||||
data[c.From] = t
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c Copy) ModifySchema(s *types.Schema, schemas *types.Schemas) error {
|
||||
|
@@ -20,8 +20,8 @@ func (d DisplayName) FromInternal(data map[string]interface{}) {
|
||||
displayNameMappers.FromInternal(data)
|
||||
}
|
||||
|
||||
func (d DisplayName) ToInternal(data map[string]interface{}) {
|
||||
displayNameMappers.ToInternal(data)
|
||||
func (d DisplayName) ToInternal(data map[string]interface{}) error {
|
||||
return displayNameMappers.ToInternal(data)
|
||||
}
|
||||
|
||||
func (d DisplayName) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
|
||||
|
@@ -15,7 +15,8 @@ func (d Drop) FromInternal(data map[string]interface{}) {
|
||||
delete(data, d.Field)
|
||||
}
|
||||
|
||||
func (d Drop) ToInternal(data map[string]interface{}) {
|
||||
func (d Drop) ToInternal(data map[string]interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d Drop) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
|
||||
|
@@ -26,9 +26,9 @@ func (e *Embed) FromInternal(data map[string]interface{}) {
|
||||
delete(data, e.Field)
|
||||
}
|
||||
|
||||
func (e *Embed) ToInternal(data map[string]interface{}) {
|
||||
func (e *Embed) ToInternal(data map[string]interface{}) error {
|
||||
if data == nil {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
|
||||
sub := map[string]interface{}{}
|
||||
@@ -43,9 +43,10 @@ func (e *Embed) ToInternal(data map[string]interface{}) {
|
||||
if e.EmptyValueOk {
|
||||
data[e.Field] = nil
|
||||
}
|
||||
return
|
||||
return nil
|
||||
}
|
||||
data[e.Field] = sub
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *Embed) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
|
||||
|
@@ -12,7 +12,8 @@ type Enum struct {
|
||||
func (e Enum) FromInternal(data map[string]interface{}) {
|
||||
}
|
||||
|
||||
func (e Enum) ToInternal(data map[string]interface{}) {
|
||||
func (e Enum) ToInternal(data map[string]interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e Enum) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
|
||||
|
@@ -27,12 +27,13 @@ func (m JSONEncode) FromInternal(data map[string]interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
func (m JSONEncode) ToInternal(data map[string]interface{}) {
|
||||
func (m JSONEncode) ToInternal(data map[string]interface{}) error {
|
||||
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())...)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m JSONEncode) getSep() string {
|
||||
|
@@ -16,11 +16,12 @@ func (e LabelField) FromInternal(data map[string]interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
func (e LabelField) ToInternal(data map[string]interface{}) {
|
||||
func (e LabelField) ToInternal(data map[string]interface{}) error {
|
||||
v, ok := data[e.Field]
|
||||
if ok {
|
||||
values.PutValue(data, v, "labels", "field.cattle.io/"+e.Field)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e LabelField) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
|
||||
|
@@ -23,10 +23,11 @@ func (m Move) FromInternal(data map[string]interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
func (m Move) ToInternal(data map[string]interface{}) {
|
||||
func (m Move) ToInternal(data map[string]interface{}) error {
|
||||
if v, ok := values.RemoveValue(data, strings.Split(m.To, "/")...); ok {
|
||||
values.PutValue(data, v, strings.Split(m.From, "/")...)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m Move) ModifySchema(s *types.Schema, schemas *types.Schemas) error {
|
||||
|
@@ -11,6 +11,7 @@ type Object struct {
|
||||
func NewObject(mappers ...types.Mapper) Object {
|
||||
return Object{
|
||||
Mappers: append([]types.Mapper{
|
||||
&APIGroup{},
|
||||
&Embed{Field: "metadata"},
|
||||
&Embed{Field: "spec", Optional: true},
|
||||
&ReadOnly{Field: "status", Optional: true, SubFields: true},
|
||||
|
@@ -13,7 +13,8 @@ type ReadOnly struct {
|
||||
func (r ReadOnly) FromInternal(data map[string]interface{}) {
|
||||
}
|
||||
|
||||
func (r ReadOnly) ToInternal(data map[string]interface{}) {
|
||||
func (r ReadOnly) ToInternal(data map[string]interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r ReadOnly) readOnly(field types.Field, schema *types.Schema, schemas *types.Schemas) types.Field {
|
||||
|
@@ -17,10 +17,11 @@ func (r *RenameReference) FromInternal(data map[string]interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
func (r *RenameReference) ToInternal(data map[string]interface{}) {
|
||||
func (r *RenameReference) ToInternal(data map[string]interface{}) error {
|
||||
if r.mapper != nil {
|
||||
r.mapper.ToInternal(data)
|
||||
return r.mapper.ToInternal(data)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *RenameReference) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
|
||||
|
@@ -11,7 +11,8 @@ type Required struct {
|
||||
func (e Required) FromInternal(data map[string]interface{}) {
|
||||
}
|
||||
|
||||
func (e Required) ToInternal(data map[string]interface{}) {
|
||||
func (e Required) ToInternal(data map[string]interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e Required) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
|
||||
|
@@ -17,10 +17,11 @@ func (s *Scope) FromInternal(data map[string]interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Scope) ToInternal(data map[string]interface{}) {
|
||||
func (s *Scope) ToInternal(data map[string]interface{}) error {
|
||||
if s.run {
|
||||
types.Mappers(s.Mappers).ToInternal(data)
|
||||
return types.Mappers(s.Mappers).ToInternal(data)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Scope) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
|
||||
|
@@ -39,10 +39,10 @@ func (s SetValue) getTo() string {
|
||||
return s.To
|
||||
}
|
||||
|
||||
func (s SetValue) ToInternal(data map[string]interface{}) {
|
||||
func (s SetValue) ToInternal(data map[string]interface{}) error {
|
||||
v, ok := values.GetValue(data, strings.Split(s.getTo(), "/")...)
|
||||
if !ok {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
|
||||
if s.IfEq == nil {
|
||||
@@ -50,6 +50,8 @@ func (s SetValue) ToInternal(data map[string]interface{}) {
|
||||
} else if v == s.Value {
|
||||
values.PutValue(data, s.IfEq, strings.Split(s.Field, "/")...)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s SetValue) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
|
||||
|
@@ -26,7 +26,8 @@ func (s SliceMerge) FromInternal(data map[string]interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s SliceMerge) ToInternal(data map[string]interface{}) {
|
||||
func (s SliceMerge) ToInternal(data map[string]interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s SliceMerge) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
|
||||
|
@@ -29,7 +29,7 @@ func (s SliceToMap) FromInternal(data map[string]interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s SliceToMap) ToInternal(data map[string]interface{}) {
|
||||
func (s SliceToMap) ToInternal(data map[string]interface{}) error {
|
||||
datas, _ := data[s.Field].(map[string]interface{})
|
||||
var result []interface{}
|
||||
|
||||
@@ -46,6 +46,8 @@ func (s SliceToMap) ToInternal(data map[string]interface{}) {
|
||||
} else if datas != nil {
|
||||
data[s.Field] = result
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s SliceToMap) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
|
||||
|
@@ -23,7 +23,7 @@ func (u *UnionEmbed) FromInternal(data map[string]interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
func (u *UnionEmbed) ToInternal(data map[string]interface{}) {
|
||||
func (u *UnionEmbed) ToInternal(data map[string]interface{}) error {
|
||||
outer:
|
||||
for _, mapper := range u.Fields {
|
||||
if len(mapper.CheckFields) == 0 {
|
||||
@@ -38,9 +38,10 @@ outer:
|
||||
}
|
||||
|
||||
embed := u.embeds[mapper.FieldName]
|
||||
embed.ToInternal(data)
|
||||
return
|
||||
return embed.ToInternal(data)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *UnionEmbed) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
|
||||
|
@@ -18,10 +18,12 @@ func (m UntypedMove) FromInternal(data map[string]interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
func (m UntypedMove) ToInternal(data map[string]interface{}) {
|
||||
func (m UntypedMove) ToInternal(data map[string]interface{}) error {
|
||||
if v, ok := values.RemoveValue(data, strings.Split(m.To, m.getSep())...); ok {
|
||||
values.PutValue(data, v, strings.Split(m.From, m.getSep())...)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m UntypedMove) getSep() string {
|
||||
|
Reference in New Issue
Block a user