Skip to content

Commit

Permalink
Remove duplicate intermediate types.
Browse files Browse the repository at this point in the history
This condenses the N types and M properties from N*M intermediate type
definitions to just M intermediate type definitions. These intermediate
types are no longer generated in the gen_<TYPE>.go files, but are all
within the separate gen_intermediate.go file.

This will hopefully reduce resource consumption during compilation of
the vocab package.
  • Loading branch information
cjslep committed Jun 17, 2018
1 parent 307eb25 commit af5ed41
Show file tree
Hide file tree
Showing 58 changed files with 30,369 additions and 265,763 deletions.
23 changes: 21 additions & 2 deletions tools/vocab/gen/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,13 @@ func GenerateImplementations(types []*defs.Type, properties []*defs.PropertyType
Content: b,
})

// ActivityStream Types
m := make(map[*defs.PropertyType]*intermedDef)
for _, t := range types {
p := &defs.PackageDef{
Name: "vocab",
}
funcs, defs, interfaces, imports := generateDefinitions(t)
imports["fmt"] = true
funcs, defs, interfaces, imports := generateDefinitions(t, m)
for i, _ := range imports {
p.Imports = append(p.Imports, i)
}
Expand All @@ -92,6 +93,24 @@ func GenerateImplementations(types []*defs.Type, properties []*defs.PropertyType
Content: b,
})
}

// Intermediate definitions
p = &defs.PackageDef{
Name: "vocab",
Imports: []string{"fmt", "net/url", "time"},
}
for _, v := range m {
p.F = append(p.F, v.F...)
p.Defs = append(p.Defs, v.S)
}
b, err = format.Source([]byte(p.Generate()))
if err != nil {
return
}
f = append(f, &File{
Name: fmt.Sprintf("gen_intermediate.go"),
Content: b,
})
return
}

Expand Down
10 changes: 5 additions & 5 deletions tools/vocab/gen/generate_definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/go-fed/activity/tools/defs"
)

func generateDefinitions(t *defs.Type) (fd []*defs.FunctionDef, sd []*defs.StructDef, x []*defs.InterfaceDef, imports map[string]bool) {
func generateDefinitions(t *defs.Type, m map[*defs.PropertyType]*intermedDef) (fd []*defs.FunctionDef, sd []*defs.StructDef, x []*defs.InterfaceDef, imports map[string]bool) {
imports = make(map[string]bool)
this := &defs.StructDef{
Typename: t.Name,
Expand All @@ -23,7 +23,7 @@ func generateDefinitions(t *defs.Type) (fd []*defs.FunctionDef, sd []*defs.Struc
var serializeFragments []string
var deserializeFragments []string
for _, p := range t.GetProperties() {
pf, pd, s, d := generatePropertyDefinitions(p, this, thisInterface)
pf, pd, s, d := generatePropertyDefinitions(p, this, thisInterface, m)
serializeFragments = append(serializeFragments, s)
deserializeFragments = append(deserializeFragments, d)
fd = append(fd, pf...)
Expand All @@ -36,7 +36,7 @@ func generateDefinitions(t *defs.Type) (fd []*defs.FunctionDef, sd []*defs.Struc
}
}
}
generateWithoutProperties(t, this, thisInterface)
generateWithoutProperties(t, this, thisInterface, m)
generateAddUnknownFunction(t, this)
generateHasUnknownFunction(t, this)
generateRemoveUnknownFunction(t, this)
Expand Down Expand Up @@ -237,7 +237,7 @@ func generateSerializeFunction(t *defs.Type, this *defs.StructDef, fragments []s
this.F = append(this.F, d)
}

func generateWithoutProperties(d *defs.Type, this *defs.StructDef, it *defs.InterfaceDef) {
func generateWithoutProperties(d *defs.Type, this *defs.StructDef, it *defs.InterfaceDef, m map[*defs.PropertyType]*intermedDef) {
hasNamed := make(map[string]bool, 0)
for _, p := range d.GetProperties() {
hasNamed[p.Name] = true
Expand All @@ -251,7 +251,7 @@ func generateWithoutProperties(d *defs.Type, this *defs.StructDef, it *defs.Inte
Typename: this.Typename,
Comment: this.Comment,
}
_, _, _, _ = generatePropertyDefinitions(t, clonedThis, it)
_, _, _, _ = generatePropertyDefinitions(t, clonedThis, it, m)
for _, f := range clonedThis.F {
if len(f.Return) > 1 {
panic("generateWithoutProperties can only handle replacing functions with zero or one return types")
Expand Down
Loading

0 comments on commit af5ed41

Please sign in to comment.