1
0
mirror of https://github.com/rancher/norman.git synced 2025-09-03 08:14:40 +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

@@ -95,7 +95,11 @@ func (j *JSONResponseWriter) convert(b *builder.Builder, context *types.APIConte
if schema == nil { if schema == nil {
return nil return nil
} }
data, err := b.Construct(schema, input, builder.List) op := builder.List
if context.Method == http.MethodPost {
op = builder.ListForCreate
}
data, err := b.Construct(schema, input, op)
if err != nil { if err != nil {
logrus.Errorf("Failed to construct object on output: %v", err) logrus.Errorf("Failed to construct object on output: %v", err)
return nil return nil

View File

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