Skip to content

Commit

Permalink
Properties must be sorted (#8)
Browse files Browse the repository at this point in the history
The "properties" must be sorted so that the generated dingo.go is deterministic. It also makes it a little easier to read.
  • Loading branch information
elliotchance committed Jun 11, 2019
1 parent 479a901 commit 2586155
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,11 @@ func main() {
}

// Properties
for propertyName, propertyValue := range definition.Properties {
for _, property := range definition.SortedProperties() {
instantiation = append(instantiation, &ast.AssignStmt{
Tok: token.ASSIGN,
Lhs: []ast.Expr{&ast.Ident{Name: serviceTempVariable + "." + propertyName}},
Rhs: []ast.Expr{&ast.Ident{Name: resolveStatement(propertyValue)}},
Lhs: []ast.Expr{&ast.Ident{Name: serviceTempVariable + "." + property.Name}},
Rhs: []ast.Expr{&ast.Ident{Name: resolveStatement(property.Value)}},
})
}

Expand Down
5 changes: 5 additions & 0 deletions property.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

type Property struct {
Name, Value string
}
20 changes: 20 additions & 0 deletions service.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package main

import "sort"

type Service struct {
Type Type
Interface Type
Expand Down Expand Up @@ -42,3 +44,21 @@ func (service *Service) Imports() map[string]string {

return imports
}

func (service *Service) SortedProperties() (sortedProperties []*Property) {
var propertyNames []string
for propertyName := range service.Properties {
propertyNames = append(propertyNames, propertyName)
}

sort.Strings(propertyNames)

for _, propertyName := range propertyNames {
sortedProperties = append(sortedProperties, &Property{
Name: propertyName,
Value: service.Properties[propertyName],
})
}

return
}

0 comments on commit 2586155

Please sign in to comment.