mirror of
https://github.com/rancher/norman.git
synced 2025-09-11 03:59:26 +00:00
Add norman field attribute "pointer"
Add a new field attribute "pointer" to indicate that the generated
client code for the field must be a pointer. This allows clients to
differentiate between sending nil/leaving the value unset and sending an
empty map or slice.
This change also removes the `nullablestring` norman type introduced in
30f8d18
since schemas that need a pointer to a string can now use this
field attribute. There are no libraries currently using this feature so
it should be safe to remove.
Example usage:
```
Labels map[string]string `json:"labels" norman:"pointer"`
```
Resulting API schema:
```
"labels": {
"create": true,
"nullable": true,
"pointer": true,
"type": "map[string]",
"update": true
}
```
Generated client code:
```
Labels *map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"`
```
This commit is contained in:
@@ -35,17 +35,19 @@ type fieldInfo struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getGoType(field types.Field, schema *types.Schema, schemas *types.Schemas) string {
|
func getGoType(field types.Field, schema *types.Schema, schemas *types.Schemas) string {
|
||||||
return getTypeString(field.Nullable, field.Type, schema, schemas)
|
return getTypeString(field.Nullable, field.Type, field.Pointer, schema, schemas)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getTypeString(nullable bool, typeName string, schema *types.Schema, schemas *types.Schemas) string {
|
func getTypeString(nullable bool, typeName string, pointer bool, schema *types.Schema, schemas *types.Schemas) string {
|
||||||
switch {
|
switch {
|
||||||
|
case pointer:
|
||||||
|
return "*" + getTypeString(nullable, typeName, false, schema, schemas)
|
||||||
case strings.HasPrefix(typeName, "reference["):
|
case strings.HasPrefix(typeName, "reference["):
|
||||||
return "string"
|
return "string"
|
||||||
case strings.HasPrefix(typeName, "map["):
|
case strings.HasPrefix(typeName, "map["):
|
||||||
return "map[string]" + getTypeString(false, typeName[len("map["):len(typeName)-1], schema, schemas)
|
return "map[string]" + getTypeString(false, typeName[len("map["):len(typeName)-1], false, schema, schemas)
|
||||||
case strings.HasPrefix(typeName, "array["):
|
case strings.HasPrefix(typeName, "array["):
|
||||||
return "[]" + getTypeString(false, typeName[len("array["):len(typeName)-1], schema, schemas)
|
return "[]" + getTypeString(false, typeName[len("array["):len(typeName)-1], false, schema, schemas)
|
||||||
}
|
}
|
||||||
|
|
||||||
name := ""
|
name := ""
|
||||||
@@ -81,8 +83,6 @@ func getTypeString(nullable bool, typeName string, schema *types.Schema, schemas
|
|||||||
return "string"
|
return "string"
|
||||||
case "hostname":
|
case "hostname":
|
||||||
return "string"
|
return "string"
|
||||||
case "nullablestring":
|
|
||||||
return "*string"
|
|
||||||
default:
|
default:
|
||||||
if schema != nil && schemas != nil {
|
if schema != nil && schemas != nil {
|
||||||
otherSchema := schemas.Schema(&schema.Version, typeName)
|
otherSchema := schemas.Schema(&schema.Version, typeName)
|
||||||
|
@@ -382,8 +382,6 @@ func ConvertSimple(fieldType string, value interface{}, op Operation) (interface
|
|||||||
return convert.ToString(value), nil
|
return convert.ToString(value), nil
|
||||||
case "reference":
|
case "reference":
|
||||||
return convert.ToString(value), nil
|
return convert.ToString(value), nil
|
||||||
case "nullablestring":
|
|
||||||
return convert.ToString(value), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, ErrComplexType
|
return nil, ErrComplexType
|
||||||
|
@@ -393,6 +393,8 @@ func applyTag(structField *reflect.StructField, field *Field) error {
|
|||||||
field.ValidChars = value
|
field.ValidChars = value
|
||||||
case "invalidChars":
|
case "invalidChars":
|
||||||
field.InvalidChars = value
|
field.InvalidChars = value
|
||||||
|
case "pointer":
|
||||||
|
field.Pointer = true
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("invalid tag %s on field %s", key, structField.Name)
|
return fmt.Errorf("invalid tag %s on field %s", key, structField.Name)
|
||||||
}
|
}
|
||||||
|
@@ -150,6 +150,7 @@ type Field struct {
|
|||||||
Description string `json:"description,omitempty"`
|
Description string `json:"description,omitempty"`
|
||||||
CodeName string `json:"-"`
|
CodeName string `json:"-"`
|
||||||
DynamicField bool `json:"dynamicField,omitempty"`
|
DynamicField bool `json:"dynamicField,omitempty"`
|
||||||
|
Pointer bool `json:"pointer,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Action struct {
|
type Action struct {
|
||||||
|
Reference in New Issue
Block a user