Line comment support #18

This commit is contained in:
Dan Markham 2019-04-23 15:13:34 -07:00
parent 325a6e167c
commit ccf4a55cf2
No known key found for this signature in database
GPG Key ID: 7994037517927D77
2 changed files with 9 additions and 3 deletions

View File

@ -1438,7 +1438,7 @@ func runGoldenTest(t *testing.T, test Golden, generateJSON, generateYAML, genera
if len(tokens) != 3 { if len(tokens) != 3 {
t.Fatalf("%s: need type declaration on first line", test.name) t.Fatalf("%s: need type declaration on first line", test.name)
} }
g.generate(tokens[1], generateJSON, generateYAML, generateSQL, generateText, "noop", trimPrefix, prefix) g.generate(tokens[1], generateJSON, generateYAML, generateSQL, generateText, "noop", trimPrefix, prefix, false)
got := string(g.format()) got := string(g.format())
if got != test.output { if got != test.output {
// dmp := diffmatchpatch.New() // dmp := diffmatchpatch.New()

View File

@ -53,6 +53,7 @@ var (
transformMethod = flag.String("transform", "noop", "enum item name transformation method. Default: noop") transformMethod = flag.String("transform", "noop", "enum item name transformation method. Default: noop")
trimPrefix = flag.String("trimprefix", "", "transform each item name by removing a prefix. Default: \"\"") trimPrefix = flag.String("trimprefix", "", "transform each item name by removing a prefix. Default: \"\"")
addPrefix = flag.String("addprefix", "", "transform each item name by adding a prefix. Default: \"\"") addPrefix = flag.String("addprefix", "", "transform each item name by adding a prefix. Default: \"\"")
linecomment = flag.Bool("linecomment", false, "use line comment text as printed text when present")
) )
var comments arrayFlags var comments arrayFlags
@ -127,7 +128,7 @@ func main() {
// Run generate for each type. // Run generate for each type.
for _, typeName := range typs { for _, typeName := range typs {
g.generate(typeName, *json, *yaml, *sql, *text, *transformMethod, *trimPrefix, *addPrefix) g.generate(typeName, *json, *yaml, *sql, *text, *transformMethod, *trimPrefix, *addPrefix, *linecomment)
} }
// Format the output. // Format the output.
@ -397,9 +398,10 @@ func (g *Generator) prefixValueNames(values []Value, prefix string) {
// generate produces the String method for the named type. // generate produces the String method for the named type.
func (g *Generator) generate(typeName string, includeJSON, includeYAML, includeSQL, includeText bool, func (g *Generator) generate(typeName string, includeJSON, includeYAML, includeSQL, includeText bool,
transformMethod string, trimPrefix string, addPrefix string) { transformMethod string, trimPrefix string, addPrefix string, lineComment bool) {
values := make([]Value, 0, 100) values := make([]Value, 0, 100)
for _, file := range g.pkg.files { for _, file := range g.pkg.files {
file.lineComment = lineComment
// Set the state for this run of the walker. // Set the state for this run of the walker.
file.typeName = typeName file.typeName = typeName
file.values = nil file.values = nil
@ -603,6 +605,10 @@ func (f *File) genDecl(node ast.Node) bool {
signed: info&types.IsUnsigned == 0, signed: info&types.IsUnsigned == 0,
str: value.String(), str: value.String(),
} }
if c := vspec.Comment; f.lineComment && c != nil && len(c.List) == 1 {
v.name = strings.TrimSpace(c.Text())
}
f.values = append(f.values, v) f.values = append(f.values, v)
} }
} }