Skip to content

Commit

Permalink
Merge pull request #113 from jackhftang/master
Browse files Browse the repository at this point in the history
deterministic generation
  • Loading branch information
francoispqt authored May 29, 2019
2 parents e46791d + 8bd2766 commit c53b22c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
14 changes: 9 additions & 5 deletions gojay/codegen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os"
"path/filepath"
"strings"

"github.com/viant/toolbox"
)

Expand Down Expand Up @@ -89,19 +88,24 @@ func (g *Generator) Generate() error {

func (g *Generator) writeCode() error {
var generatedCode = []string{}
for _, code := range g.pooledObjects {

for _, key := range sortedKeys(g.pooledObjects) {
code := g.pooledObjects[key]
generatedCode = append(generatedCode, code)
}
generatedCode = append(generatedCode, "")
for _, code := range g.sliceTypes {
for _, key := range sortedKeys(g.sliceTypes) {
code := g.sliceTypes[key]
generatedCode = append(generatedCode, code)
}
generatedCode = append(generatedCode, "")
for _, code := range g.structTypes {
for _, key := range sortedKeys(g.structTypes) {
code := g.structTypes[key]
generatedCode = append(generatedCode, code)
}

for _, code := range g.poolInit {
for _, key := range sortedKeys(g.poolInit) {
code := g.poolInit[key]
if g.Init != "" {
g.Init += "\n"
}
Expand Down
12 changes: 12 additions & 0 deletions gojay/codegen/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/viant/toolbox"
"reflect"
"strings"
"sort"
)

func firstLetterToUppercase(text string) string {
Expand Down Expand Up @@ -87,3 +88,14 @@ func getJSONKey(options *Options, field *toolbox.FieldInfo) string {
func normalizeTypeName(typeName string) string {
return strings.Replace(typeName, "*", "", strings.Count(typeName, "*"))
}

func sortedKeys(m map[string]string) ([]string) {
keys := make([]string, len(m))
i := 0
for k := range m {
keys[i] = k
i++
}
sort.Strings(keys)
return keys
}

0 comments on commit c53b22c

Please sign in to comment.