1
0
mirror of https://github.com/rancher/norman.git synced 2025-09-01 23:36:58 +00:00

write writeOnly fields on create

This commit is contained in:
Darren Shepherd
2018-01-19 21:28:40 -07:00
parent 76d8256085
commit 8982c1cc59
2 changed files with 20 additions and 9 deletions

View File

@@ -11,14 +11,19 @@ import (
)
var (
Create = Operation("create")
Update = Operation("update")
Action = Operation("action")
List = Operation("list")
Create = Operation("create")
Update = Operation("update")
Action = Operation("action")
List = Operation("list")
ListForCreate = Operation("listcreate")
)
type Operation string
func (o Operation) IsList() bool {
return strings.HasPrefix(string(o), "list")
}
type Builder struct {
apiContext *types.APIContext
Version *types.APIVersion
@@ -66,7 +71,7 @@ func (b *Builder) copyInputs(schema *types.Schema, input map[string]interface{},
}
if value != nil || wasNull {
if op != List {
if !op.IsList() {
if slice, ok := value.([]interface{}); ok {
for _, sliceValue := range slice {
if sliceValue == nil {
@@ -84,7 +89,7 @@ func (b *Builder) copyInputs(schema *types.Schema, input map[string]interface{},
}
result[fieldName] = value
if op == List && field.Type == "date" && value != "" {
if op.IsList() && field.Type == "date" && value != "" {
ts, err := convert.ToTimestamp(value)
if err == nil {
result[fieldName+"TS"] = ts
@@ -93,7 +98,7 @@ func (b *Builder) copyInputs(schema *types.Schema, input map[string]interface{},
}
}
if op == List {
if op.IsList() {
if !convert.IsEmpty(input["type"]) {
result["type"] = input["type"]
}
@@ -129,7 +134,7 @@ func (b *Builder) checkDefaultAndRequired(schema *types.Schema, input map[string
}
}
if op == List && fieldMatchesOp(field, List) && definition.IsReferenceType(field.Type) && !hasKey {
if op.IsList() && fieldMatchesOp(field, List) && definition.IsReferenceType(field.Type) && !hasKey {
result[fieldName] = nil
}
}
@@ -349,6 +354,8 @@ func fieldMatchesOp(field types.Field, op Operation) bool {
return field.Update
case List:
return !field.WriteOnly
case ListForCreate:
return true
default:
return false
}