Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support overriden types #116

Merged
merged 1 commit into from
Nov 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions schema/gen/go/externUtil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package gengo

import (
"go/ast"
"go/parser"
"go/token"
"path"
"strings"
)

// getExternTypes provides a mapping of all types defined in the destination package.
// It is used by generate to not duplicate defined types to allow overriding of types.
func getExternTypes(pth string) (map[string]struct{}, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overridenTypes?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

manualHijinx?

set := token.NewFileSet()
packs, err := parser.ParseDir(set, pth, nil, 0)
if err != nil {
return nil, err
}

types := make(map[string]struct{})
for _, pack := range packs {
for fname, f := range pack.Files {
if strings.HasPrefix(path.Base(fname), "ipldsch_") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is a generator and the performance isn't the most important thing, but parsing all files to then only look at a small minority is a bit wasteful :)

It would be significantly less work to use globbing to find the ipldsch_*.go files we're interested in, and then parse and inspect those.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is excluding the three ipldsch_*.go files and looking at all others to find types that are defined not in generated code. (this also addresses your other comment of "is it stable over multiple runs")

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦 I read the logic backwards. I guess my point here is still valid, though now it's just "you can avoid reading+parsing these 3-4 files".

continue
}
for _, d := range f.Decls {
if t, isType := d.(*ast.GenDecl); isType {
if t.Tok == token.TYPE {
for _, s := range t.Specs {
ts := s.(*ast.TypeSpec)
types[ts.Name.Name] = struct{}{}
}
}
}
}
}
}

return types, nil
}
10 changes: 9 additions & 1 deletion schema/gen/go/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ func Generate(pth string, pkgName string, ts schema.TypeSystem, adjCfg *AdjunctC
EmitInternalEnums(pkgName, f)
})

externs, err := getExternTypes(pth)
if err != nil {
// Consider warning that duplication may be present due to inability to parse destination.
externs = make(map[string]struct{})
}

// Local helper function for applying generation logic to each type.
// We will end up doing this more than once because in this layout, more than one file contains part of the story for each type.
applyToEachType := func(fn func(tg TypeGenerator, w io.Writer), f io.Writer) {
Expand All @@ -28,7 +34,9 @@ func Generate(pth string, pkgName string, ts schema.TypeSystem, adjCfg *AdjunctC
types := ts.GetTypes()
keys := make(sortableTypeNames, 0, len(types))
for tn := range types {
keys = append(keys, tn)
if _, exists := externs[tn.String()]; !exists {
keys = append(keys, tn)
}
}
sort.Sort(keys)
for _, tn := range keys {
Expand Down