Skip to content

Commit f6fa191

Browse files
committed
add flag to enable/disable comment generation
1 parent d5adec6 commit f6fa191

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

slipscheme.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ func main() {
114114
overwrite := flag.Bool("overwrite", false, "force overwriting existing go files")
115115
stdout := flag.Bool("stdout", false, "print go code to stdout rather than files")
116116
format := flag.Bool("fmt", true, "pass code through gofmt")
117+
comments := flag.Bool("comments", true, "enable/disable print comments")
118+
117119
flag.Parse()
118120

119121
processor := &SchemaProcessor{
@@ -122,6 +124,7 @@ func main() {
122124
Overwrite: *overwrite,
123125
Stdout: *stdout,
124126
Fmt: *format,
127+
Comment: *comments,
125128
}
126129

127130
err := processor.Process(flag.Args())
@@ -137,6 +140,7 @@ type SchemaProcessor struct {
137140
Overwrite bool
138141
Stdout bool
139142
Fmt bool
143+
Comment bool
140144
processed map[string]bool
141145
}
142146

@@ -249,15 +253,19 @@ func camelCase(name string) string {
249253
return caseName
250254
}
251255

252-
func (s *SchemaProcessor) processSchema(schema *Schema) (typeName string, err error) {
253-
prettyPrint, err := json.MarshalIndent(schema, "// ", " ")
254-
if err != nil {
255-
return "", err
256+
func (s *SchemaProcessor) structComment(schema *Schema, typeName string) string {
257+
if !s.Comment {
258+
return ""
256259
}
260+
prettySchema, _ := json.MarshalIndent(schema, "// ", " ")
261+
return fmt.Sprintf("// %s defined from schema:\n// %s\n", typeName, prettySchema)
262+
}
263+
264+
func (s *SchemaProcessor) processSchema(schema *Schema) (typeName string, err error) {
257265
if schema.Type == OBJECT {
258266
typeName = camelCase(schema.Name())
259267
if schema.Properties != nil {
260-
typeData := fmt.Sprintf("// %s defined from schema:\n// %s\ntype %s struct {\n", typeName, prettyPrint, typeName)
268+
typeData := fmt.Sprintf("%stype %s struct {\n", s.structComment(schema, typeName), typeName)
261269
keys := []string{}
262270
for k := range schema.Properties {
263271
keys = append(keys, k)
@@ -292,7 +300,7 @@ func (s *SchemaProcessor) processSchema(schema *Schema) (typeName string, err er
292300
// verify subTypeName is not a simple type
293301
if strings.Title(subTypeName) == subTypeName {
294302
typeName = strings.TrimPrefix(fmt.Sprintf("%sMap", subTypeName), "*")
295-
typeData := fmt.Sprintf("// %s defined from schema:\n// %s\ntype %s map[string]%s\n\n", typeName, prettyPrint, typeName, subTypeName)
303+
typeData := fmt.Sprintf("%stype %s map[string]%s\n\n", s.structComment(schema, typeName), typeName, subTypeName)
296304
if err := s.writeGoCode(typeName, typeData); err != nil {
297305
return "", err
298306
}
@@ -313,7 +321,7 @@ func (s *SchemaProcessor) processSchema(schema *Schema) (typeName string, err er
313321
typeName = fmt.Sprintf("%ss", subTypeName)
314322
}
315323
typeName = strings.TrimPrefix(typeName, "*")
316-
typeData := fmt.Sprintf("// %s defined from schema:\n// %s\ntype %s []%s\n\n", typeName, prettyPrint, typeName, subTypeName)
324+
typeData := fmt.Sprintf("%stype %s []%s\n\n", s.structComment(schema, typeName), typeName, subTypeName)
317325
if err := s.writeGoCode(typeName, typeData); err != nil {
318326
return "", err
319327
}

0 commit comments

Comments
 (0)