-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better handling of types defined in different files in the package.
- Loading branch information
1 parent
4f41402
commit 713f9c5
Showing
18 changed files
with
159 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package everything | ||
|
||
type Child struct { | ||
ID ChildID `json:"id"` | ||
Name string `json:"name"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package everything | ||
|
||
type ParentID string | ||
|
||
type ChildID int64 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package format | ||
|
||
import ( | ||
"fmt" | ||
"go/ast" | ||
) | ||
|
||
type FallbackFormatter struct { | ||
TypeFormatterBase | ||
} | ||
|
||
func (f *FallbackFormatter) under(expr ast.Expr) *ast.TypeSpec { | ||
if e, ok := expr.(*ast.Ident); ok && e.Obj == nil { | ||
if val, ok := f.Registry.KnownTypes[e.Name]; ok { | ||
return val | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (f *FallbackFormatter) CanFormat(expr ast.Expr) bool { | ||
return f.under(expr) != nil | ||
} | ||
|
||
func (f *FallbackFormatter) Signature(expr ast.Expr) string { | ||
t := f.under(expr) | ||
switch t.Type.(type) { | ||
case *ast.Ident: | ||
return f.Registry.GetTypeFormatter(t.Type).Signature(t.Type) | ||
default: | ||
return t.Name.Name | ||
} | ||
} | ||
|
||
func (f *FallbackFormatter) Declaration(fieldName string, expr ast.Expr) string { | ||
return fmt.Sprintf("%s %s", f.Signature(expr), fieldName) | ||
} | ||
|
||
func (f *FallbackFormatter) Constructor(fieldName string, _ ast.Expr) string { | ||
return "required this." + fieldName | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,51 @@ | ||
package format | ||
|
||
import ( | ||
"fmt" | ||
"go/ast" | ||
) | ||
|
||
type TypeFormatter interface { | ||
SetRegistry(registry *TypeFormatterRegistry) | ||
CanFormat(expr ast.Expr) bool | ||
Signature(expr ast.Expr) string | ||
Declaration(fieldName string, expr ast.Expr) string | ||
Constructor(fieldName string, expr ast.Expr) string | ||
} | ||
|
||
var Formatters []TypeFormatter | ||
type TypeFormatterBase struct { | ||
Registry *TypeFormatterRegistry | ||
} | ||
|
||
func (t *TypeFormatterBase) SetRegistry(registry *TypeFormatterRegistry) { | ||
t.Registry = registry | ||
} | ||
|
||
type TypeFormatterRegistry struct { | ||
KnownTypes map[string]*ast.TypeSpec | ||
Formatters []TypeFormatter | ||
} | ||
|
||
func NewTypeFormatterRegistry() *TypeFormatterRegistry { | ||
return &TypeFormatterRegistry{ | ||
KnownTypes: make(map[string]*ast.TypeSpec), | ||
Formatters: make([]TypeFormatter, 0), | ||
} | ||
} | ||
func (t *TypeFormatterRegistry) RegisterTypeFormatter(formatter TypeFormatter) { | ||
t.Formatters = append(t.Formatters, formatter) | ||
formatter.SetRegistry(t) | ||
} | ||
|
||
func GetTypeFormatter(expr ast.Expr) TypeFormatter { | ||
for _, f := range Formatters { | ||
func (t *TypeFormatterRegistry) GetTypeFormatter(expr ast.Expr) TypeFormatter { | ||
// walks the t.Formatters in reverse order | ||
// so that the last registered formatter is the first to be checked | ||
for i := len(t.Formatters) - 1; i >= 0; i-- { | ||
f := t.Formatters[i] | ||
if f.CanFormat(expr) { | ||
return f | ||
} | ||
} | ||
|
||
panic("no formatter found for type") | ||
panic(fmt.Sprintf("no formatter found for %v", expr)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.