mirror of
https://github.com/rancher/types.git
synced 2025-09-01 13:18:20 +00:00
Move mappers
This commit is contained in:
52
mapper/resource.go
Normal file
52
mapper/resource.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package mapper
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/rancher/norman/types"
|
||||
m "github.com/rancher/norman/types/mapper"
|
||||
)
|
||||
|
||||
type PivotMapper struct {
|
||||
Plural bool
|
||||
}
|
||||
|
||||
func (r PivotMapper) FromInternal(data map[string]interface{}) {
|
||||
for key, value := range data {
|
||||
mapValue, ok := value.(map[string]interface{})
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
for subKey, subValue := range mapValue {
|
||||
if r.Plural {
|
||||
m.PutValue(data, subValue, subKey, strings.TrimSuffix(key, "s"))
|
||||
} else {
|
||||
m.PutValue(data, subValue, subKey, key)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
delete(data, key)
|
||||
}
|
||||
}
|
||||
|
||||
func (r PivotMapper) ToInternal(data map[string]interface{}) {
|
||||
for key, value := range data {
|
||||
mapValue, ok := value.(map[string]interface{})
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
for subKey, subValue := range mapValue {
|
||||
if r.Plural {
|
||||
m.PutValue(data, subValue, subKey, key+"s")
|
||||
} else {
|
||||
m.PutValue(data, subValue, subKey, key)
|
||||
}
|
||||
}
|
||||
delete(data, key)
|
||||
}
|
||||
}
|
||||
|
||||
func (r PivotMapper) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user