Skip to content

Commit

Permalink
print comments for each type we write so golint will pass on the
Browse files Browse the repository at this point in the history
generated files
  • Loading branch information
coryb committed Aug 20, 2016
1 parent 5c13244 commit d9e2ce9
Showing 1 changed file with 45 additions and 19 deletions.
64 changes: 45 additions & 19 deletions slipscheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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 //
/////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit d9e2ce9

Please sign in to comment.