diff --git a/go.mod b/go.mod index b48ae7e..474c50d 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.19 require ( github.com/getkin/kin-openapi v0.89.0 github.com/mitchellh/go-homedir v1.1.0 + github.com/mitchellh/mapstructure v1.5.0 github.com/pkg/errors v0.9.1 github.com/rs/zerolog v1.32.0 github.com/sirupsen/logrus v1.9.3 @@ -12,6 +13,7 @@ require ( github.com/spf13/pflag v1.0.5 github.com/spf13/viper v1.18.1 github.com/stretchr/testify v1.8.4 + golang.org/x/exp v0.0.0-20230905200255-921286631fa9 gopkg.in/yaml.v2 v2.4.0 gopkg.in/yaml.v3 v3.0.1 ) @@ -29,7 +31,6 @@ require ( github.com/mailru/easyjson v0.7.7 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.19 // indirect - github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/pelletier/go-toml/v2 v2.1.0 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/sagikazarmark/locafero v0.4.0 // indirect @@ -40,7 +41,6 @@ require ( github.com/subosito/gotenv v1.6.0 // indirect go.uber.org/atomic v1.9.0 // indirect go.uber.org/multierr v1.9.0 // indirect - golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect golang.org/x/sys v0.15.0 // indirect golang.org/x/text v0.14.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect diff --git a/pkg/generators/models/generate.go b/pkg/generators/models/generate.go index f52f053..ae65f03 100644 --- a/pkg/generators/models/generate.go +++ b/pkg/generators/models/generate.go @@ -11,10 +11,11 @@ import ( "path/filepath" "strings" - tpl "github.com/contiamo/openapi-generator-go/v2/pkg/generators/templates" "github.com/getkin/kin-openapi/openapi3" "github.com/pkg/errors" "github.com/rs/zerolog" + + tpl "github.com/contiamo/openapi-generator-go/v2/pkg/generators/templates" ) // DefaultPackageName used in the models source code @@ -284,7 +285,7 @@ func (g generator) writeModelToFile(ctx context.Context, model *Model, dst strin log.Debug().Msg("Formatting the rendered code...") content, err := format.Source(buf.Bytes()) if err != nil { - return errors.Wrap(err, "cannot format model code") + return errors.Wrap(err, "cannot format model code: "+buf.String()) } log.Debug().Msg("Code has been formatted.") diff --git a/pkg/generators/models/go_type_from_ref.go b/pkg/generators/models/go_type_from_ref.go index 516ca89..225db6c 100644 --- a/pkg/generators/models/go_type_from_ref.go +++ b/pkg/generators/models/go_type_from_ref.go @@ -86,12 +86,6 @@ func goTypeFromSpec(schemaRef *openapi3.SchemaRef) string { propertyType = "interface{}" } - if schema.Nullable && - !strings.HasPrefix(propertyType, "[]") && - !strings.HasPrefix(propertyType, "map[") { - - propertyType = "*" + propertyType - } return propertyType } diff --git a/pkg/generators/models/models.go b/pkg/generators/models/models.go index 11a5eec..3a3e1ab 100644 --- a/pkg/generators/models/models.go +++ b/pkg/generators/models/models.go @@ -6,12 +6,15 @@ import ( "fmt" "io" "math" + "reflect" "regexp" "sort" "text/template" "github.com/getkin/kin-openapi/openapi3" "github.com/pkg/errors" + "golang.org/x/exp/maps" + "golang.org/x/exp/slices" tpl "github.com/contiamo/openapi-generator-go/v2/pkg/generators/templates" ) @@ -25,8 +28,8 @@ var ( // Exported types may have a Validate() function // Exported types with a leading pointer may have a Validate() function // Slices may contain data that is validatable (they are also never pointers) - // Maps may contain data that is validateable (they are also never pointers ) - validatedTypesRegExp = regexp.MustCompile(`^(\*?[A-Z])|(\[\])|(map\[)`) + // Maps may contain data that is validatable (they are also never pointers ) + validatedTypesRegExp = regexp.MustCompile(`^(\*?[A-Z])|(\[])|(map\[)`) ) // something is just something that does not have to be allocated @@ -36,18 +39,18 @@ type something struct{} // Used for marking already visited types when recursively resolve `allOf` definitions and merging types. type passedSchemas map[*openapi3.SchemaRef]something -// ModelKind is a kind of a Go model to render -type ModelKind string +// TemplateKind is a kind of template to render +type TemplateKind string const ( // Struct is a regular Go struct - Struct ModelKind = "struct" + Struct TemplateKind = "struct" // Enum is a Go enum definition - Enum ModelKind = "enum" + Enum TemplateKind = "enum" // Value is a value type - Value ModelKind = "value" + Value TemplateKind = "value" // OneOf is a oneof value - OneOf ModelKind = "oneof" + OneOf TemplateKind = "oneof" ) // Model is a template model for rendering Go code for a given API schema @@ -62,8 +65,8 @@ type Model struct { Description string // Imports is a list of imports that will be present in the Go file. Key is the package and value is the alias Imports map[string]string - // Kind is a kind of generated model (e.g. `struct` or `enum`) - Kind ModelKind + // TemplateKind is a kind of generated model (e.g. `struct` or `enum`) + TemplateKind TemplateKind // Properties is a list of type's property descriptors Properties []PropSpec // ConvertSpecs contains a list of convert functions for this model @@ -88,6 +91,8 @@ type Model struct { type ConvertSpec struct { // TargetGoType is the target type of the conversion TargetGoType string + // IsPtr is true when the target type is a pointer + IsPtr bool } type ValidationSpec struct { @@ -95,13 +100,15 @@ type ValidationSpec struct { HasPattern bool Pattern string PatternErrorMsg string - IsNullable bool NeedsValidation bool - IsRequiredInValidation bool HasMin, HasMax bool Min, Max float64 HasMinLength, HasMaxLength bool MinLength, MaxLength uint64 + HasMinItems, HasMaxItems bool + MinItems, MaxItems uint64 + HasMinProps, HasMaxProps bool + MinProps, MaxProps uint64 HasFormat bool IsDate bool IsDateTime bool @@ -115,6 +122,11 @@ type ValidationSpec struct { IsIP bool IsIPv4 bool IsIPv6 bool + IsEnumWithNil bool + IsEnumWithZero bool + + // Derived validations that should be added to the property + DerivedValidations []string } // PropSpec is a Go property descriptor @@ -136,10 +148,26 @@ type PropSpec struct { Value string // IsRequired is true when the property is a required value and should not be omitted on marshaling IsRequired bool + // IsPtr is true when the property is a pointer. This is derived from the nullable property in the schema + IsPtr bool // IsEnum is true when the property is a enum item IsEnum bool // IsOneOf is true when the property is a `oneof` schema IsOneOf bool + // IsArray is true when the property is an array + IsArray bool + // IsMap is true when the property is a map + IsMap bool + // IsStruct is true when the property is a struct + IsStruct bool + // IsString is true when the property is a string + IsString bool + // IsNumber is true when the property is a number + IsNumber bool + // IsInteger is true when the property is an integer + IsInteger bool + // IsRef is true when the property is a Ref to a schema + IsRef bool } type Discriminator struct { @@ -159,12 +187,12 @@ func NewModelFromRef(ref *openapi3.SchemaRef) (model *Model, err error) { switch { case len(ref.Value.Enum) > 0: - model.Kind = Enum + model.TemplateKind = Enum model.GoType = goTypeFromSpec(ref) model.Properties = enumPropsFromRef(ref, model) case ref.Value.Type == "object" || len(ref.Value.Properties) > 0: - model.Kind = Struct + model.TemplateKind = Struct model.Properties, model.Imports, err = structPropsFromRef(ref) if len(model.Properties) == 0 { model.GoType = "map[string]interface{}" @@ -175,13 +203,13 @@ func NewModelFromRef(ref *openapi3.SchemaRef) (model *Model, err error) { model.AdditionalPropertiesGoType = goTypeFromSpec(ref.Value.AdditionalProperties) } case len(ref.Value.OneOf) > 0: - model.Kind = OneOf + model.TemplateKind = OneOf model.configureOneOf(ref) default: - model.Kind = Value + model.TemplateKind = Value model.GoType = goTypeFromSpec(ref) model.Imports = make(map[string]string) - err = fillValidationSpec(ref, &model.ValidationSpec, model.GoType, false, model.Imports) + err = fillValidationSpec(ref, &model.ValidationSpec, model.GoType, model.Imports) } if err != nil { return nil, err @@ -193,46 +221,46 @@ func NewModelFromRef(ref *openapi3.SchemaRef) (model *Model, err error) { // NewModelFromParameters returns a model built from operation parameters func NewModelFromParameters(params openapi3.Parameters) (model *Model, err error) { model = &Model{ - Kind: Struct, - Imports: make(map[string]string), + TemplateKind: Struct, + Imports: make(map[string]string), } + // convert to SchemaRef to reuse struct props logic + ref := &openapi3.SchemaRef{ + Value: &openapi3.Schema{ + Type: "object", + Required: make([]string, 0), + Properties: make(map[string]*openapi3.SchemaRef), + }, + } for _, param := range params { - spec := PropSpec{ - Name: tpl.ToPascalCase(param.Value.Name), - Description: param.Value.Description, - GoType: goTypeFromSpec(param.Value.Schema), - IsRequired: param.Value.Required, - } - - if spec.GoType == "time.Time" || spec.GoType == "*time.Time" { - model.Imports["time"] = "" - } - err := fillValidationSpecForProperty(param.Value.Schema, &spec, model.Imports) - if err != nil { - return nil, fmt.Errorf("invalid parameter %q:%w", param.Value.Name, err) - } - - omit := "" - if !spec.IsRequired { - omit += ",omitempty" + if param.Value.Schema != nil { + schema := param.Value.Schema.Value + if param.Value.Description != "" { + // use a copy to modify the description without affecting the source schema + schemaCopy := *param.Value.Schema.Value + schemaCopy.Description = param.Value.Description + schema = &schemaCopy + } + ref.Value.Properties[param.Value.Name] = &openapi3.SchemaRef{ + Ref: param.Value.Schema.Ref, + Value: schema, + } + if param.Value.Required { + ref.Value.Required = append(ref.Value.Required, param.Value.Name) + } } - - jsonTags := fmt.Sprintf(" `json:\"%s%s\"", param.Value.Name, omit) - jsonTags += fmt.Sprintf(" mapstructure:\"%s%s\"", param.Value.Name, omit) - jsonTags += "`" - - spec.JSONTags = jsonTags - model.Properties = append(model.Properties, spec) } - return model, nil + model.Properties, model.Imports, err = structPropsFromRef(ref) + return model, err } func (m *Model) configureOneOf(ref *openapi3.SchemaRef) { for _, oneOf := range ref.Value.OneOf { m.ConvertSpecs = append(m.ConvertSpecs, ConvertSpec{ TargetGoType: goTypeFromSpec(oneOf), + IsPtr: oneOf.Value.Nullable && oneOf.Value.Type != "" && oneOf.Value.Type != "object" && oneOf.Value.Type != "array", }) } @@ -262,7 +290,7 @@ func (m *Model) configureOneOf(ref *openapi3.SchemaRef) { func (m *Model) Render(ctx context.Context, writer io.Writer) error { var tpl *template.Template - switch m.Kind { + switch m.TemplateKind { case Struct: tpl = modelTemplate case Enum: @@ -516,19 +544,13 @@ func deepMerge(left *openapi3.SchemaRef, right *openapi3.SchemaRef, passed passe } func dedup[T any](input []T) []T { - seen := make(map[string]bool) + seen := make(map[string]T) - n := 0 for _, x := range input { key := fmt.Sprintf("%v", x) - if !seen[key] { - seen[key] = true - input[n] = x - n++ - } + seen[key] = x } - input = input[:n] - return input + return maps.Values(seen) } // structPropsFromRef creates property descriptors for a Go struct from a schema @@ -574,15 +596,34 @@ func structPropsFromRef(ref *openapi3.SchemaRef) (specs []PropSpec, imports map[ } spec := PropSpec{ - Name: tpl.ToPascalCase(name), - PropertyName: name, - Description: prop.Value.Description, - GoType: goType, - IsRequired: isRequired, - IsEnum: len(prop.Value.Enum) > 0, - ValidationSpec: ValidationSpec{IsNullable: prop.Value.Nullable}, - IsOneOf: prop.Value.OneOf != nil && len(prop.Value.OneOf) > 0, + Name: tpl.ToPascalCase(name), + PropertyName: name, + Description: prop.Value.Description, + GoType: goType, + IsRequired: isRequired, + IsEnum: len(prop.Value.Enum) > 0, + IsArray: prop.Value.Type == "array", + IsMap: (prop.Value.Type == "" || prop.Value.Type == "object") && len(prop.Value.Properties) == 0, + IsStruct: (prop.Value.Type == "" || prop.Value.Type == "object") && len(prop.Value.Properties) > 0, + IsString: prop.Value.Type == "string", + IsNumber: prop.Value.Type == "number", + IsInteger: prop.Value.Type == "integer", + IsRef: prop.Ref != "", + ValidationSpec: ValidationSpec{ + IsEnumWithNil: len(prop.Value.Enum) > 0 && slices.ContainsFunc(prop.Value.Enum, func(i any) bool { + // Reflect considers the enum value nil to be invalid because it doesn't have a type for it + return !reflect.ValueOf(i).IsValid() + }), + IsEnumWithZero: len(prop.Value.Enum) > 0 && slices.ContainsFunc(prop.Value.Enum, func(i any) bool { + val := reflect.ValueOf(i) + return val.IsValid() && val.IsZero() + }), + }, + IsOneOf: prop.Value.OneOf != nil && len(prop.Value.OneOf) > 0, } + // Set/Update dependent properties + spec.IsPtr = prop.Value.Nullable && !(spec.IsMap || spec.IsArray) && (!spec.IsEnum || spec.IsEnumWithNil) + spec.IsString = spec.IsString && !spec.IsEnum if spec.GoType == "time.Time" || spec.GoType == "*time.Time" { imports["time"] = "" @@ -599,77 +640,113 @@ func structPropsFromRef(ref *openapi3.SchemaRef) (specs []PropSpec, imports map[ spec.JSONTags = jsonTags - err := fillValidationSpecForProperty(prop, &spec, imports) + err := fillValidationSpec(prop, &spec.ValidationSpec, spec.GoType, imports) if err != nil { return nil, nil, fmt.Errorf("invalid %s property %q: %w", ExtensionPatternError, name, err) } + + resolveDerivedValidation(&spec, imports) specs = append(specs, spec) } return specs, imports, err } -// fillValidationSpecForProperty sets validation rules for the property descriptor out of format and -// bounds in the spec. -// Returns a list of unique imports in order to implement the validation. -func fillValidationSpecForProperty(ref *openapi3.SchemaRef, spec *PropSpec, imports map[string]string) (err error) { - // Only add validation fields that are part of the property and not the referenced schema. - if ref.Ref != "" { - valSpec := ValidationSpec{} - err = fillValidationSpec(ref, &valSpec, spec.GoType, spec.IsRequired, make(map[string]string)) - if err != nil { - return err +// resolveDerivedValidation resolves the extra validation rules that are derived from the required, nullable and other +// properties that affect the validation rules. Example: OZZO min length validation for strings consider the empty string +// valid, so we need to add the OZZO Required validation to catch that. +func resolveDerivedValidation(spec *PropSpec, imports map[string]string) { + for validation, specs := range DerivedRulesByValidationType { + for _, rule := range specs { + if rule.IsStruct == spec.IsStruct && + rule.IsEnum == spec.IsEnum && + rule.IsString == spec.IsString && + rule.IsMap == spec.IsMap && + rule.IsArray == spec.IsArray && + rule.IsNumber == spec.IsNumber && + rule.IsInteger == spec.IsInteger && + rule.IsPtr == spec.IsPtr && + (rule.IsEnumWithNull == nil || *rule.IsEnumWithNull == spec.IsEnumWithNil) && + (rule.IsEnumWithZero == nil || *rule.IsEnumWithZero == spec.IsEnumWithZero) && + (rule.IsRequired == nil || *rule.IsRequired == spec.IsRequired) && + (rule.HasFormat == nil || *rule.HasFormat == spec.HasFormat) && + (rule.HasPattern == nil || *rule.HasPattern == spec.HasPattern) && + (rule.HasMin == nil || *rule.HasMin == spec.HasMin) && + (rule.IsMinGreaterThanZero == nil || *rule.IsMinGreaterThanZero == (spec.Min > 0)) && + (rule.HasMax == nil || *rule.HasMax == spec.HasMax) && + (rule.IsMaxLessThanZero == nil || *rule.IsMaxLessThanZero == (spec.Max < 0)) && + (rule.HasMinLength == nil || *rule.HasMinLength == spec.HasMinLength) && + (rule.HasMaxLength == nil || *rule.HasMaxLength == spec.HasMaxLength) && + (rule.HasMinItems == nil || *rule.HasMinItems == spec.HasMinItems) && + (rule.HasMaxItems == nil || *rule.HasMaxItems == spec.HasMaxItems) && + (rule.HasMinProps == nil || *rule.HasMinProps == spec.HasMinProps) && + (rule.HasMaxProps == nil || *rule.HasMaxProps == spec.HasMaxProps) { + + /* // Enable for easier debugging of the rules + rowJSON, _ := json.Marshal(rule) + fmt.Printf("Adding derived validation %s to %s, matching rule: %s\n", validation, spec.Name, string(rowJSON)) + */ + imports["github.com/go-ozzo/ozzo-validation/v4"] = "validation" + spec.DerivedValidations = append(spec.DerivedValidations, "validation."+validation) + // We only add one validation type per property, so we break here + break + } } - // Enable to force validation on referenced types. - spec.NeedsValidation = valSpec.NeedsValidation - spec.IsRequiredInValidation = !valSpec.IsNullable && spec.IsRequired - return nil } - - return fillValidationSpec(ref, &spec.ValidationSpec, spec.GoType, spec.IsRequired, imports) } -func fillValidationSpec(ref *openapi3.SchemaRef, spec *ValidationSpec, goType string, isRequired bool, imports map[string]string) (err error) { +func fillValidationSpec(ref *openapi3.SchemaRef, spec *ValidationSpec, goType string, imports map[string]string) (err error) { if validatedTypesRegExp.MatchString(goType) { // enable recursive validation spec.NeedsValidation = true - spec.IsRequiredInValidation = !spec.IsNullable && isRequired } if ref.Value.Min != nil { spec.NeedsValidation = true spec.HasMin = true spec.Min = *ref.Value.Min - if spec.Min > 0 { - spec.IsRequiredInValidation = !spec.IsNullable && isRequired - } } if ref.Value.Max != nil { spec.NeedsValidation = true spec.HasMax = true spec.Max = *ref.Value.Max - if spec.Max > 0 { - spec.IsRequiredInValidation = !spec.IsNullable && isRequired - } } if ref.Value.MinLength > 0 { spec.NeedsValidation = true spec.HasMinLength = true spec.MinLength = ref.Value.MinLength - if spec.MinLength > 0 { - spec.IsRequiredInValidation = !spec.IsNullable && isRequired - } } if ref.Value.MaxLength != nil { spec.NeedsValidation = true spec.HasMaxLength = true spec.MaxLength = *ref.Value.MaxLength - if spec.MaxLength > 0 { - spec.IsRequiredInValidation = !spec.IsNullable && isRequired - } + } + + if ref.Value.MinItems > 0 { + spec.NeedsValidation = true + spec.HasMinItems = true + spec.MinItems = ref.Value.MinItems + } + + if ref.Value.MaxItems != nil { + spec.NeedsValidation = true + spec.HasMaxItems = true + spec.MaxItems = *ref.Value.MaxItems + } + + if ref.Value.MinProps > 0 { + spec.NeedsValidation = true + spec.HasMinProps = true + spec.MinProps = ref.Value.MinProps + } + + if ref.Value.MaxProps != nil { + spec.NeedsValidation = true + spec.HasMaxProps = true + spec.MaxProps = *ref.Value.MaxProps } if ref.Value.ExclusiveMin { @@ -680,10 +757,6 @@ func fillValidationSpec(ref *openapi3.SchemaRef, spec *ValidationSpec, goType st spec.Min -= math.SmallestNonzeroFloat64 } - if ref.Value.Format != "" { - spec.HasFormat = true - } - if ref.Value.Pattern != "" { spec.NeedsValidation = true spec.HasPattern = true @@ -702,77 +775,90 @@ func fillValidationSpec(ref *openapi3.SchemaRef, spec *ValidationSpec, goType st } } + if ref.Value.Format != "" { + spec.HasFormat = true + } switch ref.Value.Format { case "date": - spec.IsRequiredInValidation = !spec.IsNullable && isRequired spec.NeedsValidation = true spec.IsDate = true case "date-time": - spec.IsRequiredInValidation = !spec.IsNullable && isRequired spec.NeedsValidation = true spec.IsDateTime = true - imports["time"] = "" + if ref.Ref == "" { + imports["time"] = "" + } case "byte": - spec.IsRequiredInValidation = !spec.IsNullable && isRequired spec.NeedsValidation = true spec.IsBase64 = true - imports["github.com/go-ozzo/ozzo-validation/v4/is"] = "" + if ref.Ref == "" { + imports["github.com/go-ozzo/ozzo-validation/v4/is"] = "" + } case "email": - spec.IsRequiredInValidation = !spec.IsNullable && isRequired spec.NeedsValidation = true spec.IsEmail = true - imports["github.com/go-ozzo/ozzo-validation/v4/is"] = "" + if ref.Ref == "" { + imports["github.com/go-ozzo/ozzo-validation/v4/is"] = "" + } case "uuid": - spec.IsRequiredInValidation = !spec.IsNullable && isRequired spec.NeedsValidation = true spec.IsUUID = true - imports["github.com/go-ozzo/ozzo-validation/v4/is"] = "" + if ref.Ref == "" { + imports["github.com/go-ozzo/ozzo-validation/v4/is"] = "" + } case "url": - spec.IsRequiredInValidation = !spec.IsNullable && isRequired spec.NeedsValidation = true spec.IsURL = true - imports["github.com/go-ozzo/ozzo-validation/v4/is"] = "" + if ref.Ref == "" { + imports["github.com/go-ozzo/ozzo-validation/v4/is"] = "" + } case "uri": - spec.IsRequiredInValidation = !spec.IsNullable && isRequired spec.NeedsValidation = true spec.IsURI = true - imports["github.com/go-ozzo/ozzo-validation/v4/is"] = "" + if ref.Ref == "" { + imports["github.com/go-ozzo/ozzo-validation/v4/is"] = "" + } case "request-uri": - spec.IsRequiredInValidation = !spec.IsNullable && isRequired spec.NeedsValidation = true spec.IsRequestURI = true - imports["github.com/go-ozzo/ozzo-validation/v4/is"] = "" + if ref.Ref == "" { + imports["github.com/go-ozzo/ozzo-validation/v4/is"] = "" + } case "hostname": - spec.IsRequiredInValidation = !spec.IsNullable && isRequired spec.NeedsValidation = true spec.IsHostname = true - imports["github.com/go-ozzo/ozzo-validation/v4/is"] = "" + if ref.Ref == "" { + imports["github.com/go-ozzo/ozzo-validation/v4/is"] = "" + } case "ipv4": - spec.IsRequiredInValidation = !spec.IsNullable && isRequired spec.NeedsValidation = true spec.IsIPv4 = true - imports["github.com/go-ozzo/ozzo-validation/v4/is"] = "" + if ref.Ref == "" { + imports["github.com/go-ozzo/ozzo-validation/v4/is"] = "" + } case "ipv6": - spec.IsRequiredInValidation = !spec.IsNullable && isRequired spec.NeedsValidation = true spec.IsIPv6 = true - imports["github.com/go-ozzo/ozzo-validation/v4/is"] = "" + if ref.Ref == "" { + imports["github.com/go-ozzo/ozzo-validation/v4/is"] = "" + } case "ip": - spec.IsRequiredInValidation = !spec.IsNullable && isRequired spec.NeedsValidation = true spec.IsIP = true - imports["github.com/go-ozzo/ozzo-validation/v4/is"] = "" + if ref.Ref == "" { + imports["github.com/go-ozzo/ozzo-validation/v4/is"] = "" + } case "", "int32", "int64", "float", "double": break // do nothing @@ -788,6 +874,10 @@ func fillValidationSpec(ref *openapi3.SchemaRef, spec *ValidationSpec, goType st // enumPropsFromRef generates a list of enum property/item descriptors. func enumPropsFromRef(ref *openapi3.SchemaRef, model *Model) (specs []PropSpec) { for idx, val := range ref.Value.Enum { + // Ignore nil values as it is handled by the referencing struct + if val == nil { + continue + } valueVarName := tpl.ToPascalCase(fmt.Sprintf("%v", val)) if valueVarName == "" { valueVarName = fmt.Sprintf("Item%d", idx) diff --git a/pkg/generators/models/models_test.go b/pkg/generators/models/models_test.go index 79304b1..275eb65 100644 --- a/pkg/generators/models/models_test.go +++ b/pkg/generators/models/models_test.go @@ -176,6 +176,14 @@ var cases = []struct { name: "example of an enum with number values", directory: "testdata/cases/enum_with_number_values", }, + { + name: "example of nullable enum", + directory: "testdata/cases/enum_nullable", + }, + { + name: "example of nullable and required combinations", + directory: "testdata/cases/required_nullable_combinations", + }, } func TestModels(t *testing.T) { @@ -277,5 +285,5 @@ func equalFiles(t *testing.T, expected, actual string) { require.NoError(t, err) bs2, err := os.ReadFile(actual) require.NoError(t, err) - require.Equal(t, string(bs1), string(bs2)) + require.Equal(t, string(bs1), string(bs2), "files differ: %s", expected) } diff --git a/pkg/generators/models/templates.go b/pkg/generators/models/templates.go index 6d2fb61..042a55a 100644 --- a/pkg/generators/models/templates.go +++ b/pkg/generators/models/templates.go @@ -25,5 +25,6 @@ var ( "toSnakeCase": tpl.ToSnakeCase, "removeSpecial": tpl.RemoveSpecial, "typeDisplayName": tpl.TypeDisplayName, + "ternary": tpl.Ternary, } ) diff --git a/pkg/generators/models/templates/model.gotpl b/pkg/generators/models/templates/model.gotpl index c76708f..b4ae98e 100644 --- a/pkg/generators/models/templates/model.gotpl +++ b/pkg/generators/models/templates/model.gotpl @@ -21,6 +21,7 @@ import ( {{- $modelName := .Name }} {{- range .Properties}} +{{- if .IsRef }}{{ continue }}{{ end }} {{- if .PatternErrorMsg}} // {{ $modelName}}{{.Name}}PatternError is the error message returned for pattern validation errors on {{ $modelName}}.{{.Name}} var {{ $modelName}}{{.Name}}PatternError = validation.NewError("validation_{{.Name }}_pattern_invalid", "{{.PatternErrorMsg}}") @@ -39,7 +40,7 @@ type {{.Name}} {{ .GoType }} type {{.Name}} struct { {{- range .Properties}} {{ (printf "%s: %s" .Name .Description) | commentBlock }} - {{.Name}} {{.GoType}} {{.JSONTags}} + {{.Name}} {{if .IsPtr}}*{{end}}{{.GoType}} {{.JSONTags}} {{- end}} } {{- end}} @@ -125,29 +126,31 @@ func (m {{$modelPropertiesName}}) Validate() error { {{- range .Properties}} {{- if .NeedsValidation }} "{{ firstLower .Name }}": validation.Validate( + {{- /* isRequired: {{.IsRequired}}, isPtr: {{.IsPtr}}, isString: {{.IsString}}, isMap: {{.IsMap}}, isEnum: {{.IsEnum}}, hasFormat: {{.HasFormat}}, hasMin: {{.HasMin}}, min: {{.Min}}, hasMax: {{.HasMax}}, max: {{.Max}}, hasMinLength: {{.HasMinLength}}, minLength: {{.MinLength}}, hasMaxLength: {{.HasMaxLength}}, maxLength: {{.MaxLength}} */}} m.{{ .Name }}, - {{- if and .IsRequiredInValidation}} - {{- if or .IsEnum .Min .MinLength .HasFormat}}validation.Required,{{ else }}validation.NotNil,{{- end }} - {{- end }} - {{- if .HasMin }}validation.Min({{ .GoType }}({{ .Min }})),{{ end }} - {{- if .HasMax }}validation.Max({{ .GoType }}({{ .Max }})),{{ end }} - {{- if or .HasMinLength .HasMaxLength }}validation.Length({{ .MinLength }},{{ .MaxLength }}),{{ end }} - {{- if .IsDate }}validation.Date("2006-01-02"),{{ end }} - {{- if .IsDateTime }}validation.Date(time.RFC3339),{{ end }} - {{- if .IsBase64 }}is.Base64,{{ end }} - {{- if .IsEmail }}is.EmailFormat,{{ end }} - {{- if .IsUUID }}is.UUID,{{ end }} - {{- if .IsURL }}is.URL.Error("must be a valid URL with HTTP or HTTPS scheme"),{{ end }} - {{- if .IsURI }}is.RequestURI,{{ end }} - {{- if .IsRequestURI }}is.RequestURL.Error("must be valid URI with scheme"),{{ end }} - {{- if .IsHostname }}is.Host,{{ end }} - {{- if .IsIPv4 }}is.IPv4,{{ end }} - {{- if .IsIPv6 }}is.IPv6,{{ end }} - {{- if .IsIP }}is.IP,{{ end }} - {{- if .Pattern }}validation.Match({{ $modelName | firstLower }}{{.Name}}Pattern){{if .PatternErrorMsg}}.ErrorObject({{ $modelName}}{{.Name}}PatternError){{end}},{{ end }} - {{- if and .IsOneOf (not .IsRequiredInValidation) }} - {{.GoType}}NilableRule{}, - validation.Skip, + {{- range .DerivedValidations }}{{.}},{{ end }} + {{- if not .IsRef }} + {{- if .HasMin }}validation.Min({{ .GoType }}({{ .Min }})),{{ end }} + {{- if .HasMax }}validation.Max({{ .GoType }}({{ .Max }})),{{ end }} + {{- if or .HasMinLength .HasMaxLength }}validation.Length({{ .MinLength }},{{ .MaxLength }}),{{ end }} + {{- if or .HasMinItems .HasMaxItems }}validation.Length({{ .MinItems }},{{ .MaxItems }}),{{ end }} + {{- if or .HasMinProps .HasMaxProps }}validation.Length({{ .MinProps }},{{ .MaxProps }}),{{ end }} + {{- if .IsDate }}validation.Date("2006-01-02"),{{ end }} + {{- if .IsDateTime }}validation.Date(time.RFC3339),{{ end }} + {{- if .IsBase64 }}is.Base64,{{ end }} + {{- if .IsEmail }}is.EmailFormat,{{ end }} + {{- if .IsUUID }}is.UUID,{{ end }} + {{- if .IsURL }}is.URL.Error("must be a valid URL with HTTP or HTTPS scheme"),{{ end }} + {{- if .IsURI }}is.RequestURI,{{ end }} + {{- if .IsRequestURI }}is.RequestURL.Error("must be valid URI with scheme"),{{ end }} + {{- if .IsHostname }}is.Host,{{ end }} + {{- if .IsIPv4 }}is.IPv4,{{ end }} + {{- if .IsIPv6 }}is.IPv6,{{ end }} + {{- if .IsIP }}is.IP,{{ end }} + {{- if .Pattern }}validation.Match({{ firstLower $modelName }}{{.Name}}Pattern){{if .PatternErrorMsg}}.ErrorObject({{ $modelName}}{{.Name}}PatternError){{end}},{{ end }} + {{- else if .IsOneOf }} + {{.GoType}}NilableRule{}, + validation.Skip, {{- end }} ), {{- end }} @@ -156,12 +159,12 @@ func (m {{$modelPropertiesName}}) Validate() error { } {{ range .Properties}} // Get{{.Name}} returns the {{.Name}} property -func (m {{$modelPropertiesName}}) Get{{.Name}}() {{.GoType}} { +func (m {{$modelPropertiesName}}) Get{{.Name}}() {{if .IsPtr}}*{{end}}{{.GoType}} { return m.{{.Name}} } // Set{{.Name}} sets the {{.Name}} property -func (m *{{$modelPropertiesName}}) Set{{.Name}}(val {{.GoType}}) { +func (m *{{$modelPropertiesName}}) Set{{.Name}}(val {{if .IsPtr}}*{{end}}{{.GoType}}) { m.{{.Name}} = val } {{ end}} diff --git a/pkg/generators/models/templates/oneof.gotpl b/pkg/generators/models/templates/oneof.gotpl index 3d9a32e..682cc01 100644 --- a/pkg/generators/models/templates/oneof.gotpl +++ b/pkg/generators/models/templates/oneof.gotpl @@ -109,8 +109,10 @@ func (m *{{$modelName}}) UnmarshalJSON(bs []byte) error { } {{- range $convert := .ConvertSpecs }} -{{ (printf "From%s sets the %s from a %s" ($convert.TargetGoType | typeDisplayName) $modelName $convert.TargetGoType) | commentBlock }} -func (m *{{$modelName}}) From{{$convert.TargetGoType | typeDisplayName}}(data {{$convert.TargetGoType}}) { +{{ $star := $convert.IsPtr | ternary "*" "" }} +{{ $nullable := $convert.IsPtr | ternary "Nullable" "" }} +{{ (printf "From%s%s sets the %s from a %s%s" $nullable ($convert.TargetGoType | typeDisplayName) $modelName $star $convert.TargetGoType) | commentBlock }} +func (m *{{$modelName}}) From{{$nullable}}{{$convert.TargetGoType | typeDisplayName}}(data {{$star}}{{$convert.TargetGoType}}) { m.data = data } @@ -127,8 +129,10 @@ func (m {{$modelName}}) As(target interface{}) error { } {{- range $convert := .ConvertSpecs }} -{{ (printf "As%s converts %s to a %s" ($convert.TargetGoType | typeDisplayName) $modelName $convert.TargetGoType) | commentBlock }} -func (m {{$modelName}}) As{{$convert.TargetGoType | typeDisplayName }}() (result {{$convert.TargetGoType}}, err error) { +{{ $star := $convert.IsPtr | ternary "*" "" }} +{{ $nullable := $convert.IsPtr | ternary "Nullable" "" }} +{{ (printf "As%s%s converts %s to a %s%s" $nullable ($convert.TargetGoType | typeDisplayName) $modelName $star $convert.TargetGoType) | commentBlock }} +func (m {{$modelName}}) As{{$nullable}}{{$convert.TargetGoType | typeDisplayName }}() (result {{$star}}{{$convert.TargetGoType}}, err error) { {{- if $.Discriminator.Field }} if m.data == nil { return result, Empty{{$modelName}}Error diff --git a/pkg/generators/models/testdata/cases/allof_enum/expected/model_artist.go b/pkg/generators/models/testdata/cases/allof_enum/expected/model_artist.go index 5c584b4..957de91 100644 --- a/pkg/generators/models/testdata/cases/allof_enum/expected/model_artist.go +++ b/pkg/generators/models/testdata/cases/allof_enum/expected/model_artist.go @@ -13,9 +13,9 @@ import ( // Artist is an object. type Artist struct { // LeftHand: - LeftHand *AnyThing `json:"leftHand,omitempty" mapstructure:"leftHand,omitempty"` + LeftHand AnyThing `json:"leftHand,omitempty" mapstructure:"leftHand,omitempty"` // RightHand: - RightHand *Color `json:"rightHand,omitempty" mapstructure:"rightHand,omitempty"` + RightHand Color `json:"rightHand,omitempty" mapstructure:"rightHand,omitempty"` } // Validate implements basic validation for this model @@ -31,21 +31,21 @@ func (m Artist) Validate() error { } // GetLeftHand returns the LeftHand property -func (m Artist) GetLeftHand() *AnyThing { +func (m Artist) GetLeftHand() AnyThing { return m.LeftHand } // SetLeftHand sets the LeftHand property -func (m *Artist) SetLeftHand(val *AnyThing) { +func (m *Artist) SetLeftHand(val AnyThing) { m.LeftHand = val } // GetRightHand returns the RightHand property -func (m Artist) GetRightHand() *Color { +func (m Artist) GetRightHand() Color { return m.RightHand } // SetRightHand sets the RightHand property -func (m *Artist) SetRightHand(val *Color) { +func (m *Artist) SetRightHand(val Color) { m.RightHand = val } diff --git a/pkg/generators/models/testdata/cases/allof_enum/generated/model_artist.go b/pkg/generators/models/testdata/cases/allof_enum/generated/model_artist.go index 5c584b4..957de91 100644 --- a/pkg/generators/models/testdata/cases/allof_enum/generated/model_artist.go +++ b/pkg/generators/models/testdata/cases/allof_enum/generated/model_artist.go @@ -13,9 +13,9 @@ import ( // Artist is an object. type Artist struct { // LeftHand: - LeftHand *AnyThing `json:"leftHand,omitempty" mapstructure:"leftHand,omitempty"` + LeftHand AnyThing `json:"leftHand,omitempty" mapstructure:"leftHand,omitempty"` // RightHand: - RightHand *Color `json:"rightHand,omitempty" mapstructure:"rightHand,omitempty"` + RightHand Color `json:"rightHand,omitempty" mapstructure:"rightHand,omitempty"` } // Validate implements basic validation for this model @@ -31,21 +31,21 @@ func (m Artist) Validate() error { } // GetLeftHand returns the LeftHand property -func (m Artist) GetLeftHand() *AnyThing { +func (m Artist) GetLeftHand() AnyThing { return m.LeftHand } // SetLeftHand sets the LeftHand property -func (m *Artist) SetLeftHand(val *AnyThing) { +func (m *Artist) SetLeftHand(val AnyThing) { m.LeftHand = val } // GetRightHand returns the RightHand property -func (m Artist) GetRightHand() *Color { +func (m Artist) GetRightHand() Color { return m.RightHand } // SetRightHand sets the RightHand property -func (m *Artist) SetRightHand(val *Color) { +func (m *Artist) SetRightHand(val Color) { m.RightHand = val } diff --git a/pkg/generators/models/testdata/cases/allof_mixin_skipping/expected/model_column_metadata.go b/pkg/generators/models/testdata/cases/allof_mixin_skipping/expected/model_column_metadata.go index ed176d4..62a1621 100644 --- a/pkg/generators/models/testdata/cases/allof_mixin_skipping/expected/model_column_metadata.go +++ b/pkg/generators/models/testdata/cases/allof_mixin_skipping/expected/model_column_metadata.go @@ -24,7 +24,7 @@ type ColumnMetadata struct { func (m ColumnMetadata) Validate() error { return validation.Errors{ "type": validation.Validate( - m.Type, validation.NotNil, + m.Type, ), }.Filter() } diff --git a/pkg/generators/models/testdata/cases/allof_mixin_skipping/generated/model_column_metadata.go b/pkg/generators/models/testdata/cases/allof_mixin_skipping/generated/model_column_metadata.go index ed176d4..62a1621 100644 --- a/pkg/generators/models/testdata/cases/allof_mixin_skipping/generated/model_column_metadata.go +++ b/pkg/generators/models/testdata/cases/allof_mixin_skipping/generated/model_column_metadata.go @@ -24,7 +24,7 @@ type ColumnMetadata struct { func (m ColumnMetadata) Validate() error { return validation.Errors{ "type": validation.Validate( - m.Type, validation.NotNil, + m.Type, ), }.Filter() } diff --git a/pkg/generators/models/testdata/cases/enum_nullable/api.yaml b/pkg/generators/models/testdata/cases/enum_nullable/api.yaml new file mode 100644 index 0000000..61d4f49 --- /dev/null +++ b/pkg/generators/models/testdata/cases/enum_nullable/api.yaml @@ -0,0 +1,184 @@ +openapi: 3.0.0 +info: + version: 0.1.0 + title: Test + +components: + schemas: + + StringEnum: + type: string + enum: + - foo + - bar + + StringEnumNullable: + type: string + nullable: true + enum: + - foo + - bar + + StringEnumWithNullAndNullable: + type: string + nullable: true # it should make ref nullable + enum: + - foo + - bar + - null + + StringEnumWithNull: + type: string + enum: + - foo + - bar + - null + + StringEnumWithZero: + type: string + enum: + - foo + - bar + - "" + + StringEnumWithNullAndZero: + type: string + enum: + - foo + - bar + - null + - "" + + IntegerEnumWithNull: + type: integer + enum: + - 1 + - 2 + - null + + IntegerEnumWithZero: + type: integer + enum: + - 1 + - 2 + - 0 + + IntegerEnumWithNullAndZero: + type: integer + enum: + - 1 + - 2 + - null + - 0 + + + Wrapper: + type: object + required: + - requiredStringEnum + - requiredNullableStringEnumWithNull + - requiredStringEnumNullable + - requiredStringEnumWithNullAndNullable + - requiredStringEnumWithNull + - requiredStringEnumWithZero + - requiredNullableStringEnumWithZero + - requiredStringEnumWithNullAndZero + - requiredNullableStringEnumWithNullAndZero + + properties: + + stringEnum: + description: a regular string enum + allOf: + - $ref: "#/components/schemas/StringEnum" + + requiredStringEnum: + description: a required regular string enum + allOf: + - $ref: "#/components/schemas/StringEnum" + + stringEnumNullable: + description: a string enum that is nullable + allOf: + - $ref: "#/components/schemas/StringEnumNullable" + + requiredStringEnumNullable: + description: a required string enum that is nullable + allOf: + - $ref: "#/components/schemas/StringEnumNullable" + + nullableStringEnumWithNull: + description: Add nullable in the prop for non nullable enum + allOf: + - $ref: "#/components/schemas/StringEnumWithNull" + nullable: true + + requiredNullableStringEnumWithNull: + description: Add nullable in the prop for non nullable enum + allOf: + - $ref: "#/components/schemas/StringEnumWithNull" + nullable: true + + stringEnumWithNullAndNullable: + description: Ref a nullable enum. It should make the prop nullable + allOf: + - $ref: "#/components/schemas/StringEnumWithNullAndNullable" + + requiredStringEnumWithNullAndNullable: + description: Ref a nullable enum. It should make the prop nullable + allOf: + - $ref: "#/components/schemas/StringEnumWithNullAndNullable" + + stringEnumWithNull: + description: Ref a non nullable enum. It should not make the prop nullable + allOf: + - $ref: "#/components/schemas/StringEnumWithNull" + + requiredStringEnumWithNull: + description: Ref a non nullable enum. It should not make the prop nullable + allOf: + - $ref: "#/components/schemas/StringEnumWithNull" + + stringEnumWithNullAndZero: + description: Ref a non nullable enum with zero value. + allOf: + - $ref: "#/components/schemas/StringEnumWithNullAndZero" + + nullableStringEnumWithNullAndZero: + description: Add nullable in the prop for non nullable enum with zero value and null + allOf: + - $ref: "#/components/schemas/StringEnumWithNullAndZero" + nullable: true + + requiredStringEnumWithNullAndZero: + description: Ref a non nullable enum with zero value. + allOf: + - $ref: "#/components/schemas/StringEnumWithNullAndZero" + + requiredNullableStringEnumWithNullAndZero: + description: Add nullable in the prop for non nullable enum with zero value and null + allOf: + - $ref: "#/components/schemas/StringEnumWithNullAndZero" + nullable: true + + stringEnumWithZero: + description: Ref a non nullable enum with zero value. + allOf: + - $ref: "#/components/schemas/StringEnumWithZero" + + requiredStringEnumWithZero: + description: Ref a non nullable enum with zero value. + allOf: + - $ref: "#/components/schemas/StringEnumWithZero" + + nullableStringEnumWithZero: + description: Add nullable in the prop for non nullable enum with zero value + allOf: + - $ref: "#/components/schemas/StringEnumWithZero" + nullable: true + + requiredNullableStringEnumWithZero: + description: Add nullable in the prop for non nullable enum with zero value + allOf: + - $ref: "#/components/schemas/StringEnumWithZero" + nullable: true \ No newline at end of file diff --git a/pkg/generators/models/testdata/cases/enum_nullable/expected/model_integer_enum_with_null.go b/pkg/generators/models/testdata/cases/enum_nullable/expected/model_integer_enum_with_null.go new file mode 100644 index 0000000..6d2b9f4 --- /dev/null +++ b/pkg/generators/models/testdata/cases/enum_nullable/expected/model_integer_enum_with_null.go @@ -0,0 +1,41 @@ +// Code generated by openapi-generator-go DO NOT EDIT. +// +// Source: +// +// Title: Test +// Version: 0.1.0 +package generatortest + +import ( + validation "github.com/go-ozzo/ozzo-validation/v4" +) + +// IntegerEnumWithNull is an enum. +type IntegerEnumWithNull int32 + +// Validate implements basic validation for this model +func (m IntegerEnumWithNull) Validate() error { + return InKnownIntegerEnumWithNull.Validate(m) +} + +var ( + IntegerEnumWithNull1 IntegerEnumWithNull = 1 + IntegerEnumWithNull2 IntegerEnumWithNull = 2 + + // KnownIntegerEnumWithNull is the list of valid IntegerEnumWithNull + KnownIntegerEnumWithNull = []IntegerEnumWithNull{ + IntegerEnumWithNull1, + IntegerEnumWithNull2, + } + // KnownIntegerEnumWithNullInt32 is the list of valid IntegerEnumWithNull as int32 + KnownIntegerEnumWithNullInt32 = []int32{ + int32(IntegerEnumWithNull1), + int32(IntegerEnumWithNull2), + } + + // InKnownIntegerEnumWithNull is an ozzo-validator for IntegerEnumWithNull + InKnownIntegerEnumWithNull = validation.In( + IntegerEnumWithNull1, + IntegerEnumWithNull2, + ) +) diff --git a/pkg/generators/models/testdata/cases/enum_nullable/expected/model_integer_enum_with_null_and_zero.go b/pkg/generators/models/testdata/cases/enum_nullable/expected/model_integer_enum_with_null_and_zero.go new file mode 100644 index 0000000..85c620d --- /dev/null +++ b/pkg/generators/models/testdata/cases/enum_nullable/expected/model_integer_enum_with_null_and_zero.go @@ -0,0 +1,45 @@ +// Code generated by openapi-generator-go DO NOT EDIT. +// +// Source: +// +// Title: Test +// Version: 0.1.0 +package generatortest + +import ( + validation "github.com/go-ozzo/ozzo-validation/v4" +) + +// IntegerEnumWithNullAndZero is an enum. +type IntegerEnumWithNullAndZero int32 + +// Validate implements basic validation for this model +func (m IntegerEnumWithNullAndZero) Validate() error { + return InKnownIntegerEnumWithNullAndZero.Validate(m) +} + +var ( + IntegerEnumWithNullAndZero0 IntegerEnumWithNullAndZero = 0 + IntegerEnumWithNullAndZero1 IntegerEnumWithNullAndZero = 1 + IntegerEnumWithNullAndZero2 IntegerEnumWithNullAndZero = 2 + + // KnownIntegerEnumWithNullAndZero is the list of valid IntegerEnumWithNullAndZero + KnownIntegerEnumWithNullAndZero = []IntegerEnumWithNullAndZero{ + IntegerEnumWithNullAndZero0, + IntegerEnumWithNullAndZero1, + IntegerEnumWithNullAndZero2, + } + // KnownIntegerEnumWithNullAndZeroInt32 is the list of valid IntegerEnumWithNullAndZero as int32 + KnownIntegerEnumWithNullAndZeroInt32 = []int32{ + int32(IntegerEnumWithNullAndZero0), + int32(IntegerEnumWithNullAndZero1), + int32(IntegerEnumWithNullAndZero2), + } + + // InKnownIntegerEnumWithNullAndZero is an ozzo-validator for IntegerEnumWithNullAndZero + InKnownIntegerEnumWithNullAndZero = validation.In( + IntegerEnumWithNullAndZero0, + IntegerEnumWithNullAndZero1, + IntegerEnumWithNullAndZero2, + ) +) diff --git a/pkg/generators/models/testdata/cases/enum_nullable/expected/model_integer_enum_with_zero.go b/pkg/generators/models/testdata/cases/enum_nullable/expected/model_integer_enum_with_zero.go new file mode 100644 index 0000000..aaaf347 --- /dev/null +++ b/pkg/generators/models/testdata/cases/enum_nullable/expected/model_integer_enum_with_zero.go @@ -0,0 +1,45 @@ +// Code generated by openapi-generator-go DO NOT EDIT. +// +// Source: +// +// Title: Test +// Version: 0.1.0 +package generatortest + +import ( + validation "github.com/go-ozzo/ozzo-validation/v4" +) + +// IntegerEnumWithZero is an enum. +type IntegerEnumWithZero int32 + +// Validate implements basic validation for this model +func (m IntegerEnumWithZero) Validate() error { + return InKnownIntegerEnumWithZero.Validate(m) +} + +var ( + IntegerEnumWithZero0 IntegerEnumWithZero = 0 + IntegerEnumWithZero1 IntegerEnumWithZero = 1 + IntegerEnumWithZero2 IntegerEnumWithZero = 2 + + // KnownIntegerEnumWithZero is the list of valid IntegerEnumWithZero + KnownIntegerEnumWithZero = []IntegerEnumWithZero{ + IntegerEnumWithZero0, + IntegerEnumWithZero1, + IntegerEnumWithZero2, + } + // KnownIntegerEnumWithZeroInt32 is the list of valid IntegerEnumWithZero as int32 + KnownIntegerEnumWithZeroInt32 = []int32{ + int32(IntegerEnumWithZero0), + int32(IntegerEnumWithZero1), + int32(IntegerEnumWithZero2), + } + + // InKnownIntegerEnumWithZero is an ozzo-validator for IntegerEnumWithZero + InKnownIntegerEnumWithZero = validation.In( + IntegerEnumWithZero0, + IntegerEnumWithZero1, + IntegerEnumWithZero2, + ) +) diff --git a/pkg/generators/models/testdata/cases/enum_nullable/expected/model_string_enum.go b/pkg/generators/models/testdata/cases/enum_nullable/expected/model_string_enum.go new file mode 100644 index 0000000..ee9e103 --- /dev/null +++ b/pkg/generators/models/testdata/cases/enum_nullable/expected/model_string_enum.go @@ -0,0 +1,41 @@ +// Code generated by openapi-generator-go DO NOT EDIT. +// +// Source: +// +// Title: Test +// Version: 0.1.0 +package generatortest + +import ( + validation "github.com/go-ozzo/ozzo-validation/v4" +) + +// StringEnum is an enum. +type StringEnum string + +// Validate implements basic validation for this model +func (m StringEnum) Validate() error { + return InKnownStringEnum.Validate(m) +} + +var ( + StringEnumBar StringEnum = "bar" + StringEnumFoo StringEnum = "foo" + + // KnownStringEnum is the list of valid StringEnum + KnownStringEnum = []StringEnum{ + StringEnumBar, + StringEnumFoo, + } + // KnownStringEnumString is the list of valid StringEnum as string + KnownStringEnumString = []string{ + string(StringEnumBar), + string(StringEnumFoo), + } + + // InKnownStringEnum is an ozzo-validator for StringEnum + InKnownStringEnum = validation.In( + StringEnumBar, + StringEnumFoo, + ) +) diff --git a/pkg/generators/models/testdata/cases/enum_nullable/expected/model_string_enum_nullable.go b/pkg/generators/models/testdata/cases/enum_nullable/expected/model_string_enum_nullable.go new file mode 100644 index 0000000..3af067e --- /dev/null +++ b/pkg/generators/models/testdata/cases/enum_nullable/expected/model_string_enum_nullable.go @@ -0,0 +1,41 @@ +// Code generated by openapi-generator-go DO NOT EDIT. +// +// Source: +// +// Title: Test +// Version: 0.1.0 +package generatortest + +import ( + validation "github.com/go-ozzo/ozzo-validation/v4" +) + +// StringEnumNullable is an enum. +type StringEnumNullable string + +// Validate implements basic validation for this model +func (m StringEnumNullable) Validate() error { + return InKnownStringEnumNullable.Validate(m) +} + +var ( + StringEnumNullableBar StringEnumNullable = "bar" + StringEnumNullableFoo StringEnumNullable = "foo" + + // KnownStringEnumNullable is the list of valid StringEnumNullable + KnownStringEnumNullable = []StringEnumNullable{ + StringEnumNullableBar, + StringEnumNullableFoo, + } + // KnownStringEnumNullableString is the list of valid StringEnumNullable as string + KnownStringEnumNullableString = []string{ + string(StringEnumNullableBar), + string(StringEnumNullableFoo), + } + + // InKnownStringEnumNullable is an ozzo-validator for StringEnumNullable + InKnownStringEnumNullable = validation.In( + StringEnumNullableBar, + StringEnumNullableFoo, + ) +) diff --git a/pkg/generators/models/testdata/cases/enum_nullable/expected/model_string_enum_with_null.go b/pkg/generators/models/testdata/cases/enum_nullable/expected/model_string_enum_with_null.go new file mode 100644 index 0000000..e7824fc --- /dev/null +++ b/pkg/generators/models/testdata/cases/enum_nullable/expected/model_string_enum_with_null.go @@ -0,0 +1,41 @@ +// Code generated by openapi-generator-go DO NOT EDIT. +// +// Source: +// +// Title: Test +// Version: 0.1.0 +package generatortest + +import ( + validation "github.com/go-ozzo/ozzo-validation/v4" +) + +// StringEnumWithNull is an enum. +type StringEnumWithNull string + +// Validate implements basic validation for this model +func (m StringEnumWithNull) Validate() error { + return InKnownStringEnumWithNull.Validate(m) +} + +var ( + StringEnumWithNullBar StringEnumWithNull = "bar" + StringEnumWithNullFoo StringEnumWithNull = "foo" + + // KnownStringEnumWithNull is the list of valid StringEnumWithNull + KnownStringEnumWithNull = []StringEnumWithNull{ + StringEnumWithNullBar, + StringEnumWithNullFoo, + } + // KnownStringEnumWithNullString is the list of valid StringEnumWithNull as string + KnownStringEnumWithNullString = []string{ + string(StringEnumWithNullBar), + string(StringEnumWithNullFoo), + } + + // InKnownStringEnumWithNull is an ozzo-validator for StringEnumWithNull + InKnownStringEnumWithNull = validation.In( + StringEnumWithNullBar, + StringEnumWithNullFoo, + ) +) diff --git a/pkg/generators/models/testdata/cases/enum_nullable/expected/model_string_enum_with_null_and_nullable.go b/pkg/generators/models/testdata/cases/enum_nullable/expected/model_string_enum_with_null_and_nullable.go new file mode 100644 index 0000000..501b46a --- /dev/null +++ b/pkg/generators/models/testdata/cases/enum_nullable/expected/model_string_enum_with_null_and_nullable.go @@ -0,0 +1,41 @@ +// Code generated by openapi-generator-go DO NOT EDIT. +// +// Source: +// +// Title: Test +// Version: 0.1.0 +package generatortest + +import ( + validation "github.com/go-ozzo/ozzo-validation/v4" +) + +// StringEnumWithNullAndNullable is an enum. +type StringEnumWithNullAndNullable string + +// Validate implements basic validation for this model +func (m StringEnumWithNullAndNullable) Validate() error { + return InKnownStringEnumWithNullAndNullable.Validate(m) +} + +var ( + StringEnumWithNullAndNullableBar StringEnumWithNullAndNullable = "bar" + StringEnumWithNullAndNullableFoo StringEnumWithNullAndNullable = "foo" + + // KnownStringEnumWithNullAndNullable is the list of valid StringEnumWithNullAndNullable + KnownStringEnumWithNullAndNullable = []StringEnumWithNullAndNullable{ + StringEnumWithNullAndNullableBar, + StringEnumWithNullAndNullableFoo, + } + // KnownStringEnumWithNullAndNullableString is the list of valid StringEnumWithNullAndNullable as string + KnownStringEnumWithNullAndNullableString = []string{ + string(StringEnumWithNullAndNullableBar), + string(StringEnumWithNullAndNullableFoo), + } + + // InKnownStringEnumWithNullAndNullable is an ozzo-validator for StringEnumWithNullAndNullable + InKnownStringEnumWithNullAndNullable = validation.In( + StringEnumWithNullAndNullableBar, + StringEnumWithNullAndNullableFoo, + ) +) diff --git a/pkg/generators/models/testdata/cases/enum_nullable/expected/model_string_enum_with_null_and_zero.go b/pkg/generators/models/testdata/cases/enum_nullable/expected/model_string_enum_with_null_and_zero.go new file mode 100644 index 0000000..44f2cd9 --- /dev/null +++ b/pkg/generators/models/testdata/cases/enum_nullable/expected/model_string_enum_with_null_and_zero.go @@ -0,0 +1,45 @@ +// Code generated by openapi-generator-go DO NOT EDIT. +// +// Source: +// +// Title: Test +// Version: 0.1.0 +package generatortest + +import ( + validation "github.com/go-ozzo/ozzo-validation/v4" +) + +// StringEnumWithNullAndZero is an enum. +type StringEnumWithNullAndZero string + +// Validate implements basic validation for this model +func (m StringEnumWithNullAndZero) Validate() error { + return InKnownStringEnumWithNullAndZero.Validate(m) +} + +var ( + StringEnumWithNullAndZeroBar StringEnumWithNullAndZero = "bar" + StringEnumWithNullAndZeroFoo StringEnumWithNullAndZero = "foo" + StringEnumWithNullAndZeroItem3 StringEnumWithNullAndZero = "" + + // KnownStringEnumWithNullAndZero is the list of valid StringEnumWithNullAndZero + KnownStringEnumWithNullAndZero = []StringEnumWithNullAndZero{ + StringEnumWithNullAndZeroBar, + StringEnumWithNullAndZeroFoo, + StringEnumWithNullAndZeroItem3, + } + // KnownStringEnumWithNullAndZeroString is the list of valid StringEnumWithNullAndZero as string + KnownStringEnumWithNullAndZeroString = []string{ + string(StringEnumWithNullAndZeroBar), + string(StringEnumWithNullAndZeroFoo), + string(StringEnumWithNullAndZeroItem3), + } + + // InKnownStringEnumWithNullAndZero is an ozzo-validator for StringEnumWithNullAndZero + InKnownStringEnumWithNullAndZero = validation.In( + StringEnumWithNullAndZeroBar, + StringEnumWithNullAndZeroFoo, + StringEnumWithNullAndZeroItem3, + ) +) diff --git a/pkg/generators/models/testdata/cases/enum_nullable/expected/model_string_enum_with_zero.go b/pkg/generators/models/testdata/cases/enum_nullable/expected/model_string_enum_with_zero.go new file mode 100644 index 0000000..6b5f622 --- /dev/null +++ b/pkg/generators/models/testdata/cases/enum_nullable/expected/model_string_enum_with_zero.go @@ -0,0 +1,45 @@ +// Code generated by openapi-generator-go DO NOT EDIT. +// +// Source: +// +// Title: Test +// Version: 0.1.0 +package generatortest + +import ( + validation "github.com/go-ozzo/ozzo-validation/v4" +) + +// StringEnumWithZero is an enum. +type StringEnumWithZero string + +// Validate implements basic validation for this model +func (m StringEnumWithZero) Validate() error { + return InKnownStringEnumWithZero.Validate(m) +} + +var ( + StringEnumWithZeroBar StringEnumWithZero = "bar" + StringEnumWithZeroFoo StringEnumWithZero = "foo" + StringEnumWithZeroItem2 StringEnumWithZero = "" + + // KnownStringEnumWithZero is the list of valid StringEnumWithZero + KnownStringEnumWithZero = []StringEnumWithZero{ + StringEnumWithZeroBar, + StringEnumWithZeroFoo, + StringEnumWithZeroItem2, + } + // KnownStringEnumWithZeroString is the list of valid StringEnumWithZero as string + KnownStringEnumWithZeroString = []string{ + string(StringEnumWithZeroBar), + string(StringEnumWithZeroFoo), + string(StringEnumWithZeroItem2), + } + + // InKnownStringEnumWithZero is an ozzo-validator for StringEnumWithZero + InKnownStringEnumWithZero = validation.In( + StringEnumWithZeroBar, + StringEnumWithZeroFoo, + StringEnumWithZeroItem2, + ) +) diff --git a/pkg/generators/models/testdata/cases/enum_nullable/expected/model_wrapper.go b/pkg/generators/models/testdata/cases/enum_nullable/expected/model_wrapper.go new file mode 100644 index 0000000..ed4b33c --- /dev/null +++ b/pkg/generators/models/testdata/cases/enum_nullable/expected/model_wrapper.go @@ -0,0 +1,291 @@ +// Code generated by openapi-generator-go DO NOT EDIT. +// +// Source: +// +// Title: Test +// Version: 0.1.0 +package generatortest + +import ( + validation "github.com/go-ozzo/ozzo-validation/v4" +) + +// Wrapper is an object. +type Wrapper struct { + // NullableStringEnumWithNull: Add nullable in the prop for non nullable enum + NullableStringEnumWithNull *StringEnumWithNull `json:"nullableStringEnumWithNull,omitempty" mapstructure:"nullableStringEnumWithNull,omitempty"` + // NullableStringEnumWithNullAndZero: Add nullable in the prop for non nullable enum with zero value and null + NullableStringEnumWithNullAndZero *StringEnumWithNullAndZero `json:"nullableStringEnumWithNullAndZero,omitempty" mapstructure:"nullableStringEnumWithNullAndZero,omitempty"` + // NullableStringEnumWithZero: Add nullable in the prop for non nullable enum with zero value + NullableStringEnumWithZero StringEnumWithZero `json:"nullableStringEnumWithZero,omitempty" mapstructure:"nullableStringEnumWithZero,omitempty"` + // RequiredNullableStringEnumWithNull: Add nullable in the prop for non nullable enum + RequiredNullableStringEnumWithNull *StringEnumWithNull `json:"requiredNullableStringEnumWithNull" mapstructure:"requiredNullableStringEnumWithNull"` + // RequiredNullableStringEnumWithNullAndZero: Add nullable in the prop for non nullable enum with zero value and null + RequiredNullableStringEnumWithNullAndZero *StringEnumWithNullAndZero `json:"requiredNullableStringEnumWithNullAndZero" mapstructure:"requiredNullableStringEnumWithNullAndZero"` + // RequiredNullableStringEnumWithZero: Add nullable in the prop for non nullable enum with zero value + RequiredNullableStringEnumWithZero StringEnumWithZero `json:"requiredNullableStringEnumWithZero" mapstructure:"requiredNullableStringEnumWithZero"` + // RequiredStringEnum: a required regular string enum + RequiredStringEnum StringEnum `json:"requiredStringEnum" mapstructure:"requiredStringEnum"` + // RequiredStringEnumNullable: a required string enum that is nullable + RequiredStringEnumNullable StringEnumNullable `json:"requiredStringEnumNullable" mapstructure:"requiredStringEnumNullable"` + // RequiredStringEnumWithNull: Ref a non nullable enum. It should not make the prop nullable + RequiredStringEnumWithNull StringEnumWithNull `json:"requiredStringEnumWithNull" mapstructure:"requiredStringEnumWithNull"` + // RequiredStringEnumWithNullAndNullable: Ref a nullable enum. It should make the prop nullable + RequiredStringEnumWithNullAndNullable *StringEnumWithNullAndNullable `json:"requiredStringEnumWithNullAndNullable" mapstructure:"requiredStringEnumWithNullAndNullable"` + // RequiredStringEnumWithNullAndZero: Ref a non nullable enum with zero value. + RequiredStringEnumWithNullAndZero StringEnumWithNullAndZero `json:"requiredStringEnumWithNullAndZero" mapstructure:"requiredStringEnumWithNullAndZero"` + // RequiredStringEnumWithZero: Ref a non nullable enum with zero value. + RequiredStringEnumWithZero StringEnumWithZero `json:"requiredStringEnumWithZero" mapstructure:"requiredStringEnumWithZero"` + // StringEnum: a regular string enum + StringEnum StringEnum `json:"stringEnum,omitempty" mapstructure:"stringEnum,omitempty"` + // StringEnumNullable: a string enum that is nullable + StringEnumNullable StringEnumNullable `json:"stringEnumNullable,omitempty" mapstructure:"stringEnumNullable,omitempty"` + // StringEnumWithNull: Ref a non nullable enum. It should not make the prop nullable + StringEnumWithNull StringEnumWithNull `json:"stringEnumWithNull,omitempty" mapstructure:"stringEnumWithNull,omitempty"` + // StringEnumWithNullAndNullable: Ref a nullable enum. It should make the prop nullable + StringEnumWithNullAndNullable *StringEnumWithNullAndNullable `json:"stringEnumWithNullAndNullable,omitempty" mapstructure:"stringEnumWithNullAndNullable,omitempty"` + // StringEnumWithNullAndZero: Ref a non nullable enum with zero value. + StringEnumWithNullAndZero StringEnumWithNullAndZero `json:"stringEnumWithNullAndZero,omitempty" mapstructure:"stringEnumWithNullAndZero,omitempty"` + // StringEnumWithZero: Ref a non nullable enum with zero value. + StringEnumWithZero StringEnumWithZero `json:"stringEnumWithZero,omitempty" mapstructure:"stringEnumWithZero,omitempty"` +} + +// Validate implements basic validation for this model +func (m Wrapper) Validate() error { + return validation.Errors{ + "nullableStringEnumWithNull": validation.Validate( + m.NullableStringEnumWithNull, validation.NilOrNotEmpty, + ), + "nullableStringEnumWithNullAndZero": validation.Validate( + m.NullableStringEnumWithNullAndZero, + ), + "nullableStringEnumWithZero": validation.Validate( + m.NullableStringEnumWithZero, + ), + "requiredNullableStringEnumWithNull": validation.Validate( + m.RequiredNullableStringEnumWithNull, validation.NilOrNotEmpty, + ), + "requiredNullableStringEnumWithNullAndZero": validation.Validate( + m.RequiredNullableStringEnumWithNullAndZero, + ), + "requiredNullableStringEnumWithZero": validation.Validate( + m.RequiredNullableStringEnumWithZero, + ), + "requiredStringEnum": validation.Validate( + m.RequiredStringEnum, validation.Required, + ), + "requiredStringEnumNullable": validation.Validate( + m.RequiredStringEnumNullable, validation.Required, + ), + "requiredStringEnumWithNull": validation.Validate( + m.RequiredStringEnumWithNull, validation.Required, + ), + "requiredStringEnumWithNullAndNullable": validation.Validate( + m.RequiredStringEnumWithNullAndNullable, validation.NilOrNotEmpty, + ), + "requiredStringEnumWithNullAndZero": validation.Validate( + m.RequiredStringEnumWithNullAndZero, + ), + "requiredStringEnumWithZero": validation.Validate( + m.RequiredStringEnumWithZero, + ), + "stringEnum": validation.Validate( + m.StringEnum, + ), + "stringEnumNullable": validation.Validate( + m.StringEnumNullable, + ), + "stringEnumWithNull": validation.Validate( + m.StringEnumWithNull, + ), + "stringEnumWithNullAndNullable": validation.Validate( + m.StringEnumWithNullAndNullable, validation.NilOrNotEmpty, + ), + "stringEnumWithNullAndZero": validation.Validate( + m.StringEnumWithNullAndZero, + ), + "stringEnumWithZero": validation.Validate( + m.StringEnumWithZero, + ), + }.Filter() +} + +// GetNullableStringEnumWithNull returns the NullableStringEnumWithNull property +func (m Wrapper) GetNullableStringEnumWithNull() *StringEnumWithNull { + return m.NullableStringEnumWithNull +} + +// SetNullableStringEnumWithNull sets the NullableStringEnumWithNull property +func (m *Wrapper) SetNullableStringEnumWithNull(val *StringEnumWithNull) { + m.NullableStringEnumWithNull = val +} + +// GetNullableStringEnumWithNullAndZero returns the NullableStringEnumWithNullAndZero property +func (m Wrapper) GetNullableStringEnumWithNullAndZero() *StringEnumWithNullAndZero { + return m.NullableStringEnumWithNullAndZero +} + +// SetNullableStringEnumWithNullAndZero sets the NullableStringEnumWithNullAndZero property +func (m *Wrapper) SetNullableStringEnumWithNullAndZero(val *StringEnumWithNullAndZero) { + m.NullableStringEnumWithNullAndZero = val +} + +// GetNullableStringEnumWithZero returns the NullableStringEnumWithZero property +func (m Wrapper) GetNullableStringEnumWithZero() StringEnumWithZero { + return m.NullableStringEnumWithZero +} + +// SetNullableStringEnumWithZero sets the NullableStringEnumWithZero property +func (m *Wrapper) SetNullableStringEnumWithZero(val StringEnumWithZero) { + m.NullableStringEnumWithZero = val +} + +// GetRequiredNullableStringEnumWithNull returns the RequiredNullableStringEnumWithNull property +func (m Wrapper) GetRequiredNullableStringEnumWithNull() *StringEnumWithNull { + return m.RequiredNullableStringEnumWithNull +} + +// SetRequiredNullableStringEnumWithNull sets the RequiredNullableStringEnumWithNull property +func (m *Wrapper) SetRequiredNullableStringEnumWithNull(val *StringEnumWithNull) { + m.RequiredNullableStringEnumWithNull = val +} + +// GetRequiredNullableStringEnumWithNullAndZero returns the RequiredNullableStringEnumWithNullAndZero property +func (m Wrapper) GetRequiredNullableStringEnumWithNullAndZero() *StringEnumWithNullAndZero { + return m.RequiredNullableStringEnumWithNullAndZero +} + +// SetRequiredNullableStringEnumWithNullAndZero sets the RequiredNullableStringEnumWithNullAndZero property +func (m *Wrapper) SetRequiredNullableStringEnumWithNullAndZero(val *StringEnumWithNullAndZero) { + m.RequiredNullableStringEnumWithNullAndZero = val +} + +// GetRequiredNullableStringEnumWithZero returns the RequiredNullableStringEnumWithZero property +func (m Wrapper) GetRequiredNullableStringEnumWithZero() StringEnumWithZero { + return m.RequiredNullableStringEnumWithZero +} + +// SetRequiredNullableStringEnumWithZero sets the RequiredNullableStringEnumWithZero property +func (m *Wrapper) SetRequiredNullableStringEnumWithZero(val StringEnumWithZero) { + m.RequiredNullableStringEnumWithZero = val +} + +// GetRequiredStringEnum returns the RequiredStringEnum property +func (m Wrapper) GetRequiredStringEnum() StringEnum { + return m.RequiredStringEnum +} + +// SetRequiredStringEnum sets the RequiredStringEnum property +func (m *Wrapper) SetRequiredStringEnum(val StringEnum) { + m.RequiredStringEnum = val +} + +// GetRequiredStringEnumNullable returns the RequiredStringEnumNullable property +func (m Wrapper) GetRequiredStringEnumNullable() StringEnumNullable { + return m.RequiredStringEnumNullable +} + +// SetRequiredStringEnumNullable sets the RequiredStringEnumNullable property +func (m *Wrapper) SetRequiredStringEnumNullable(val StringEnumNullable) { + m.RequiredStringEnumNullable = val +} + +// GetRequiredStringEnumWithNull returns the RequiredStringEnumWithNull property +func (m Wrapper) GetRequiredStringEnumWithNull() StringEnumWithNull { + return m.RequiredStringEnumWithNull +} + +// SetRequiredStringEnumWithNull sets the RequiredStringEnumWithNull property +func (m *Wrapper) SetRequiredStringEnumWithNull(val StringEnumWithNull) { + m.RequiredStringEnumWithNull = val +} + +// GetRequiredStringEnumWithNullAndNullable returns the RequiredStringEnumWithNullAndNullable property +func (m Wrapper) GetRequiredStringEnumWithNullAndNullable() *StringEnumWithNullAndNullable { + return m.RequiredStringEnumWithNullAndNullable +} + +// SetRequiredStringEnumWithNullAndNullable sets the RequiredStringEnumWithNullAndNullable property +func (m *Wrapper) SetRequiredStringEnumWithNullAndNullable(val *StringEnumWithNullAndNullable) { + m.RequiredStringEnumWithNullAndNullable = val +} + +// GetRequiredStringEnumWithNullAndZero returns the RequiredStringEnumWithNullAndZero property +func (m Wrapper) GetRequiredStringEnumWithNullAndZero() StringEnumWithNullAndZero { + return m.RequiredStringEnumWithNullAndZero +} + +// SetRequiredStringEnumWithNullAndZero sets the RequiredStringEnumWithNullAndZero property +func (m *Wrapper) SetRequiredStringEnumWithNullAndZero(val StringEnumWithNullAndZero) { + m.RequiredStringEnumWithNullAndZero = val +} + +// GetRequiredStringEnumWithZero returns the RequiredStringEnumWithZero property +func (m Wrapper) GetRequiredStringEnumWithZero() StringEnumWithZero { + return m.RequiredStringEnumWithZero +} + +// SetRequiredStringEnumWithZero sets the RequiredStringEnumWithZero property +func (m *Wrapper) SetRequiredStringEnumWithZero(val StringEnumWithZero) { + m.RequiredStringEnumWithZero = val +} + +// GetStringEnum returns the StringEnum property +func (m Wrapper) GetStringEnum() StringEnum { + return m.StringEnum +} + +// SetStringEnum sets the StringEnum property +func (m *Wrapper) SetStringEnum(val StringEnum) { + m.StringEnum = val +} + +// GetStringEnumNullable returns the StringEnumNullable property +func (m Wrapper) GetStringEnumNullable() StringEnumNullable { + return m.StringEnumNullable +} + +// SetStringEnumNullable sets the StringEnumNullable property +func (m *Wrapper) SetStringEnumNullable(val StringEnumNullable) { + m.StringEnumNullable = val +} + +// GetStringEnumWithNull returns the StringEnumWithNull property +func (m Wrapper) GetStringEnumWithNull() StringEnumWithNull { + return m.StringEnumWithNull +} + +// SetStringEnumWithNull sets the StringEnumWithNull property +func (m *Wrapper) SetStringEnumWithNull(val StringEnumWithNull) { + m.StringEnumWithNull = val +} + +// GetStringEnumWithNullAndNullable returns the StringEnumWithNullAndNullable property +func (m Wrapper) GetStringEnumWithNullAndNullable() *StringEnumWithNullAndNullable { + return m.StringEnumWithNullAndNullable +} + +// SetStringEnumWithNullAndNullable sets the StringEnumWithNullAndNullable property +func (m *Wrapper) SetStringEnumWithNullAndNullable(val *StringEnumWithNullAndNullable) { + m.StringEnumWithNullAndNullable = val +} + +// GetStringEnumWithNullAndZero returns the StringEnumWithNullAndZero property +func (m Wrapper) GetStringEnumWithNullAndZero() StringEnumWithNullAndZero { + return m.StringEnumWithNullAndZero +} + +// SetStringEnumWithNullAndZero sets the StringEnumWithNullAndZero property +func (m *Wrapper) SetStringEnumWithNullAndZero(val StringEnumWithNullAndZero) { + m.StringEnumWithNullAndZero = val +} + +// GetStringEnumWithZero returns the StringEnumWithZero property +func (m Wrapper) GetStringEnumWithZero() StringEnumWithZero { + return m.StringEnumWithZero +} + +// SetStringEnumWithZero sets the StringEnumWithZero property +func (m *Wrapper) SetStringEnumWithZero(val StringEnumWithZero) { + m.StringEnumWithZero = val +} diff --git a/pkg/generators/models/testdata/cases/enum_nullable/generated/model_integer_enum_with_null.go b/pkg/generators/models/testdata/cases/enum_nullable/generated/model_integer_enum_with_null.go new file mode 100644 index 0000000..6d2b9f4 --- /dev/null +++ b/pkg/generators/models/testdata/cases/enum_nullable/generated/model_integer_enum_with_null.go @@ -0,0 +1,41 @@ +// Code generated by openapi-generator-go DO NOT EDIT. +// +// Source: +// +// Title: Test +// Version: 0.1.0 +package generatortest + +import ( + validation "github.com/go-ozzo/ozzo-validation/v4" +) + +// IntegerEnumWithNull is an enum. +type IntegerEnumWithNull int32 + +// Validate implements basic validation for this model +func (m IntegerEnumWithNull) Validate() error { + return InKnownIntegerEnumWithNull.Validate(m) +} + +var ( + IntegerEnumWithNull1 IntegerEnumWithNull = 1 + IntegerEnumWithNull2 IntegerEnumWithNull = 2 + + // KnownIntegerEnumWithNull is the list of valid IntegerEnumWithNull + KnownIntegerEnumWithNull = []IntegerEnumWithNull{ + IntegerEnumWithNull1, + IntegerEnumWithNull2, + } + // KnownIntegerEnumWithNullInt32 is the list of valid IntegerEnumWithNull as int32 + KnownIntegerEnumWithNullInt32 = []int32{ + int32(IntegerEnumWithNull1), + int32(IntegerEnumWithNull2), + } + + // InKnownIntegerEnumWithNull is an ozzo-validator for IntegerEnumWithNull + InKnownIntegerEnumWithNull = validation.In( + IntegerEnumWithNull1, + IntegerEnumWithNull2, + ) +) diff --git a/pkg/generators/models/testdata/cases/enum_nullable/generated/model_integer_enum_with_null_and_zero.go b/pkg/generators/models/testdata/cases/enum_nullable/generated/model_integer_enum_with_null_and_zero.go new file mode 100644 index 0000000..85c620d --- /dev/null +++ b/pkg/generators/models/testdata/cases/enum_nullable/generated/model_integer_enum_with_null_and_zero.go @@ -0,0 +1,45 @@ +// Code generated by openapi-generator-go DO NOT EDIT. +// +// Source: +// +// Title: Test +// Version: 0.1.0 +package generatortest + +import ( + validation "github.com/go-ozzo/ozzo-validation/v4" +) + +// IntegerEnumWithNullAndZero is an enum. +type IntegerEnumWithNullAndZero int32 + +// Validate implements basic validation for this model +func (m IntegerEnumWithNullAndZero) Validate() error { + return InKnownIntegerEnumWithNullAndZero.Validate(m) +} + +var ( + IntegerEnumWithNullAndZero0 IntegerEnumWithNullAndZero = 0 + IntegerEnumWithNullAndZero1 IntegerEnumWithNullAndZero = 1 + IntegerEnumWithNullAndZero2 IntegerEnumWithNullAndZero = 2 + + // KnownIntegerEnumWithNullAndZero is the list of valid IntegerEnumWithNullAndZero + KnownIntegerEnumWithNullAndZero = []IntegerEnumWithNullAndZero{ + IntegerEnumWithNullAndZero0, + IntegerEnumWithNullAndZero1, + IntegerEnumWithNullAndZero2, + } + // KnownIntegerEnumWithNullAndZeroInt32 is the list of valid IntegerEnumWithNullAndZero as int32 + KnownIntegerEnumWithNullAndZeroInt32 = []int32{ + int32(IntegerEnumWithNullAndZero0), + int32(IntegerEnumWithNullAndZero1), + int32(IntegerEnumWithNullAndZero2), + } + + // InKnownIntegerEnumWithNullAndZero is an ozzo-validator for IntegerEnumWithNullAndZero + InKnownIntegerEnumWithNullAndZero = validation.In( + IntegerEnumWithNullAndZero0, + IntegerEnumWithNullAndZero1, + IntegerEnumWithNullAndZero2, + ) +) diff --git a/pkg/generators/models/testdata/cases/enum_nullable/generated/model_integer_enum_with_zero.go b/pkg/generators/models/testdata/cases/enum_nullable/generated/model_integer_enum_with_zero.go new file mode 100644 index 0000000..aaaf347 --- /dev/null +++ b/pkg/generators/models/testdata/cases/enum_nullable/generated/model_integer_enum_with_zero.go @@ -0,0 +1,45 @@ +// Code generated by openapi-generator-go DO NOT EDIT. +// +// Source: +// +// Title: Test +// Version: 0.1.0 +package generatortest + +import ( + validation "github.com/go-ozzo/ozzo-validation/v4" +) + +// IntegerEnumWithZero is an enum. +type IntegerEnumWithZero int32 + +// Validate implements basic validation for this model +func (m IntegerEnumWithZero) Validate() error { + return InKnownIntegerEnumWithZero.Validate(m) +} + +var ( + IntegerEnumWithZero0 IntegerEnumWithZero = 0 + IntegerEnumWithZero1 IntegerEnumWithZero = 1 + IntegerEnumWithZero2 IntegerEnumWithZero = 2 + + // KnownIntegerEnumWithZero is the list of valid IntegerEnumWithZero + KnownIntegerEnumWithZero = []IntegerEnumWithZero{ + IntegerEnumWithZero0, + IntegerEnumWithZero1, + IntegerEnumWithZero2, + } + // KnownIntegerEnumWithZeroInt32 is the list of valid IntegerEnumWithZero as int32 + KnownIntegerEnumWithZeroInt32 = []int32{ + int32(IntegerEnumWithZero0), + int32(IntegerEnumWithZero1), + int32(IntegerEnumWithZero2), + } + + // InKnownIntegerEnumWithZero is an ozzo-validator for IntegerEnumWithZero + InKnownIntegerEnumWithZero = validation.In( + IntegerEnumWithZero0, + IntegerEnumWithZero1, + IntegerEnumWithZero2, + ) +) diff --git a/pkg/generators/models/testdata/cases/enum_nullable/generated/model_string_enum.go b/pkg/generators/models/testdata/cases/enum_nullable/generated/model_string_enum.go new file mode 100644 index 0000000..ee9e103 --- /dev/null +++ b/pkg/generators/models/testdata/cases/enum_nullable/generated/model_string_enum.go @@ -0,0 +1,41 @@ +// Code generated by openapi-generator-go DO NOT EDIT. +// +// Source: +// +// Title: Test +// Version: 0.1.0 +package generatortest + +import ( + validation "github.com/go-ozzo/ozzo-validation/v4" +) + +// StringEnum is an enum. +type StringEnum string + +// Validate implements basic validation for this model +func (m StringEnum) Validate() error { + return InKnownStringEnum.Validate(m) +} + +var ( + StringEnumBar StringEnum = "bar" + StringEnumFoo StringEnum = "foo" + + // KnownStringEnum is the list of valid StringEnum + KnownStringEnum = []StringEnum{ + StringEnumBar, + StringEnumFoo, + } + // KnownStringEnumString is the list of valid StringEnum as string + KnownStringEnumString = []string{ + string(StringEnumBar), + string(StringEnumFoo), + } + + // InKnownStringEnum is an ozzo-validator for StringEnum + InKnownStringEnum = validation.In( + StringEnumBar, + StringEnumFoo, + ) +) diff --git a/pkg/generators/models/testdata/cases/enum_nullable/generated/model_string_enum_nullable.go b/pkg/generators/models/testdata/cases/enum_nullable/generated/model_string_enum_nullable.go new file mode 100644 index 0000000..3af067e --- /dev/null +++ b/pkg/generators/models/testdata/cases/enum_nullable/generated/model_string_enum_nullable.go @@ -0,0 +1,41 @@ +// Code generated by openapi-generator-go DO NOT EDIT. +// +// Source: +// +// Title: Test +// Version: 0.1.0 +package generatortest + +import ( + validation "github.com/go-ozzo/ozzo-validation/v4" +) + +// StringEnumNullable is an enum. +type StringEnumNullable string + +// Validate implements basic validation for this model +func (m StringEnumNullable) Validate() error { + return InKnownStringEnumNullable.Validate(m) +} + +var ( + StringEnumNullableBar StringEnumNullable = "bar" + StringEnumNullableFoo StringEnumNullable = "foo" + + // KnownStringEnumNullable is the list of valid StringEnumNullable + KnownStringEnumNullable = []StringEnumNullable{ + StringEnumNullableBar, + StringEnumNullableFoo, + } + // KnownStringEnumNullableString is the list of valid StringEnumNullable as string + KnownStringEnumNullableString = []string{ + string(StringEnumNullableBar), + string(StringEnumNullableFoo), + } + + // InKnownStringEnumNullable is an ozzo-validator for StringEnumNullable + InKnownStringEnumNullable = validation.In( + StringEnumNullableBar, + StringEnumNullableFoo, + ) +) diff --git a/pkg/generators/models/testdata/cases/enum_nullable/generated/model_string_enum_with_null.go b/pkg/generators/models/testdata/cases/enum_nullable/generated/model_string_enum_with_null.go new file mode 100644 index 0000000..e7824fc --- /dev/null +++ b/pkg/generators/models/testdata/cases/enum_nullable/generated/model_string_enum_with_null.go @@ -0,0 +1,41 @@ +// Code generated by openapi-generator-go DO NOT EDIT. +// +// Source: +// +// Title: Test +// Version: 0.1.0 +package generatortest + +import ( + validation "github.com/go-ozzo/ozzo-validation/v4" +) + +// StringEnumWithNull is an enum. +type StringEnumWithNull string + +// Validate implements basic validation for this model +func (m StringEnumWithNull) Validate() error { + return InKnownStringEnumWithNull.Validate(m) +} + +var ( + StringEnumWithNullBar StringEnumWithNull = "bar" + StringEnumWithNullFoo StringEnumWithNull = "foo" + + // KnownStringEnumWithNull is the list of valid StringEnumWithNull + KnownStringEnumWithNull = []StringEnumWithNull{ + StringEnumWithNullBar, + StringEnumWithNullFoo, + } + // KnownStringEnumWithNullString is the list of valid StringEnumWithNull as string + KnownStringEnumWithNullString = []string{ + string(StringEnumWithNullBar), + string(StringEnumWithNullFoo), + } + + // InKnownStringEnumWithNull is an ozzo-validator for StringEnumWithNull + InKnownStringEnumWithNull = validation.In( + StringEnumWithNullBar, + StringEnumWithNullFoo, + ) +) diff --git a/pkg/generators/models/testdata/cases/enum_nullable/generated/model_string_enum_with_null_and_nullable.go b/pkg/generators/models/testdata/cases/enum_nullable/generated/model_string_enum_with_null_and_nullable.go new file mode 100644 index 0000000..501b46a --- /dev/null +++ b/pkg/generators/models/testdata/cases/enum_nullable/generated/model_string_enum_with_null_and_nullable.go @@ -0,0 +1,41 @@ +// Code generated by openapi-generator-go DO NOT EDIT. +// +// Source: +// +// Title: Test +// Version: 0.1.0 +package generatortest + +import ( + validation "github.com/go-ozzo/ozzo-validation/v4" +) + +// StringEnumWithNullAndNullable is an enum. +type StringEnumWithNullAndNullable string + +// Validate implements basic validation for this model +func (m StringEnumWithNullAndNullable) Validate() error { + return InKnownStringEnumWithNullAndNullable.Validate(m) +} + +var ( + StringEnumWithNullAndNullableBar StringEnumWithNullAndNullable = "bar" + StringEnumWithNullAndNullableFoo StringEnumWithNullAndNullable = "foo" + + // KnownStringEnumWithNullAndNullable is the list of valid StringEnumWithNullAndNullable + KnownStringEnumWithNullAndNullable = []StringEnumWithNullAndNullable{ + StringEnumWithNullAndNullableBar, + StringEnumWithNullAndNullableFoo, + } + // KnownStringEnumWithNullAndNullableString is the list of valid StringEnumWithNullAndNullable as string + KnownStringEnumWithNullAndNullableString = []string{ + string(StringEnumWithNullAndNullableBar), + string(StringEnumWithNullAndNullableFoo), + } + + // InKnownStringEnumWithNullAndNullable is an ozzo-validator for StringEnumWithNullAndNullable + InKnownStringEnumWithNullAndNullable = validation.In( + StringEnumWithNullAndNullableBar, + StringEnumWithNullAndNullableFoo, + ) +) diff --git a/pkg/generators/models/testdata/cases/enum_nullable/generated/model_string_enum_with_null_and_zero.go b/pkg/generators/models/testdata/cases/enum_nullable/generated/model_string_enum_with_null_and_zero.go new file mode 100644 index 0000000..44f2cd9 --- /dev/null +++ b/pkg/generators/models/testdata/cases/enum_nullable/generated/model_string_enum_with_null_and_zero.go @@ -0,0 +1,45 @@ +// Code generated by openapi-generator-go DO NOT EDIT. +// +// Source: +// +// Title: Test +// Version: 0.1.0 +package generatortest + +import ( + validation "github.com/go-ozzo/ozzo-validation/v4" +) + +// StringEnumWithNullAndZero is an enum. +type StringEnumWithNullAndZero string + +// Validate implements basic validation for this model +func (m StringEnumWithNullAndZero) Validate() error { + return InKnownStringEnumWithNullAndZero.Validate(m) +} + +var ( + StringEnumWithNullAndZeroBar StringEnumWithNullAndZero = "bar" + StringEnumWithNullAndZeroFoo StringEnumWithNullAndZero = "foo" + StringEnumWithNullAndZeroItem3 StringEnumWithNullAndZero = "" + + // KnownStringEnumWithNullAndZero is the list of valid StringEnumWithNullAndZero + KnownStringEnumWithNullAndZero = []StringEnumWithNullAndZero{ + StringEnumWithNullAndZeroBar, + StringEnumWithNullAndZeroFoo, + StringEnumWithNullAndZeroItem3, + } + // KnownStringEnumWithNullAndZeroString is the list of valid StringEnumWithNullAndZero as string + KnownStringEnumWithNullAndZeroString = []string{ + string(StringEnumWithNullAndZeroBar), + string(StringEnumWithNullAndZeroFoo), + string(StringEnumWithNullAndZeroItem3), + } + + // InKnownStringEnumWithNullAndZero is an ozzo-validator for StringEnumWithNullAndZero + InKnownStringEnumWithNullAndZero = validation.In( + StringEnumWithNullAndZeroBar, + StringEnumWithNullAndZeroFoo, + StringEnumWithNullAndZeroItem3, + ) +) diff --git a/pkg/generators/models/testdata/cases/enum_nullable/generated/model_string_enum_with_zero.go b/pkg/generators/models/testdata/cases/enum_nullable/generated/model_string_enum_with_zero.go new file mode 100644 index 0000000..6b5f622 --- /dev/null +++ b/pkg/generators/models/testdata/cases/enum_nullable/generated/model_string_enum_with_zero.go @@ -0,0 +1,45 @@ +// Code generated by openapi-generator-go DO NOT EDIT. +// +// Source: +// +// Title: Test +// Version: 0.1.0 +package generatortest + +import ( + validation "github.com/go-ozzo/ozzo-validation/v4" +) + +// StringEnumWithZero is an enum. +type StringEnumWithZero string + +// Validate implements basic validation for this model +func (m StringEnumWithZero) Validate() error { + return InKnownStringEnumWithZero.Validate(m) +} + +var ( + StringEnumWithZeroBar StringEnumWithZero = "bar" + StringEnumWithZeroFoo StringEnumWithZero = "foo" + StringEnumWithZeroItem2 StringEnumWithZero = "" + + // KnownStringEnumWithZero is the list of valid StringEnumWithZero + KnownStringEnumWithZero = []StringEnumWithZero{ + StringEnumWithZeroBar, + StringEnumWithZeroFoo, + StringEnumWithZeroItem2, + } + // KnownStringEnumWithZeroString is the list of valid StringEnumWithZero as string + KnownStringEnumWithZeroString = []string{ + string(StringEnumWithZeroBar), + string(StringEnumWithZeroFoo), + string(StringEnumWithZeroItem2), + } + + // InKnownStringEnumWithZero is an ozzo-validator for StringEnumWithZero + InKnownStringEnumWithZero = validation.In( + StringEnumWithZeroBar, + StringEnumWithZeroFoo, + StringEnumWithZeroItem2, + ) +) diff --git a/pkg/generators/models/testdata/cases/enum_nullable/generated/model_wrapper.go b/pkg/generators/models/testdata/cases/enum_nullable/generated/model_wrapper.go new file mode 100644 index 0000000..ed4b33c --- /dev/null +++ b/pkg/generators/models/testdata/cases/enum_nullable/generated/model_wrapper.go @@ -0,0 +1,291 @@ +// Code generated by openapi-generator-go DO NOT EDIT. +// +// Source: +// +// Title: Test +// Version: 0.1.0 +package generatortest + +import ( + validation "github.com/go-ozzo/ozzo-validation/v4" +) + +// Wrapper is an object. +type Wrapper struct { + // NullableStringEnumWithNull: Add nullable in the prop for non nullable enum + NullableStringEnumWithNull *StringEnumWithNull `json:"nullableStringEnumWithNull,omitempty" mapstructure:"nullableStringEnumWithNull,omitempty"` + // NullableStringEnumWithNullAndZero: Add nullable in the prop for non nullable enum with zero value and null + NullableStringEnumWithNullAndZero *StringEnumWithNullAndZero `json:"nullableStringEnumWithNullAndZero,omitempty" mapstructure:"nullableStringEnumWithNullAndZero,omitempty"` + // NullableStringEnumWithZero: Add nullable in the prop for non nullable enum with zero value + NullableStringEnumWithZero StringEnumWithZero `json:"nullableStringEnumWithZero,omitempty" mapstructure:"nullableStringEnumWithZero,omitempty"` + // RequiredNullableStringEnumWithNull: Add nullable in the prop for non nullable enum + RequiredNullableStringEnumWithNull *StringEnumWithNull `json:"requiredNullableStringEnumWithNull" mapstructure:"requiredNullableStringEnumWithNull"` + // RequiredNullableStringEnumWithNullAndZero: Add nullable in the prop for non nullable enum with zero value and null + RequiredNullableStringEnumWithNullAndZero *StringEnumWithNullAndZero `json:"requiredNullableStringEnumWithNullAndZero" mapstructure:"requiredNullableStringEnumWithNullAndZero"` + // RequiredNullableStringEnumWithZero: Add nullable in the prop for non nullable enum with zero value + RequiredNullableStringEnumWithZero StringEnumWithZero `json:"requiredNullableStringEnumWithZero" mapstructure:"requiredNullableStringEnumWithZero"` + // RequiredStringEnum: a required regular string enum + RequiredStringEnum StringEnum `json:"requiredStringEnum" mapstructure:"requiredStringEnum"` + // RequiredStringEnumNullable: a required string enum that is nullable + RequiredStringEnumNullable StringEnumNullable `json:"requiredStringEnumNullable" mapstructure:"requiredStringEnumNullable"` + // RequiredStringEnumWithNull: Ref a non nullable enum. It should not make the prop nullable + RequiredStringEnumWithNull StringEnumWithNull `json:"requiredStringEnumWithNull" mapstructure:"requiredStringEnumWithNull"` + // RequiredStringEnumWithNullAndNullable: Ref a nullable enum. It should make the prop nullable + RequiredStringEnumWithNullAndNullable *StringEnumWithNullAndNullable `json:"requiredStringEnumWithNullAndNullable" mapstructure:"requiredStringEnumWithNullAndNullable"` + // RequiredStringEnumWithNullAndZero: Ref a non nullable enum with zero value. + RequiredStringEnumWithNullAndZero StringEnumWithNullAndZero `json:"requiredStringEnumWithNullAndZero" mapstructure:"requiredStringEnumWithNullAndZero"` + // RequiredStringEnumWithZero: Ref a non nullable enum with zero value. + RequiredStringEnumWithZero StringEnumWithZero `json:"requiredStringEnumWithZero" mapstructure:"requiredStringEnumWithZero"` + // StringEnum: a regular string enum + StringEnum StringEnum `json:"stringEnum,omitempty" mapstructure:"stringEnum,omitempty"` + // StringEnumNullable: a string enum that is nullable + StringEnumNullable StringEnumNullable `json:"stringEnumNullable,omitempty" mapstructure:"stringEnumNullable,omitempty"` + // StringEnumWithNull: Ref a non nullable enum. It should not make the prop nullable + StringEnumWithNull StringEnumWithNull `json:"stringEnumWithNull,omitempty" mapstructure:"stringEnumWithNull,omitempty"` + // StringEnumWithNullAndNullable: Ref a nullable enum. It should make the prop nullable + StringEnumWithNullAndNullable *StringEnumWithNullAndNullable `json:"stringEnumWithNullAndNullable,omitempty" mapstructure:"stringEnumWithNullAndNullable,omitempty"` + // StringEnumWithNullAndZero: Ref a non nullable enum with zero value. + StringEnumWithNullAndZero StringEnumWithNullAndZero `json:"stringEnumWithNullAndZero,omitempty" mapstructure:"stringEnumWithNullAndZero,omitempty"` + // StringEnumWithZero: Ref a non nullable enum with zero value. + StringEnumWithZero StringEnumWithZero `json:"stringEnumWithZero,omitempty" mapstructure:"stringEnumWithZero,omitempty"` +} + +// Validate implements basic validation for this model +func (m Wrapper) Validate() error { + return validation.Errors{ + "nullableStringEnumWithNull": validation.Validate( + m.NullableStringEnumWithNull, validation.NilOrNotEmpty, + ), + "nullableStringEnumWithNullAndZero": validation.Validate( + m.NullableStringEnumWithNullAndZero, + ), + "nullableStringEnumWithZero": validation.Validate( + m.NullableStringEnumWithZero, + ), + "requiredNullableStringEnumWithNull": validation.Validate( + m.RequiredNullableStringEnumWithNull, validation.NilOrNotEmpty, + ), + "requiredNullableStringEnumWithNullAndZero": validation.Validate( + m.RequiredNullableStringEnumWithNullAndZero, + ), + "requiredNullableStringEnumWithZero": validation.Validate( + m.RequiredNullableStringEnumWithZero, + ), + "requiredStringEnum": validation.Validate( + m.RequiredStringEnum, validation.Required, + ), + "requiredStringEnumNullable": validation.Validate( + m.RequiredStringEnumNullable, validation.Required, + ), + "requiredStringEnumWithNull": validation.Validate( + m.RequiredStringEnumWithNull, validation.Required, + ), + "requiredStringEnumWithNullAndNullable": validation.Validate( + m.RequiredStringEnumWithNullAndNullable, validation.NilOrNotEmpty, + ), + "requiredStringEnumWithNullAndZero": validation.Validate( + m.RequiredStringEnumWithNullAndZero, + ), + "requiredStringEnumWithZero": validation.Validate( + m.RequiredStringEnumWithZero, + ), + "stringEnum": validation.Validate( + m.StringEnum, + ), + "stringEnumNullable": validation.Validate( + m.StringEnumNullable, + ), + "stringEnumWithNull": validation.Validate( + m.StringEnumWithNull, + ), + "stringEnumWithNullAndNullable": validation.Validate( + m.StringEnumWithNullAndNullable, validation.NilOrNotEmpty, + ), + "stringEnumWithNullAndZero": validation.Validate( + m.StringEnumWithNullAndZero, + ), + "stringEnumWithZero": validation.Validate( + m.StringEnumWithZero, + ), + }.Filter() +} + +// GetNullableStringEnumWithNull returns the NullableStringEnumWithNull property +func (m Wrapper) GetNullableStringEnumWithNull() *StringEnumWithNull { + return m.NullableStringEnumWithNull +} + +// SetNullableStringEnumWithNull sets the NullableStringEnumWithNull property +func (m *Wrapper) SetNullableStringEnumWithNull(val *StringEnumWithNull) { + m.NullableStringEnumWithNull = val +} + +// GetNullableStringEnumWithNullAndZero returns the NullableStringEnumWithNullAndZero property +func (m Wrapper) GetNullableStringEnumWithNullAndZero() *StringEnumWithNullAndZero { + return m.NullableStringEnumWithNullAndZero +} + +// SetNullableStringEnumWithNullAndZero sets the NullableStringEnumWithNullAndZero property +func (m *Wrapper) SetNullableStringEnumWithNullAndZero(val *StringEnumWithNullAndZero) { + m.NullableStringEnumWithNullAndZero = val +} + +// GetNullableStringEnumWithZero returns the NullableStringEnumWithZero property +func (m Wrapper) GetNullableStringEnumWithZero() StringEnumWithZero { + return m.NullableStringEnumWithZero +} + +// SetNullableStringEnumWithZero sets the NullableStringEnumWithZero property +func (m *Wrapper) SetNullableStringEnumWithZero(val StringEnumWithZero) { + m.NullableStringEnumWithZero = val +} + +// GetRequiredNullableStringEnumWithNull returns the RequiredNullableStringEnumWithNull property +func (m Wrapper) GetRequiredNullableStringEnumWithNull() *StringEnumWithNull { + return m.RequiredNullableStringEnumWithNull +} + +// SetRequiredNullableStringEnumWithNull sets the RequiredNullableStringEnumWithNull property +func (m *Wrapper) SetRequiredNullableStringEnumWithNull(val *StringEnumWithNull) { + m.RequiredNullableStringEnumWithNull = val +} + +// GetRequiredNullableStringEnumWithNullAndZero returns the RequiredNullableStringEnumWithNullAndZero property +func (m Wrapper) GetRequiredNullableStringEnumWithNullAndZero() *StringEnumWithNullAndZero { + return m.RequiredNullableStringEnumWithNullAndZero +} + +// SetRequiredNullableStringEnumWithNullAndZero sets the RequiredNullableStringEnumWithNullAndZero property +func (m *Wrapper) SetRequiredNullableStringEnumWithNullAndZero(val *StringEnumWithNullAndZero) { + m.RequiredNullableStringEnumWithNullAndZero = val +} + +// GetRequiredNullableStringEnumWithZero returns the RequiredNullableStringEnumWithZero property +func (m Wrapper) GetRequiredNullableStringEnumWithZero() StringEnumWithZero { + return m.RequiredNullableStringEnumWithZero +} + +// SetRequiredNullableStringEnumWithZero sets the RequiredNullableStringEnumWithZero property +func (m *Wrapper) SetRequiredNullableStringEnumWithZero(val StringEnumWithZero) { + m.RequiredNullableStringEnumWithZero = val +} + +// GetRequiredStringEnum returns the RequiredStringEnum property +func (m Wrapper) GetRequiredStringEnum() StringEnum { + return m.RequiredStringEnum +} + +// SetRequiredStringEnum sets the RequiredStringEnum property +func (m *Wrapper) SetRequiredStringEnum(val StringEnum) { + m.RequiredStringEnum = val +} + +// GetRequiredStringEnumNullable returns the RequiredStringEnumNullable property +func (m Wrapper) GetRequiredStringEnumNullable() StringEnumNullable { + return m.RequiredStringEnumNullable +} + +// SetRequiredStringEnumNullable sets the RequiredStringEnumNullable property +func (m *Wrapper) SetRequiredStringEnumNullable(val StringEnumNullable) { + m.RequiredStringEnumNullable = val +} + +// GetRequiredStringEnumWithNull returns the RequiredStringEnumWithNull property +func (m Wrapper) GetRequiredStringEnumWithNull() StringEnumWithNull { + return m.RequiredStringEnumWithNull +} + +// SetRequiredStringEnumWithNull sets the RequiredStringEnumWithNull property +func (m *Wrapper) SetRequiredStringEnumWithNull(val StringEnumWithNull) { + m.RequiredStringEnumWithNull = val +} + +// GetRequiredStringEnumWithNullAndNullable returns the RequiredStringEnumWithNullAndNullable property +func (m Wrapper) GetRequiredStringEnumWithNullAndNullable() *StringEnumWithNullAndNullable { + return m.RequiredStringEnumWithNullAndNullable +} + +// SetRequiredStringEnumWithNullAndNullable sets the RequiredStringEnumWithNullAndNullable property +func (m *Wrapper) SetRequiredStringEnumWithNullAndNullable(val *StringEnumWithNullAndNullable) { + m.RequiredStringEnumWithNullAndNullable = val +} + +// GetRequiredStringEnumWithNullAndZero returns the RequiredStringEnumWithNullAndZero property +func (m Wrapper) GetRequiredStringEnumWithNullAndZero() StringEnumWithNullAndZero { + return m.RequiredStringEnumWithNullAndZero +} + +// SetRequiredStringEnumWithNullAndZero sets the RequiredStringEnumWithNullAndZero property +func (m *Wrapper) SetRequiredStringEnumWithNullAndZero(val StringEnumWithNullAndZero) { + m.RequiredStringEnumWithNullAndZero = val +} + +// GetRequiredStringEnumWithZero returns the RequiredStringEnumWithZero property +func (m Wrapper) GetRequiredStringEnumWithZero() StringEnumWithZero { + return m.RequiredStringEnumWithZero +} + +// SetRequiredStringEnumWithZero sets the RequiredStringEnumWithZero property +func (m *Wrapper) SetRequiredStringEnumWithZero(val StringEnumWithZero) { + m.RequiredStringEnumWithZero = val +} + +// GetStringEnum returns the StringEnum property +func (m Wrapper) GetStringEnum() StringEnum { + return m.StringEnum +} + +// SetStringEnum sets the StringEnum property +func (m *Wrapper) SetStringEnum(val StringEnum) { + m.StringEnum = val +} + +// GetStringEnumNullable returns the StringEnumNullable property +func (m Wrapper) GetStringEnumNullable() StringEnumNullable { + return m.StringEnumNullable +} + +// SetStringEnumNullable sets the StringEnumNullable property +func (m *Wrapper) SetStringEnumNullable(val StringEnumNullable) { + m.StringEnumNullable = val +} + +// GetStringEnumWithNull returns the StringEnumWithNull property +func (m Wrapper) GetStringEnumWithNull() StringEnumWithNull { + return m.StringEnumWithNull +} + +// SetStringEnumWithNull sets the StringEnumWithNull property +func (m *Wrapper) SetStringEnumWithNull(val StringEnumWithNull) { + m.StringEnumWithNull = val +} + +// GetStringEnumWithNullAndNullable returns the StringEnumWithNullAndNullable property +func (m Wrapper) GetStringEnumWithNullAndNullable() *StringEnumWithNullAndNullable { + return m.StringEnumWithNullAndNullable +} + +// SetStringEnumWithNullAndNullable sets the StringEnumWithNullAndNullable property +func (m *Wrapper) SetStringEnumWithNullAndNullable(val *StringEnumWithNullAndNullable) { + m.StringEnumWithNullAndNullable = val +} + +// GetStringEnumWithNullAndZero returns the StringEnumWithNullAndZero property +func (m Wrapper) GetStringEnumWithNullAndZero() StringEnumWithNullAndZero { + return m.StringEnumWithNullAndZero +} + +// SetStringEnumWithNullAndZero sets the StringEnumWithNullAndZero property +func (m *Wrapper) SetStringEnumWithNullAndZero(val StringEnumWithNullAndZero) { + m.StringEnumWithNullAndZero = val +} + +// GetStringEnumWithZero returns the StringEnumWithZero property +func (m Wrapper) GetStringEnumWithZero() StringEnumWithZero { + return m.StringEnumWithZero +} + +// SetStringEnumWithZero sets the StringEnumWithZero property +func (m *Wrapper) SetStringEnumWithZero(val StringEnumWithZero) { + m.StringEnumWithZero = val +} diff --git a/pkg/generators/models/testdata/cases/nested_inline_arrays/expected/model_line.go b/pkg/generators/models/testdata/cases/nested_inline_arrays/expected/model_line.go index 7177b87..26b1518 100644 --- a/pkg/generators/models/testdata/cases/nested_inline_arrays/expected/model_line.go +++ b/pkg/generators/models/testdata/cases/nested_inline_arrays/expected/model_line.go @@ -22,7 +22,7 @@ type Line struct { func (m Line) Validate() error { return validation.Errors{ "coordinates": validation.Validate( - m.Coordinates, + m.Coordinates, validation.NilOrNotEmpty, validation.Length(2, 2), ), }.Filter() } diff --git a/pkg/generators/models/testdata/cases/nested_inline_arrays/generated/model_line.go b/pkg/generators/models/testdata/cases/nested_inline_arrays/generated/model_line.go index 7177b87..26b1518 100644 --- a/pkg/generators/models/testdata/cases/nested_inline_arrays/generated/model_line.go +++ b/pkg/generators/models/testdata/cases/nested_inline_arrays/generated/model_line.go @@ -22,7 +22,7 @@ type Line struct { func (m Line) Validate() error { return validation.Errors{ "coordinates": validation.Validate( - m.Coordinates, + m.Coordinates, validation.NilOrNotEmpty, validation.Length(2, 2), ), }.Filter() } diff --git a/pkg/generators/models/testdata/cases/oneof_mapped_discriminator/expected/model_non_empty_container.go b/pkg/generators/models/testdata/cases/oneof_mapped_discriminator/expected/model_non_empty_container.go index 6ae19bc..984bb5b 100644 --- a/pkg/generators/models/testdata/cases/oneof_mapped_discriminator/expected/model_non_empty_container.go +++ b/pkg/generators/models/testdata/cases/oneof_mapped_discriminator/expected/model_non_empty_container.go @@ -21,6 +21,8 @@ func (m NonEmptyContainer) Validate() error { return validation.Errors{ "error": validation.Validate( m.Error, validation.NotNil, + ErrorNilableRule{}, + validation.Skip, ), }.Filter() } diff --git a/pkg/generators/models/testdata/cases/oneof_mapped_discriminator/generated/model_non_empty_container.go b/pkg/generators/models/testdata/cases/oneof_mapped_discriminator/generated/model_non_empty_container.go index 6ae19bc..984bb5b 100644 --- a/pkg/generators/models/testdata/cases/oneof_mapped_discriminator/generated/model_non_empty_container.go +++ b/pkg/generators/models/testdata/cases/oneof_mapped_discriminator/generated/model_non_empty_container.go @@ -21,6 +21,8 @@ func (m NonEmptyContainer) Validate() error { return validation.Errors{ "error": validation.Validate( m.Error, validation.NotNil, + ErrorNilableRule{}, + validation.Skip, ), }.Filter() } diff --git a/pkg/generators/models/testdata/cases/parameter_model/api.yaml b/pkg/generators/models/testdata/cases/parameter_model/api.yaml index 894f26a..dba76d7 100644 --- a/pkg/generators/models/testdata/cases/parameter_model/api.yaml +++ b/pkg/generators/models/testdata/cases/parameter_model/api.yaml @@ -39,11 +39,11 @@ paths: - name: "-1" in: query schema: - type: int + type: integer - name: "+1" in: query schema: - type: int + type: integer - $ref: "#/components/parameters/PageNumber" responses: "200": diff --git a/pkg/generators/models/testdata/cases/parameter_model/expected/model_get_foo_query_parameters.go b/pkg/generators/models/testdata/cases/parameter_model/expected/model_get_foo_query_parameters.go index 5b4f772..a48e9c5 100644 --- a/pkg/generators/models/testdata/cases/parameter_model/expected/model_get_foo_query_parameters.go +++ b/pkg/generators/models/testdata/cases/parameter_model/expected/model_get_foo_query_parameters.go @@ -13,33 +13,36 @@ import ( // GetFooQueryParameters is an object. type GetFooQueryParameters struct { - // Param1: - Param1 string `json:"param1,omitempty" mapstructure:"param1,omitempty"` + // Plus1: + Plus1 int32 `json:"+1,omitempty" mapstructure:"+1,omitempty"` + // Minus1: + Minus1 int32 `json:"-1,omitempty" mapstructure:"-1,omitempty"` // Id: Id string `json:"id" mapstructure:"id"` + // Page: The current set of paged results to display, based on a 1-based array index + Page int32 `json:"page,omitempty" mapstructure:"page,omitempty"` + // Param1: + Param1 string `json:"param1,omitempty" mapstructure:"param1,omitempty"` // Param2: Param2 int32 `json:"param2,omitempty" mapstructure:"param2,omitempty"` // Param3: Param3 []string `json:"param3,omitempty" mapstructure:"param3,omitempty"` - // Param4: + // Param4: A referenced status Param4 ReferencedStatus `json:"param4,omitempty" mapstructure:"param4,omitempty"` - // Minus1: - Minus1 int `json:"-1,omitempty" mapstructure:"-1,omitempty"` - // Plus1: - Plus1 int `json:"+1,omitempty" mapstructure:"+1,omitempty"` - // Page: The current set of paged results to display, based on a 1-based array index - Page int32 `json:"page,omitempty" mapstructure:"page,omitempty"` } // Validate implements basic validation for this model func (m GetFooQueryParameters) Validate() error { return validation.Errors{ - "param1": validation.Validate( - m.Param1, is.UUID, - ), "id": validation.Validate( m.Id, validation.Required, is.UUID, ), + "page": validation.Validate( + m.Page, validation.Min(int32(1)), + ), + "param1": validation.Validate( + m.Param1, is.UUID, + ), "param2": validation.Validate( m.Param2, validation.Min(int32(0)), validation.Max(int32(10)), ), @@ -49,20 +52,27 @@ func (m GetFooQueryParameters) Validate() error { "param4": validation.Validate( m.Param4, ), - "page": validation.Validate( - m.Page, validation.Min(int32(1)), - ), }.Filter() } -// GetParam1 returns the Param1 property -func (m GetFooQueryParameters) GetParam1() string { - return m.Param1 +// GetPlus1 returns the Plus1 property +func (m GetFooQueryParameters) GetPlus1() int32 { + return m.Plus1 } -// SetParam1 sets the Param1 property -func (m *GetFooQueryParameters) SetParam1(val string) { - m.Param1 = val +// SetPlus1 sets the Plus1 property +func (m *GetFooQueryParameters) SetPlus1(val int32) { + m.Plus1 = val +} + +// GetMinus1 returns the Minus1 property +func (m GetFooQueryParameters) GetMinus1() int32 { + return m.Minus1 +} + +// SetMinus1 sets the Minus1 property +func (m *GetFooQueryParameters) SetMinus1(val int32) { + m.Minus1 = val } // GetId returns the Id property @@ -75,6 +85,26 @@ func (m *GetFooQueryParameters) SetId(val string) { m.Id = val } +// GetPage returns the Page property +func (m GetFooQueryParameters) GetPage() int32 { + return m.Page +} + +// SetPage sets the Page property +func (m *GetFooQueryParameters) SetPage(val int32) { + m.Page = val +} + +// GetParam1 returns the Param1 property +func (m GetFooQueryParameters) GetParam1() string { + return m.Param1 +} + +// SetParam1 sets the Param1 property +func (m *GetFooQueryParameters) SetParam1(val string) { + m.Param1 = val +} + // GetParam2 returns the Param2 property func (m GetFooQueryParameters) GetParam2() int32 { return m.Param2 @@ -104,33 +134,3 @@ func (m GetFooQueryParameters) GetParam4() ReferencedStatus { func (m *GetFooQueryParameters) SetParam4(val ReferencedStatus) { m.Param4 = val } - -// GetMinus1 returns the Minus1 property -func (m GetFooQueryParameters) GetMinus1() int { - return m.Minus1 -} - -// SetMinus1 sets the Minus1 property -func (m *GetFooQueryParameters) SetMinus1(val int) { - m.Minus1 = val -} - -// GetPlus1 returns the Plus1 property -func (m GetFooQueryParameters) GetPlus1() int { - return m.Plus1 -} - -// SetPlus1 sets the Plus1 property -func (m *GetFooQueryParameters) SetPlus1(val int) { - m.Plus1 = val -} - -// GetPage returns the Page property -func (m GetFooQueryParameters) GetPage() int32 { - return m.Page -} - -// SetPage sets the Page property -func (m *GetFooQueryParameters) SetPage(val int32) { - m.Page = val -} diff --git a/pkg/generators/models/testdata/cases/parameter_model/generated/model_get_foo_query_parameters.go b/pkg/generators/models/testdata/cases/parameter_model/generated/model_get_foo_query_parameters.go index 5b4f772..a48e9c5 100644 --- a/pkg/generators/models/testdata/cases/parameter_model/generated/model_get_foo_query_parameters.go +++ b/pkg/generators/models/testdata/cases/parameter_model/generated/model_get_foo_query_parameters.go @@ -13,33 +13,36 @@ import ( // GetFooQueryParameters is an object. type GetFooQueryParameters struct { - // Param1: - Param1 string `json:"param1,omitempty" mapstructure:"param1,omitempty"` + // Plus1: + Plus1 int32 `json:"+1,omitempty" mapstructure:"+1,omitempty"` + // Minus1: + Minus1 int32 `json:"-1,omitempty" mapstructure:"-1,omitempty"` // Id: Id string `json:"id" mapstructure:"id"` + // Page: The current set of paged results to display, based on a 1-based array index + Page int32 `json:"page,omitempty" mapstructure:"page,omitempty"` + // Param1: + Param1 string `json:"param1,omitempty" mapstructure:"param1,omitempty"` // Param2: Param2 int32 `json:"param2,omitempty" mapstructure:"param2,omitempty"` // Param3: Param3 []string `json:"param3,omitempty" mapstructure:"param3,omitempty"` - // Param4: + // Param4: A referenced status Param4 ReferencedStatus `json:"param4,omitempty" mapstructure:"param4,omitempty"` - // Minus1: - Minus1 int `json:"-1,omitempty" mapstructure:"-1,omitempty"` - // Plus1: - Plus1 int `json:"+1,omitempty" mapstructure:"+1,omitempty"` - // Page: The current set of paged results to display, based on a 1-based array index - Page int32 `json:"page,omitempty" mapstructure:"page,omitempty"` } // Validate implements basic validation for this model func (m GetFooQueryParameters) Validate() error { return validation.Errors{ - "param1": validation.Validate( - m.Param1, is.UUID, - ), "id": validation.Validate( m.Id, validation.Required, is.UUID, ), + "page": validation.Validate( + m.Page, validation.Min(int32(1)), + ), + "param1": validation.Validate( + m.Param1, is.UUID, + ), "param2": validation.Validate( m.Param2, validation.Min(int32(0)), validation.Max(int32(10)), ), @@ -49,20 +52,27 @@ func (m GetFooQueryParameters) Validate() error { "param4": validation.Validate( m.Param4, ), - "page": validation.Validate( - m.Page, validation.Min(int32(1)), - ), }.Filter() } -// GetParam1 returns the Param1 property -func (m GetFooQueryParameters) GetParam1() string { - return m.Param1 +// GetPlus1 returns the Plus1 property +func (m GetFooQueryParameters) GetPlus1() int32 { + return m.Plus1 } -// SetParam1 sets the Param1 property -func (m *GetFooQueryParameters) SetParam1(val string) { - m.Param1 = val +// SetPlus1 sets the Plus1 property +func (m *GetFooQueryParameters) SetPlus1(val int32) { + m.Plus1 = val +} + +// GetMinus1 returns the Minus1 property +func (m GetFooQueryParameters) GetMinus1() int32 { + return m.Minus1 +} + +// SetMinus1 sets the Minus1 property +func (m *GetFooQueryParameters) SetMinus1(val int32) { + m.Minus1 = val } // GetId returns the Id property @@ -75,6 +85,26 @@ func (m *GetFooQueryParameters) SetId(val string) { m.Id = val } +// GetPage returns the Page property +func (m GetFooQueryParameters) GetPage() int32 { + return m.Page +} + +// SetPage sets the Page property +func (m *GetFooQueryParameters) SetPage(val int32) { + m.Page = val +} + +// GetParam1 returns the Param1 property +func (m GetFooQueryParameters) GetParam1() string { + return m.Param1 +} + +// SetParam1 sets the Param1 property +func (m *GetFooQueryParameters) SetParam1(val string) { + m.Param1 = val +} + // GetParam2 returns the Param2 property func (m GetFooQueryParameters) GetParam2() int32 { return m.Param2 @@ -104,33 +134,3 @@ func (m GetFooQueryParameters) GetParam4() ReferencedStatus { func (m *GetFooQueryParameters) SetParam4(val ReferencedStatus) { m.Param4 = val } - -// GetMinus1 returns the Minus1 property -func (m GetFooQueryParameters) GetMinus1() int { - return m.Minus1 -} - -// SetMinus1 sets the Minus1 property -func (m *GetFooQueryParameters) SetMinus1(val int) { - m.Minus1 = val -} - -// GetPlus1 returns the Plus1 property -func (m GetFooQueryParameters) GetPlus1() int { - return m.Plus1 -} - -// SetPlus1 sets the Plus1 property -func (m *GetFooQueryParameters) SetPlus1(val int) { - m.Plus1 = val -} - -// GetPage returns the Page property -func (m GetFooQueryParameters) GetPage() int32 { - return m.Page -} - -// SetPage sets the Page property -func (m *GetFooQueryParameters) SetPage(val int32) { - m.Page = val -} diff --git a/pkg/generators/models/testdata/cases/required_nullable_combinations/api.yaml b/pkg/generators/models/testdata/cases/required_nullable_combinations/api.yaml new file mode 100644 index 0000000..44be5d8 --- /dev/null +++ b/pkg/generators/models/testdata/cases/required_nullable_combinations/api.yaml @@ -0,0 +1,49 @@ +openapi: 3.0.0 +info: + version: 0.1.0 + title: Test + +components: + schemas: + Enum: + type: string + enum: + - One + - Two + - null + Foo: + type: object + properties: + string: + type: string + string_nullable: + type: string + nullable: true + string_required: + type: string + string_required_nullable: + type: string + nullable: true + enum: + $ref: "#/components/schemas/Enum" + enum_nullable: + allOf: + - $ref: "#/components/schemas/Enum" + nullable: true + enum_required: + $ref: "#/components/schemas/Enum" + enum_required_nullable: + allOf: + - $ref: "#/components/schemas/Enum" + nullable: true + array_required_nullable: + type: array + minItems: 1 + maxItems: 2 + items: + type: string + required: + - string_required + - string_required_nullable + - enum_required + - enum_required_nullable diff --git a/pkg/generators/models/testdata/cases/required_nullable_combinations/expected/model_enum.go b/pkg/generators/models/testdata/cases/required_nullable_combinations/expected/model_enum.go new file mode 100644 index 0000000..9036726 --- /dev/null +++ b/pkg/generators/models/testdata/cases/required_nullable_combinations/expected/model_enum.go @@ -0,0 +1,41 @@ +// Code generated by openapi-generator-go DO NOT EDIT. +// +// Source: +// +// Title: Test +// Version: 0.1.0 +package generatortest + +import ( + validation "github.com/go-ozzo/ozzo-validation/v4" +) + +// Enum is an enum. +type Enum string + +// Validate implements basic validation for this model +func (m Enum) Validate() error { + return InKnownEnum.Validate(m) +} + +var ( + EnumOne Enum = "One" + EnumTwo Enum = "Two" + + // KnownEnum is the list of valid Enum + KnownEnum = []Enum{ + EnumOne, + EnumTwo, + } + // KnownEnumString is the list of valid Enum as string + KnownEnumString = []string{ + string(EnumOne), + string(EnumTwo), + } + + // InKnownEnum is an ozzo-validator for Enum + InKnownEnum = validation.In( + EnumOne, + EnumTwo, + ) +) diff --git a/pkg/generators/models/testdata/cases/required_nullable_combinations/expected/model_foo.go b/pkg/generators/models/testdata/cases/required_nullable_combinations/expected/model_foo.go new file mode 100644 index 0000000..216641b --- /dev/null +++ b/pkg/generators/models/testdata/cases/required_nullable_combinations/expected/model_foo.go @@ -0,0 +1,144 @@ +// Code generated by openapi-generator-go DO NOT EDIT. +// +// Source: +// +// Title: Test +// Version: 0.1.0 +package generatortest + +import ( + validation "github.com/go-ozzo/ozzo-validation/v4" +) + +// Foo is an object. +type Foo struct { + // ArrayRequiredNullable: + ArrayRequiredNullable []string `json:"array_required_nullable,omitempty" mapstructure:"array_required_nullable,omitempty"` + // Enum: + Enum Enum `json:"enum,omitempty" mapstructure:"enum,omitempty"` + // EnumNullable: + EnumNullable *Enum `json:"enum_nullable,omitempty" mapstructure:"enum_nullable,omitempty"` + // EnumRequired: + EnumRequired Enum `json:"enum_required" mapstructure:"enum_required"` + // EnumRequiredNullable: + EnumRequiredNullable *Enum `json:"enum_required_nullable" mapstructure:"enum_required_nullable"` + // String: + String string `json:"string,omitempty" mapstructure:"string,omitempty"` + // StringNullable: + StringNullable *string `json:"string_nullable,omitempty" mapstructure:"string_nullable,omitempty"` + // StringRequired: + StringRequired string `json:"string_required" mapstructure:"string_required"` + // StringRequiredNullable: + StringRequiredNullable *string `json:"string_required_nullable" mapstructure:"string_required_nullable"` +} + +// Validate implements basic validation for this model +func (m Foo) Validate() error { + return validation.Errors{ + "arrayRequiredNullable": validation.Validate( + m.ArrayRequiredNullable, validation.NilOrNotEmpty, validation.Length(1, 2), + ), + "enum": validation.Validate( + m.Enum, + ), + "enumNullable": validation.Validate( + m.EnumNullable, validation.NilOrNotEmpty, + ), + "enumRequired": validation.Validate( + m.EnumRequired, validation.Required, + ), + "enumRequiredNullable": validation.Validate( + m.EnumRequiredNullable, validation.NilOrNotEmpty, + ), + }.Filter() +} + +// GetArrayRequiredNullable returns the ArrayRequiredNullable property +func (m Foo) GetArrayRequiredNullable() []string { + return m.ArrayRequiredNullable +} + +// SetArrayRequiredNullable sets the ArrayRequiredNullable property +func (m *Foo) SetArrayRequiredNullable(val []string) { + m.ArrayRequiredNullable = val +} + +// GetEnum returns the Enum property +func (m Foo) GetEnum() Enum { + return m.Enum +} + +// SetEnum sets the Enum property +func (m *Foo) SetEnum(val Enum) { + m.Enum = val +} + +// GetEnumNullable returns the EnumNullable property +func (m Foo) GetEnumNullable() *Enum { + return m.EnumNullable +} + +// SetEnumNullable sets the EnumNullable property +func (m *Foo) SetEnumNullable(val *Enum) { + m.EnumNullable = val +} + +// GetEnumRequired returns the EnumRequired property +func (m Foo) GetEnumRequired() Enum { + return m.EnumRequired +} + +// SetEnumRequired sets the EnumRequired property +func (m *Foo) SetEnumRequired(val Enum) { + m.EnumRequired = val +} + +// GetEnumRequiredNullable returns the EnumRequiredNullable property +func (m Foo) GetEnumRequiredNullable() *Enum { + return m.EnumRequiredNullable +} + +// SetEnumRequiredNullable sets the EnumRequiredNullable property +func (m *Foo) SetEnumRequiredNullable(val *Enum) { + m.EnumRequiredNullable = val +} + +// GetString returns the String property +func (m Foo) GetString() string { + return m.String +} + +// SetString sets the String property +func (m *Foo) SetString(val string) { + m.String = val +} + +// GetStringNullable returns the StringNullable property +func (m Foo) GetStringNullable() *string { + return m.StringNullable +} + +// SetStringNullable sets the StringNullable property +func (m *Foo) SetStringNullable(val *string) { + m.StringNullable = val +} + +// GetStringRequired returns the StringRequired property +func (m Foo) GetStringRequired() string { + return m.StringRequired +} + +// SetStringRequired sets the StringRequired property +func (m *Foo) SetStringRequired(val string) { + m.StringRequired = val +} + +// GetStringRequiredNullable returns the StringRequiredNullable property +func (m Foo) GetStringRequiredNullable() *string { + return m.StringRequiredNullable +} + +// SetStringRequiredNullable sets the StringRequiredNullable property +func (m *Foo) SetStringRequiredNullable(val *string) { + m.StringRequiredNullable = val +} diff --git a/pkg/generators/models/testdata/cases/required_nullable_combinations/generated/model_enum.go b/pkg/generators/models/testdata/cases/required_nullable_combinations/generated/model_enum.go new file mode 100644 index 0000000..9036726 --- /dev/null +++ b/pkg/generators/models/testdata/cases/required_nullable_combinations/generated/model_enum.go @@ -0,0 +1,41 @@ +// Code generated by openapi-generator-go DO NOT EDIT. +// +// Source: +// +// Title: Test +// Version: 0.1.0 +package generatortest + +import ( + validation "github.com/go-ozzo/ozzo-validation/v4" +) + +// Enum is an enum. +type Enum string + +// Validate implements basic validation for this model +func (m Enum) Validate() error { + return InKnownEnum.Validate(m) +} + +var ( + EnumOne Enum = "One" + EnumTwo Enum = "Two" + + // KnownEnum is the list of valid Enum + KnownEnum = []Enum{ + EnumOne, + EnumTwo, + } + // KnownEnumString is the list of valid Enum as string + KnownEnumString = []string{ + string(EnumOne), + string(EnumTwo), + } + + // InKnownEnum is an ozzo-validator for Enum + InKnownEnum = validation.In( + EnumOne, + EnumTwo, + ) +) diff --git a/pkg/generators/models/testdata/cases/required_nullable_combinations/generated/model_foo.go b/pkg/generators/models/testdata/cases/required_nullable_combinations/generated/model_foo.go new file mode 100644 index 0000000..216641b --- /dev/null +++ b/pkg/generators/models/testdata/cases/required_nullable_combinations/generated/model_foo.go @@ -0,0 +1,144 @@ +// Code generated by openapi-generator-go DO NOT EDIT. +// +// Source: +// +// Title: Test +// Version: 0.1.0 +package generatortest + +import ( + validation "github.com/go-ozzo/ozzo-validation/v4" +) + +// Foo is an object. +type Foo struct { + // ArrayRequiredNullable: + ArrayRequiredNullable []string `json:"array_required_nullable,omitempty" mapstructure:"array_required_nullable,omitempty"` + // Enum: + Enum Enum `json:"enum,omitempty" mapstructure:"enum,omitempty"` + // EnumNullable: + EnumNullable *Enum `json:"enum_nullable,omitempty" mapstructure:"enum_nullable,omitempty"` + // EnumRequired: + EnumRequired Enum `json:"enum_required" mapstructure:"enum_required"` + // EnumRequiredNullable: + EnumRequiredNullable *Enum `json:"enum_required_nullable" mapstructure:"enum_required_nullable"` + // String: + String string `json:"string,omitempty" mapstructure:"string,omitempty"` + // StringNullable: + StringNullable *string `json:"string_nullable,omitempty" mapstructure:"string_nullable,omitempty"` + // StringRequired: + StringRequired string `json:"string_required" mapstructure:"string_required"` + // StringRequiredNullable: + StringRequiredNullable *string `json:"string_required_nullable" mapstructure:"string_required_nullable"` +} + +// Validate implements basic validation for this model +func (m Foo) Validate() error { + return validation.Errors{ + "arrayRequiredNullable": validation.Validate( + m.ArrayRequiredNullable, validation.NilOrNotEmpty, validation.Length(1, 2), + ), + "enum": validation.Validate( + m.Enum, + ), + "enumNullable": validation.Validate( + m.EnumNullable, validation.NilOrNotEmpty, + ), + "enumRequired": validation.Validate( + m.EnumRequired, validation.Required, + ), + "enumRequiredNullable": validation.Validate( + m.EnumRequiredNullable, validation.NilOrNotEmpty, + ), + }.Filter() +} + +// GetArrayRequiredNullable returns the ArrayRequiredNullable property +func (m Foo) GetArrayRequiredNullable() []string { + return m.ArrayRequiredNullable +} + +// SetArrayRequiredNullable sets the ArrayRequiredNullable property +func (m *Foo) SetArrayRequiredNullable(val []string) { + m.ArrayRequiredNullable = val +} + +// GetEnum returns the Enum property +func (m Foo) GetEnum() Enum { + return m.Enum +} + +// SetEnum sets the Enum property +func (m *Foo) SetEnum(val Enum) { + m.Enum = val +} + +// GetEnumNullable returns the EnumNullable property +func (m Foo) GetEnumNullable() *Enum { + return m.EnumNullable +} + +// SetEnumNullable sets the EnumNullable property +func (m *Foo) SetEnumNullable(val *Enum) { + m.EnumNullable = val +} + +// GetEnumRequired returns the EnumRequired property +func (m Foo) GetEnumRequired() Enum { + return m.EnumRequired +} + +// SetEnumRequired sets the EnumRequired property +func (m *Foo) SetEnumRequired(val Enum) { + m.EnumRequired = val +} + +// GetEnumRequiredNullable returns the EnumRequiredNullable property +func (m Foo) GetEnumRequiredNullable() *Enum { + return m.EnumRequiredNullable +} + +// SetEnumRequiredNullable sets the EnumRequiredNullable property +func (m *Foo) SetEnumRequiredNullable(val *Enum) { + m.EnumRequiredNullable = val +} + +// GetString returns the String property +func (m Foo) GetString() string { + return m.String +} + +// SetString sets the String property +func (m *Foo) SetString(val string) { + m.String = val +} + +// GetStringNullable returns the StringNullable property +func (m Foo) GetStringNullable() *string { + return m.StringNullable +} + +// SetStringNullable sets the StringNullable property +func (m *Foo) SetStringNullable(val *string) { + m.StringNullable = val +} + +// GetStringRequired returns the StringRequired property +func (m Foo) GetStringRequired() string { + return m.StringRequired +} + +// SetStringRequired sets the StringRequired property +func (m *Foo) SetStringRequired(val string) { + m.StringRequired = val +} + +// GetStringRequiredNullable returns the StringRequiredNullable property +func (m Foo) GetStringRequiredNullable() *string { + return m.StringRequiredNullable +} + +// SetStringRequiredNullable sets the StringRequiredNullable property +func (m *Foo) SetStringRequiredNullable(val *string) { + m.StringRequiredNullable = val +} diff --git a/pkg/generators/models/testdata/cases/update.sh b/pkg/generators/models/testdata/cases/update.sh index 9b0ccaf..4556f05 100755 --- a/pkg/generators/models/testdata/cases/update.sh +++ b/pkg/generators/models/testdata/cases/update.sh @@ -5,7 +5,7 @@ for case in $(ls); do if [ -d $case ]; then echo "updating $case" cd $case - rm -r generated + rm -rf generated openapi-generator-go generate -o generated --spec api.yaml --package-name generatortest --log-level=info rm generated/router.go rm -rf expected diff --git a/pkg/generators/models/testdata/cases/validation/api.yaml b/pkg/generators/models/testdata/cases/validation/api.yaml index 953607b..c30a97e 100644 --- a/pkg/generators/models/testdata/cases/validation/api.yaml +++ b/pkg/generators/models/testdata/cases/validation/api.yaml @@ -26,6 +26,9 @@ components: type: number minimum: 18 maximum: 120 + children: + type: integer + minimum: 0 gender: $ref: "#/components/schemas/Gender" secondGender: @@ -38,7 +41,7 @@ components: type: array items: $ref: "#/components/schemas/Color" - minLength: 1 + minItems: 1 email: type: string format: "email" @@ -104,6 +107,10 @@ components: minLength: 2 number: type: integer + subNumber: + type: integer + nullable: true + minimum: 1 name: type: string nullable: true diff --git a/pkg/generators/models/testdata/cases/validation/expected/model_address.go b/pkg/generators/models/testdata/cases/validation/expected/model_address.go index b328356..61d1486 100644 --- a/pkg/generators/models/testdata/cases/validation/expected/model_address.go +++ b/pkg/generators/models/testdata/cases/validation/expected/model_address.go @@ -18,17 +18,22 @@ type Address struct { Number int32 `json:"number" mapstructure:"number"` // Street: Street string `json:"street" mapstructure:"street"` + // SubNumber: + SubNumber *int32 `json:"subNumber,omitempty" mapstructure:"subNumber,omitempty"` } // Validate implements basic validation for this model func (m Address) Validate() error { return validation.Errors{ "name": validation.Validate( - m.Name, validation.Length(2, 0), + m.Name, validation.NilOrNotEmpty, validation.Length(2, 0), ), "street": validation.Validate( m.Street, validation.Required, validation.Length(2, 0), ), + "subNumber": validation.Validate( + m.SubNumber, validation.NilOrNotEmpty, validation.Min(int32(1)), + ), }.Filter() } @@ -61,3 +66,13 @@ func (m Address) GetStreet() string { func (m *Address) SetStreet(val string) { m.Street = val } + +// GetSubNumber returns the SubNumber property +func (m Address) GetSubNumber() *int32 { + return m.SubNumber +} + +// SetSubNumber sets the SubNumber property +func (m *Address) SetSubNumber(val *int32) { + m.SubNumber = val +} diff --git a/pkg/generators/models/testdata/cases/validation/expected/model_person.go b/pkg/generators/models/testdata/cases/validation/expected/model_person.go index 4bb0bf6..8bb1883 100644 --- a/pkg/generators/models/testdata/cases/validation/expected/model_person.go +++ b/pkg/generators/models/testdata/cases/validation/expected/model_person.go @@ -24,6 +24,8 @@ type Person struct { Age float32 `json:"age,omitempty" mapstructure:"age,omitempty"` // Base64: Base64 string `json:"base64,omitempty" mapstructure:"base64,omitempty"` + // Children: + Children int32 `json:"children,omitempty" mapstructure:"children,omitempty"` // Cron: Cron Cron `json:"cron" mapstructure:"cron"` // Date: @@ -51,7 +53,7 @@ type Person struct { // RequestURI: RequestURI string `json:"requestURI,omitempty" mapstructure:"requestURI,omitempty"` // SecondGender: - SecondGender *Gender `json:"secondGender,omitempty" mapstructure:"secondGender,omitempty"` + SecondGender Gender `json:"secondGender,omitempty" mapstructure:"secondGender,omitempty"` // Uri: Uri string `json:"uri,omitempty" mapstructure:"uri,omitempty"` // Url: @@ -72,8 +74,11 @@ func (m Person) Validate() error { "base64": validation.Validate( m.Base64, is.Base64, ), + "children": validation.Validate( + m.Children, validation.Min(int32(0)), + ), "cron": validation.Validate( - m.Cron, validation.NotNil, + m.Cron, validation.Required, ), "date": validation.Validate( m.Date, validation.Date("2006-01-02"), @@ -156,6 +161,16 @@ func (m *Person) SetBase64(val string) { m.Base64 = val } +// GetChildren returns the Children property +func (m Person) GetChildren() int32 { + return m.Children +} + +// SetChildren sets the Children property +func (m *Person) SetChildren(val int32) { + m.Children = val +} + // GetCron returns the Cron property func (m Person) GetCron() Cron { return m.Cron @@ -287,12 +302,12 @@ func (m *Person) SetRequestURI(val string) { } // GetSecondGender returns the SecondGender property -func (m Person) GetSecondGender() *Gender { +func (m Person) GetSecondGender() Gender { return m.SecondGender } // SetSecondGender sets the SecondGender property -func (m *Person) SetSecondGender(val *Gender) { +func (m *Person) SetSecondGender(val Gender) { m.SecondGender = val } diff --git a/pkg/generators/models/testdata/cases/validation/generated/model_address.go b/pkg/generators/models/testdata/cases/validation/generated/model_address.go index b328356..61d1486 100644 --- a/pkg/generators/models/testdata/cases/validation/generated/model_address.go +++ b/pkg/generators/models/testdata/cases/validation/generated/model_address.go @@ -18,17 +18,22 @@ type Address struct { Number int32 `json:"number" mapstructure:"number"` // Street: Street string `json:"street" mapstructure:"street"` + // SubNumber: + SubNumber *int32 `json:"subNumber,omitempty" mapstructure:"subNumber,omitempty"` } // Validate implements basic validation for this model func (m Address) Validate() error { return validation.Errors{ "name": validation.Validate( - m.Name, validation.Length(2, 0), + m.Name, validation.NilOrNotEmpty, validation.Length(2, 0), ), "street": validation.Validate( m.Street, validation.Required, validation.Length(2, 0), ), + "subNumber": validation.Validate( + m.SubNumber, validation.NilOrNotEmpty, validation.Min(int32(1)), + ), }.Filter() } @@ -61,3 +66,13 @@ func (m Address) GetStreet() string { func (m *Address) SetStreet(val string) { m.Street = val } + +// GetSubNumber returns the SubNumber property +func (m Address) GetSubNumber() *int32 { + return m.SubNumber +} + +// SetSubNumber sets the SubNumber property +func (m *Address) SetSubNumber(val *int32) { + m.SubNumber = val +} diff --git a/pkg/generators/models/testdata/cases/validation/generated/model_person.go b/pkg/generators/models/testdata/cases/validation/generated/model_person.go index 4bb0bf6..8bb1883 100644 --- a/pkg/generators/models/testdata/cases/validation/generated/model_person.go +++ b/pkg/generators/models/testdata/cases/validation/generated/model_person.go @@ -24,6 +24,8 @@ type Person struct { Age float32 `json:"age,omitempty" mapstructure:"age,omitempty"` // Base64: Base64 string `json:"base64,omitempty" mapstructure:"base64,omitempty"` + // Children: + Children int32 `json:"children,omitempty" mapstructure:"children,omitempty"` // Cron: Cron Cron `json:"cron" mapstructure:"cron"` // Date: @@ -51,7 +53,7 @@ type Person struct { // RequestURI: RequestURI string `json:"requestURI,omitempty" mapstructure:"requestURI,omitempty"` // SecondGender: - SecondGender *Gender `json:"secondGender,omitempty" mapstructure:"secondGender,omitempty"` + SecondGender Gender `json:"secondGender,omitempty" mapstructure:"secondGender,omitempty"` // Uri: Uri string `json:"uri,omitempty" mapstructure:"uri,omitempty"` // Url: @@ -72,8 +74,11 @@ func (m Person) Validate() error { "base64": validation.Validate( m.Base64, is.Base64, ), + "children": validation.Validate( + m.Children, validation.Min(int32(0)), + ), "cron": validation.Validate( - m.Cron, validation.NotNil, + m.Cron, validation.Required, ), "date": validation.Validate( m.Date, validation.Date("2006-01-02"), @@ -156,6 +161,16 @@ func (m *Person) SetBase64(val string) { m.Base64 = val } +// GetChildren returns the Children property +func (m Person) GetChildren() int32 { + return m.Children +} + +// SetChildren sets the Children property +func (m *Person) SetChildren(val int32) { + m.Children = val +} + // GetCron returns the Cron property func (m Person) GetCron() Cron { return m.Cron @@ -287,12 +302,12 @@ func (m *Person) SetRequestURI(val string) { } // GetSecondGender returns the SecondGender property -func (m Person) GetSecondGender() *Gender { +func (m Person) GetSecondGender() Gender { return m.SecondGender } // SetSecondGender sets the SecondGender property -func (m *Person) SetSecondGender(val *Gender) { +func (m *Person) SetSecondGender(val Gender) { m.SecondGender = val } diff --git a/pkg/generators/models/validations.go b/pkg/generators/models/validations.go new file mode 100644 index 0000000..57b2992 --- /dev/null +++ b/pkg/generators/models/validations.go @@ -0,0 +1,122 @@ +package models + +import ( + "bytes" + "embed" + "encoding/csv" + + "github.com/mitchellh/mapstructure" +) + +//go:embed validations/*_validation.csv +var dataFolder embed.FS + +func load() map[string][]*DerivedRule { + rulesFiles, err := dataFolder.ReadDir("validations") + if err != nil { + panic(err) + } + + rawRules := make(map[string][]map[string]bool) + for _, file := range rulesFiles { + rulesData, err := dataFolder.ReadFile("validations/" + file.Name()) + if err != nil { + panic(err) + } + + reader := csv.NewReader(bytes.NewBuffer(rulesData)) + reader.TrimLeadingSpace = true + records, err := reader.ReadAll() + if err != nil { + panic(err) + } + + // Header + header := records[0] + records = records[1:] + for _, row := range records { + ozzoValidation := row[0] + rule := make(map[string]bool) + for idx, col := range header[1:] { + value := row[idx+1] + // Do not set empty values + if value == "" { + continue + } + rule[col] = row[idx+1] == "y" + } + rawRules[ozzoValidation] = append(rawRules[ozzoValidation], rule) + } + } + + rulesByType := make(map[string][]*DerivedRule) + decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{ + ErrorUnused: true, + TagName: "json", + Result: &rulesByType, + }) + if err != nil { + panic(err) + } + err = decoder.Decode(rawRules) + if err != nil { + panic(err) + } + + for _, rules := range rulesByType { + for _, rule := range rules { + rule.MakeConsistent() + } + } + + return rulesByType +} + +var DerivedRulesByValidationType = load() + +type DerivedRule struct { + //Type related + IsStruct bool `json:"struct,omitempty"` + IsArray bool `json:"array,omitempty"` + IsMap bool `json:"map,omitempty"` + IsNumber bool `json:"number,omitempty"` + IsInteger bool `json:"integer,omitempty"` + IsString bool `json:"string,omitempty"` + IsEnum bool `json:"enum,omitempty"` + IsEnumWithNull *bool `json:"enum_with_null,omitempty"` + IsEnumWithZero *bool `json:"enum_with_zero,omitempty"` + IsPtr bool `json:"ptr,omitempty"` + + // Format related + HasFormat *bool `json:"format,omitempty"` + HasPattern *bool `json:"pattern,omitempty"` + + // Validation related + IsRequired *bool `json:"required,omitempty"` + + HasMin *bool `json:"min,omitempty"` + HasMax *bool `json:"max,omitempty"` + IsMinGreaterThanZero *bool `json:"min_gt_zero,omitempty"` + IsMaxLessThanZero *bool `json:"max_lt_zero,omitempty"` + + HasMinLength *bool `json:"min_length,omitempty"` + HasMaxLength *bool `json:"max_length,omitempty"` + + HasMinItems *bool `json:"min_items,omitempty"` + HasMaxItems *bool `json:"max_items,omitempty"` + IsUniqueItems *bool `json:"unique_items,omitempty"` + + HasMinProps *bool `json:"min_props,omitempty"` + HasMaxProps *bool `json:"max_props,omitempty"` +} + +func (v *DerivedRule) MakeConsistent() { + if v.IsMinGreaterThanZero != nil && *v.IsMinGreaterThanZero { + v.HasMin = new(bool) + *v.HasMin = true + } + if v.IsMaxLessThanZero != nil && *v.IsMaxLessThanZero { + v.HasMax = new(bool) + *v.HasMax = true + } +} diff --git a/pkg/generators/models/validations/array_validation.csv b/pkg/generators/models/validations/array_validation.csv new file mode 100644 index 0000000..e0454d9 --- /dev/null +++ b/pkg/generators/models/validations/array_validation.csv @@ -0,0 +1,4 @@ +validation,array,required,min_items +NilOrNotEmpty,y,n,y +NotNil,y,y,n +Required,y,y,y \ No newline at end of file diff --git a/pkg/generators/models/validations/enum_validation.csv b/pkg/generators/models/validations/enum_validation.csv new file mode 100644 index 0000000..cab181e --- /dev/null +++ b/pkg/generators/models/validations/enum_validation.csv @@ -0,0 +1,6 @@ +validation,enum,ptr,required,enum_with_null,enum_with_zero +NilOrNotEmpty,y,y,,y,n +NilOrNotEmpty,y,y,n,n,n +NotNil,y,y,,n,y +Required,y,n,y,,n +Required,y,y,y,n,n \ No newline at end of file diff --git a/pkg/generators/models/validations/integer_validation.csv b/pkg/generators/models/validations/integer_validation.csv new file mode 100644 index 0000000..923f6b2 --- /dev/null +++ b/pkg/generators/models/validations/integer_validation.csv @@ -0,0 +1,5 @@ +validation,integer,ptr,required,min_gt_zero,max_lt_zero +NilOrNotEmpty,y,y,,n,y +NilOrNotEmpty,y,y,,y,n +Required,y,n,y,n,y +Required,y,n,y,y,n \ No newline at end of file diff --git a/pkg/generators/models/validations/map_validation.csv b/pkg/generators/models/validations/map_validation.csv new file mode 100644 index 0000000..869c20a --- /dev/null +++ b/pkg/generators/models/validations/map_validation.csv @@ -0,0 +1,4 @@ +validation,map,required,min_props +NilOrNotEmpty,y,n,y +NotNil,y,y,n +Required,y,y,y \ No newline at end of file diff --git a/pkg/generators/models/validations/number_validation.csv b/pkg/generators/models/validations/number_validation.csv new file mode 100644 index 0000000..a5cb696 --- /dev/null +++ b/pkg/generators/models/validations/number_validation.csv @@ -0,0 +1,5 @@ +validation,number,ptr,required,min_gt_zero,max_lt_zero +NilOrNotEmpty,y,y,,n,y +NilOrNotEmpty,y,y,,y,n +Required,y,n,y,n,y +Required,y,n,y,y,n \ No newline at end of file diff --git a/pkg/generators/models/validations/string_validation.csv b/pkg/generators/models/validations/string_validation.csv new file mode 100644 index 0000000..55b136d --- /dev/null +++ b/pkg/generators/models/validations/string_validation.csv @@ -0,0 +1,8 @@ +validation,string,ptr,required,min_length,format,pattern +NilOrNotEmpty,y,y,n,y,, +NilOrNotEmpty,y,y,y,,,y +NilOrNotEmpty,y,y,y,,y, +NilOrNotEmpty,y,y,y,y,, +Required,y,n,y,,,y +Required,y,n,y,,y, +Required,y,n,y,y,, \ No newline at end of file diff --git a/pkg/generators/models/validations/struct_validation.csv b/pkg/generators/models/validations/struct_validation.csv new file mode 100644 index 0000000..763d29b --- /dev/null +++ b/pkg/generators/models/validations/struct_validation.csv @@ -0,0 +1,2 @@ +validation,struct,ptr,required +NotNil,y,y,y \ No newline at end of file diff --git a/pkg/generators/templates/ternary.go b/pkg/generators/templates/ternary.go new file mode 100644 index 0000000..4b63e84 --- /dev/null +++ b/pkg/generators/templates/ternary.go @@ -0,0 +1,9 @@ +package templates + +// Ternary returns the first value if the last value is true, otherwise returns the second value. +func Ternary(vt any, vf any, v bool) any { + if v { + return vt + } + return vf +}