summaryrefslogtreecommitdiffstats
path: root/cmd/binapi-generator
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/binapi-generator')
-rw-r--r--cmd/binapi-generator/generator.go39
1 files changed, 28 insertions, 11 deletions
diff --git a/cmd/binapi-generator/generator.go b/cmd/binapi-generator/generator.go
index d8420f9..94ee177 100644
--- a/cmd/binapi-generator/generator.go
+++ b/cmd/binapi-generator/generator.go
@@ -50,7 +50,8 @@ const (
// context is a structure storing details of a particular code generation task
type context struct {
inputFile string // file with input JSON data
- inputBuff *bytes.Buffer // contents of the input file
+ inputData []byte // contents of the input file
+ inputBuff *bytes.Buffer // contents of the input file currently being read
inputLine int // currently processed line in the input file
outputFile string // file with output data
packageName string // name of the Go package being generated
@@ -117,14 +118,13 @@ func generateFromFile(inputFile, outputDir string) error {
return err
}
// read the file
- inputData, err := readFile(inputFile)
+ ctx.inputData, err = readFile(inputFile)
if err != nil {
return err
}
- ctx.inputBuff = bytes.NewBuffer(inputData)
// parse JSON
- jsonRoot, err := parseJSON(inputData)
+ jsonRoot, err := parseJSON(ctx.inputData)
if err != nil {
return err
}
@@ -207,6 +207,8 @@ func generatePackage(ctx *context, w *bufio.Writer, jsonRoot *jsongo.JSONNode) e
generatePackageHeader(ctx, w, jsonRoot)
// generate data types
+ ctx.inputBuff = bytes.NewBuffer(ctx.inputData)
+ ctx.inputLine = 0
ctx.types = make(map[string]string)
types := jsonRoot.Map("types")
for i := 0; i < types.Len(); i++ {
@@ -218,6 +220,8 @@ func generatePackage(ctx *context, w *bufio.Writer, jsonRoot *jsongo.JSONNode) e
}
// generate messages
+ ctx.inputBuff = bytes.NewBuffer(ctx.inputData)
+ ctx.inputLine = 0
messages := jsonRoot.Map("messages")
for i := 0; i < messages.Len(); i++ {
msg := messages.At(i)
@@ -422,6 +426,8 @@ func generateMessageComment(ctx *context, w io.Writer, structName string, msgNam
// print out the source of the generated message - the JSON
msgFound := false
+ msgTitle := "\"" + msgName + "\","
+ var msgIndent int
for {
lineBuff, err := ctx.inputBuff.ReadBytes('\n')
if err != nil {
@@ -431,17 +437,23 @@ func generateMessageComment(ctx *context, w io.Writer, structName string, msgNam
line := string(lineBuff)
if !msgFound {
- if strings.Contains(line, msgName) {
- fmt.Fprintf(w, "// Generated from '%s', line %d:\n", ctx.inputFile, ctx.inputLine)
- fmt.Fprintln(w, "//")
- fmt.Fprint(w, "//", line)
- msgFound = true
+ msgIndent = strings.Index(line, msgTitle)
+ if msgIndent > -1 {
+ prefix := line[:msgIndent]
+ suffix := line[msgIndent+len(msgTitle):]
+ // If no other non-whitespace character then we are at the message header.
+ if strings.IndexFunc(prefix, isNotSpace) == -1 && strings.IndexFunc(suffix, isNotSpace) == -1 {
+ fmt.Fprintf(w, "// Generated from '%s', line %d:\n", ctx.inputFile, ctx.inputLine)
+ fmt.Fprintln(w, "//")
+ fmt.Fprint(w, "//", line)
+ msgFound = true
+ }
}
} else {
- fmt.Fprint(w, "//", line)
- if len(strings.Trim(line, " ")) < 4 {
+ if strings.IndexFunc(line, isNotSpace) < msgIndent {
break // end of the message in JSON
}
+ fmt.Fprint(w, "//", line)
}
}
fmt.Fprintln(w, "//")
@@ -595,6 +607,11 @@ func camelCaseName(name string) (should string) {
return string(runes)
}
+// isNotSpace returns true if the rune is NOT a whitespace character.
+func isNotSpace(r rune) bool {
+ return !unicode.IsSpace(r)
+}
+
// commonInitialisms is a set of common initialisms that need to stay in upper case.
var commonInitialisms = map[string]bool{
"ACL": true,