// Generate generates the constants for given schema/model func Generate(s *schema.Schema) ([]byte, error) { temp := template.New("constants.tmpl").Funcs(common.TemplateFuncs) if _, err := temp.Parse(ConstantsTemplate); err != nil { return nil, err } data := struct { Schema *schema.Schema }{ Schema: s, } var buf bytes.Buffer if err := temp.ExecuteTemplate(&buf, "constants.tmpl", data); err != nil { return nil, err } return writers.Clear(buf) }
// Generate generates and writes the errors of the schema func (g *Generator) Generate(context *common.Context, s *schema.Schema) ([]common.Output, error) { temp := template.New("errors.tmpl").Funcs(context.TemplateFuncs) if _, err := temp.Parse(ErrorsTemplate); err != nil { return nil, err } outputs := make([]common.Output, 0) for _, def := range common.SortedObjectSchemas(s.Definitions) { data := struct { Schema *schema.Schema }{ Schema: def, } var buf bytes.Buffer if err := temp.ExecuteTemplate(&buf, "errors.tmpl", data); err != nil { return nil, err } f, err := writers.Clear(buf) if err != nil { return nil, err } path := fmt.Sprintf( "%s/%s.go", context.Config.Target, strings.ToLower(def.Title), ) outputs = append(outputs, common.Output{ Content: f, Path: path, }) } return outputs, nil }
// GenerateModelStatements generates the CRUD statements for the model struct func GenerateModelStatements(context *common.Context, settings schema.Generator, s *schema.Schema) ([]byte, error) { packageLine, err := GeneratePackage(context, settings, s) if err != nil { return nil, err } createStatements, err := GenerateCreate(context, settings, s) if err != nil { return nil, err } updateStatements, err := GenerateUpdate(context, settings, s) if err != nil { return nil, err } deleteStatements, err := GenerateDelete(context, settings, s) if err != nil { return nil, err } selectStatements, err := GenerateSelect(context, settings, s) if err != nil { return nil, err } tableName, err := GenerateTableName(context, settings, s) if err != nil { return nil, err } var buf bytes.Buffer buf.Write(packageLine) buf.Write(createStatements) buf.Write(updateStatements) buf.Write(deleteStatements) buf.Write(selectStatements) buf.Write(tableName) return writers.Clear(buf) }
// GenerateModel generates the model itself func GenerateModel(s *schema.Schema) ([]byte, error) { packageLine, err := GeneratePackage(s) if err != nil { return nil, err } consts, err := constants.Generate(s) if err != nil { return nil, err } schema, err := GenerateSchema(s) if err != nil { return nil, err } constructor, err := constructors.Generate(s) if err != nil { return nil, err } validators, err := validators.Generate(s) if err != nil { return nil, err } var buf bytes.Buffer buf.Write(packageLine) buf.Write(consts) buf.Write(schema) buf.Write(constructor) if validators != nil { buf.Write(validators) } return writers.Clear(buf) }
// GenerateSchema generates the schema. func GenerateSchema(s *schema.Schema) ([]byte, error) { temp := template.New("schema.tmpl") temp.Funcs(schema.Helpers) _, err := temp.Parse(StructTemplate) if err != nil { return nil, err } var buf bytes.Buffer data := struct { Schema *schema.Schema }{ Schema: s, } err = temp.ExecuteTemplate(&buf, "schema.tmpl", data) if err != nil { return nil, err } return writers.Clear(buf) }
// Generate generates the validators for the the given schema func Generate(s *schema.Schema) ([]byte, error) { validators := make([]string, 0) // schemaName := p.Title schemaFirstChar := stringext.Pointerize(s.Title) for k, property := range s.Properties { key := stringext.DepunctWithInitialUpper(k) switch property.Type { case "string": if property.MinLength != 0 { validator := fmt.Sprintf("govalidator.MinLength(%s.%s, %d)", schemaFirstChar, key, property.MinLength) validators = append(validators, validator) } if property.MaxLength != 0 { validator := fmt.Sprintf("govalidator.MaxLength(%s.%s, %d)", schemaFirstChar, key, property.MaxLength) validators = append(validators, validator) } if property.Pattern != "" { validator := fmt.Sprintf("govalidator.Pattern(%s.%s, \"%s\")", schemaFirstChar, key, property.Pattern) validators = append(validators, validator) } if len(property.Enum) > 0 { generatedEnums := make([]string, len(property.Enum)) for i, enum := range property.Enum { k := stringext.DepunctWithInitialUpper(key) generatedEnums[i] = s.Title + k + "." + stringext.DepunctWithInitialUpper(enum) } validator := fmt.Sprintf("govalidator.OneOf(%s.%s, []string{\n%s,\n})", schemaFirstChar, key, strings.Join(generatedEnums, ",\n")) validators = append(validators, validator) } // TODO impplement this one switch property.Format { case "date-time": // _, err := time.Parse(time.RFC3339, s) validator := fmt.Sprintf("govalidator.Date(%s.%s)", schemaFirstChar, key) validators = append(validators, validator) } case "integer", "number": // todo implement exclusive min/max if property.Minimum != 0 { validator := fmt.Sprintf("govalidator.Min(float64(%s.%s), %f)", schemaFirstChar, key, property.Minimum) validators = append(validators, validator) } if property.Maximum != 0 { validator := fmt.Sprintf("govalidator.Max(float64(%s.%s), %f)", schemaFirstChar, key, property.Maximum) validators = append(validators, validator) } // multipleOf: if property.MultipleOf != 0 { validator := fmt.Sprintf("govalidator.MultipleOf(float64(%s.%s), %f)", schemaFirstChar, key, property.MultipleOf) validators = append(validators, validator) } } } if len(validators) == 0 { return nil, nil } templ := ` // Validate validates the %s struct func (%s *%s) Validate() error { return govalidator.NewMulti(%s).Validate() }` sslice := sort.StringSlice(validators) sslice.Sort() res := fmt.Sprintf( templ, s.Title, stringext.Pointerize(s.Title), s.Title, strings.Join(sslice, ",\n"), ) return writers.Clear(*bytes.NewBufferString(res)) }