From d9e2ce9a950a7a95ddf9d74b75aa21f60d5555e8 Mon Sep 17 00:00:00 2001 From: Cory Bennett Date: Sat, 20 Aug 2016 15:18:51 -0700 Subject: [PATCH] print comments for each type we write so golint will pass on the generated files --- slipscheme.go | 64 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 19 deletions(-) diff --git a/slipscheme.go b/slipscheme.go index 0ee1dac..ec52fce 100644 --- a/slipscheme.go +++ b/slipscheme.go @@ -15,16 +15,16 @@ import ( // Schema represents JSON schema. type Schema struct { - Title string `json:"title"` - ID string `json:"id"` - Type SchemaType `json:"type"` - Description string `json:"description"` - Definitions map[string]*Schema `json:"definitions"` - Properties map[string]*Schema `json:"properties"` - PatternProperties map[string]*Schema `json:"patternProperties"` - Ref string `json:"$ref"` - Items *Schema `json:"items"` - Root *Schema + Title string `json:"title,omitempty"` + ID string `json:"id,omitempty"` + Type SchemaType `json:"type,omitempty"` + Description string `json:"description,omitempty"` + Definitions map[string]*Schema `json:"definitions,omitempty"` + Properties map[string]*Schema `json:"properties,omitempty"` + PatternProperties map[string]*Schema `json:"patternProperties,omitempty"` + Ref string `json:"$ref,omitempty"` + Items *Schema `json:"items,omitempty"` + Root *Schema `json:"-"` } // Name will attempt to determine the name of the Schema element using @@ -86,6 +86,20 @@ func (s *SchemaType) UnmarshalJSON(b []byte) error { return fmt.Errorf("Unknown schema type \"%s\"", schemaType) } +func (s *SchemaType) MarshalJSON() ([]byte, error) { + switch *s { + case ANY: return []byte("\"object\""), nil + case ARRAY: return []byte("\"array\""), nil + case BOOLEAN: return []byte("\"boolean\""), nil + case INTEGER: return []byte("\"integer\""), nil + case NUMBER: return []byte("\"number\""), nil + case NULL: return []byte("\"null\""), nil + case OBJECT: return []byte("\"object\""), nil + case STRING: return []byte("\"string\""), nil + } + return nil, fmt.Errorf("Unknown Schema Type: %#v", s) +} + func main() { outputDir := flag.String("dir", ".", "output directory for go files") pkgName := flag.String("pkg", "main", "package namespace for go files") @@ -211,20 +225,31 @@ func camelCase(name string) string { }, name), ) caseName = strings.Replace(caseName, " ", "", -1) - - if strings.HasSuffix(caseName, "Id") { - return strings.TrimSuffix(caseName, "Id") + "ID" - } else if strings.HasSuffix(caseName, "Url") { - return strings.TrimSuffix(caseName, "Url") + "URL" + + for _, suffix := range []string{"Id", "Url", "Json", "Xml"} { + if strings.HasSuffix(caseName, suffix) { + return strings.TrimSuffix(caseName, suffix) + strings.ToUpper(suffix) + } } + + for _, prefix := range []string{"Url", "Json", "Xml"} { + if strings.HasPrefix(caseName, prefix) { + return strings.ToUpper(prefix) + strings.TrimPrefix(caseName, prefix) + } + } + return caseName } func (s *SchemaProcessor) processSchema(schema *Schema) (typeName string, err error) { + prettyPrint, err := json.MarshalIndent(schema, "// ", " ") + if err != nil { + return "", err + } if schema.Type == OBJECT { typeName = camelCase(schema.Name()) if schema.Properties != nil { - typeData := fmt.Sprintf("type %s struct {\n", typeName) + typeData := fmt.Sprintf("// %s defined from schema:\n// %s\ntype %s struct {\n", typeName, prettyPrint, typeName) keys := []string{} for k := range schema.Properties { keys = append(keys,k) @@ -259,7 +284,7 @@ func (s *SchemaProcessor) processSchema(schema *Schema) (typeName string, err er // verify subTypeName is not a simple type if strings.Title(subTypeName) == subTypeName { typeName = strings.TrimPrefix(fmt.Sprintf("%sMap", subTypeName), "*") - typeData := fmt.Sprintf("type %s map[string]%s\n\n", typeName, subTypeName) + typeData := fmt.Sprintf("// %s defined from schema:\n// %s\ntype %s map[string]%s\n\n", typeName, prettyPrint, typeName, subTypeName) if err := s.writeGoCode(typeName, typeData); err != nil { return "", err } @@ -280,7 +305,7 @@ func (s *SchemaProcessor) processSchema(schema *Schema) (typeName string, err er typeName = fmt.Sprintf("%ss", subTypeName) } typeName = strings.TrimPrefix(typeName, "*") - typeData := fmt.Sprintf("type %s []%s\n\n", typeName, subTypeName) + typeData := fmt.Sprintf("// %s defined from schema:\n// %s\ntype %s []%s\n\n", typeName, prettyPrint, typeName, subTypeName) if err := s.writeGoCode(typeName, typeData); err != nil { return "", err } @@ -346,7 +371,8 @@ func (s *SchemaProcessor) writeGoCode(typeName, code string) error { // This Code is Generated by SlipScheme Project: // https://github.com/coryb/slipscheme // -// Generated with command: %s +// Generated with command: +// %s ///////////////////////////////////////////////////////////////////////// // DO NOT EDIT // /////////////////////////////////////////////////////////////////////////