Skip to content

Commit

Permalink
Add copy mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuildthecloud committed Apr 22, 2018
1 parent 0e1438b commit 0611d5f
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions types/mapper/copy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package mapper

import (
"fmt"

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

type Copy struct {
From, To string
}

func (c Copy) FromInternal(data map[string]interface{}) {
if data == nil {
return
}
v, ok := data[c.From]
if ok {
data[c.To] = v
}
}

func (c Copy) ToInternal(data map[string]interface{}) {
if data == nil {
return
}
t, tok := data[c.To]
_, fok := data[c.From]
if tok && !fok {
data[c.From] = t
}
}

func (c Copy) ModifySchema(s *types.Schema, schemas *types.Schemas) error {
f, ok := s.ResourceFields[c.From]
if !ok {
return fmt.Errorf("field %s missing on schema %s", c.From, s.ID)
}

s.ResourceFields[c.To] = f
return nil
}

0 comments on commit 0611d5f

Please sign in to comment.