Skip to content

Commit

Permalink
Merge pull request #6 from ibuildthecloud/work
Browse files Browse the repository at this point in the history
 Enhance Move and add SetValue
  • Loading branch information
ibuildthecloud authored Nov 15, 2017
2 parents 80024df + 0cf22cc commit 3ba704a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 18 deletions.
30 changes: 13 additions & 17 deletions types/mapping/mapper/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
)

type Move struct {
From, To string
From, To string
DestDefined bool
NoDeleteFromField bool
}

func (m Move) FromInternal(data map[string]interface{}) {
Expand All @@ -26,37 +28,31 @@ func (m Move) ToInternal(data map[string]interface{}) {
}

func (m Move) ModifySchema(s *types.Schema, schemas *types.Schemas) error {
internalSchema, err := getInternal(s)
if err != nil {
return err
}

_, _, fromInternalField, ok, err := getField(internalSchema, schemas, m.From)
fromSchema, _, fromField, ok, err := getField(s, schemas, m.From)
if err != nil {
return err
}
if !ok {
return fmt.Errorf("missing field %s on internal schema %s", m.From, internalSchema.ID)
}

fromSchema, _, _, _, err := getField(s, schemas, m.From)
if err != nil {
return err
return fmt.Errorf("failed to find field %s on schema %s", m.From, s.ID)
}

toSchema, toFieldName, toField, ok, err := getField(s, schemas, m.To)
if err != nil {
return err
}
_, ok = toSchema.ResourceFields[toFieldName]
if ok && !strings.Contains(m.To, "/") {
if ok && !strings.Contains(m.To, "/") && !m.DestDefined {
return fmt.Errorf("field %s already exists on schema %s", m.To, s.ID)
}

delete(fromSchema.ResourceFields, m.From)
if !m.NoDeleteFromField {
delete(fromSchema.ResourceFields, m.From)
}

toField.CodeName = convert.Capitalize(toFieldName)
toSchema.ResourceFields[toFieldName] = fromInternalField
if !m.DestDefined {
toField.CodeName = convert.Capitalize(toFieldName)
toSchema.ResourceFields[toFieldName] = fromField
}

return nil
}
Expand Down
49 changes: 49 additions & 0 deletions types/mapping/mapper/set_value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package mapper

import (
"fmt"

"strings"

"github.com/rancher/norman/types"
)

type SetValue struct {
From, To string
Value interface{}
IfEq interface{}
}

func (s SetValue) FromInternal(data map[string]interface{}) {
v, ok := GetValue(data, strings.Split(s.From, "/")...)
if !ok {
return
}

if v == s.IfEq {
PutValue(data, s.Value, strings.Split(s.To, "/")...)
}
}

func (s SetValue) ToInternal(data map[string]interface{}) {
v, ok := GetValue(data, strings.Split(s.To, "/")...)
if !ok {
return
}

if v == s.Value {
PutValue(data, s.IfEq, strings.Split(s.From, "/")...)
}
}

func (s SetValue) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
_, _, _, ok, err := getField(schema, schemas, s.To)
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 nil
}
2 changes: 1 addition & 1 deletion types/server_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (r *RawResource) MarshalJSON() ([]byte, error) {
if r.ActionLinks {
data["actionLinks"] = r.Actions
} else {
data["action"] = r.Actions
data["actions"] = r.Actions
}
return json.Marshal(data)
}
Expand Down

0 comments on commit 3ba704a

Please sign in to comment.