-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #730 from tdakkota/feat/custom-format-primitive-types
feat(gen): allow to define custom format for primitive types
- Loading branch information
Showing
18 changed files
with
603 additions
and
146 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
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,106 @@ | ||
package gen | ||
|
||
import ( | ||
"fmt" | ||
"go/token" | ||
"reflect" | ||
|
||
"github.com/go-faster/errors" | ||
|
||
"github.com/ogen-go/ogen/gen/ir" | ||
"github.com/ogen-go/ogen/internal/xmaps" | ||
) | ||
|
||
func checkImportableType(typ reflect.Type) error { | ||
path := typ.PkgPath() | ||
if path == "main" { | ||
return errors.New("type must be in importable package") | ||
} | ||
|
||
name := typ.Name() | ||
if name == "" { | ||
return errors.New("type must be named or primitive") | ||
} | ||
|
||
if path != "" && !token.IsExported(name) { | ||
return errors.New("type must be exported") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (g *Generator) makeCustomFormats() error { | ||
importPaths := map[string]string{} | ||
|
||
makeExternal := func(typ reflect.Type) (ir.ExternalType, error) { | ||
if err := checkImportableType(typ); err != nil { | ||
return ir.ExternalType{}, err | ||
} | ||
|
||
path := typ.PkgPath() | ||
if path == "" { | ||
// Primitive type. | ||
return ir.ExternalType{Type: typ}, nil | ||
} | ||
|
||
importName, ok := importPaths[path] | ||
if !ok { | ||
importName = fmt.Sprintf("custom%d", len(importPaths)) | ||
importPaths[path] = importName | ||
g.imports = append(g.imports, fmt.Sprintf("%s %q", importName, path)) | ||
} | ||
|
||
return ir.ExternalType{ | ||
Pkg: importName, | ||
Type: typ, | ||
}, nil | ||
} | ||
|
||
for _, jsonTyp := range xmaps.SortedKeys(g.opt.CustomFormats) { | ||
formats := g.opt.CustomFormats[jsonTyp] | ||
for _, format := range xmaps.SortedKeys(formats) { | ||
def := formats[format] | ||
|
||
if _, ok := g.customFormats[jsonTyp]; !ok { | ||
g.customFormats[jsonTyp] = map[string]ir.CustomFormat{} | ||
} | ||
|
||
f, err := func() (f ir.CustomFormat, _ error) { | ||
goName, err := pascalNonEmpty(format) | ||
if err != nil { | ||
return f, errors.Wrap(err, "generate go name") | ||
} | ||
|
||
typ, err := makeExternal(def.typ) | ||
if err != nil { | ||
return f, errors.Wrap(err, "format type") | ||
} | ||
|
||
json, err := makeExternal(def.json) | ||
if err != nil { | ||
return f, errors.Wrap(err, "json encoding") | ||
} | ||
|
||
text, err := makeExternal(def.text) | ||
if err != nil { | ||
return f, errors.Wrap(err, "text encoding") | ||
} | ||
|
||
return ir.CustomFormat{ | ||
Name: format, | ||
GoName: goName, | ||
Type: typ, | ||
JSON: json, | ||
Text: text, | ||
}, nil | ||
}() | ||
if err != nil { | ||
return errors.Wrapf(err, "custom format %q:%q", jsonTyp, format) | ||
} | ||
|
||
g.customFormats[jsonTyp][format] = f | ||
} | ||
} | ||
|
||
return nil | ||
} |
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,87 @@ | ||
package gen | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
"unsafe" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ogen-go/ogen/gen/ir" | ||
) | ||
|
||
func typeOf[T any]() reflect.Type { | ||
return reflect.TypeOf(new(T)).Elem() | ||
} | ||
|
||
type foo struct{} | ||
|
||
type ExportedFunc func() foo | ||
|
||
func Test_checkImportableType(t *testing.T) { | ||
tests := []struct { | ||
typ reflect.Type | ||
wantErr bool | ||
}{ | ||
// Primitive types. | ||
{typeOf[bool](), false}, | ||
{typeOf[int](), false}, | ||
{typeOf[int8](), false}, | ||
{typeOf[int16](), false}, | ||
{typeOf[int32](), false}, | ||
{typeOf[int64](), false}, | ||
{typeOf[uint](), false}, | ||
{typeOf[uint8](), false}, | ||
{typeOf[uint16](), false}, | ||
{typeOf[uint32](), false}, | ||
{typeOf[uint64](), false}, | ||
{typeOf[uintptr](), false}, | ||
{typeOf[float32](), false}, | ||
{typeOf[float64](), false}, | ||
{typeOf[complex64](), false}, | ||
{typeOf[complex128](), false}, | ||
{typeOf[string](), false}, | ||
{typeOf[unsafe.Pointer](), false}, | ||
// Exported types. | ||
{typeOf[Generator](), false}, | ||
{typeOf[ir.Kind](), false}, | ||
{typeOf[ir.CustomFormat](), false}, | ||
{typeOf[ExportedFunc](), false}, | ||
|
||
// Negative cases. | ||
// | ||
// Unexported type. | ||
{typeOf[foo](), true}, | ||
{typeOf[func(foo)](), true}, | ||
{typeOf[func() foo](), true}, | ||
{typeOf[*foo](), true}, | ||
{typeOf[chan foo](), true}, | ||
{typeOf[[]foo](), true}, | ||
{typeOf[map[string]foo](), true}, | ||
// Unnamed type. | ||
{typeOf[struct{}](), true}, | ||
{typeOf[func(struct{})](), true}, | ||
{typeOf[struct{ X int }](), true}, | ||
} | ||
for i, tt := range tests { | ||
t.Run(fmt.Sprintf("Test%d", i+1), func(t *testing.T) { | ||
check := require.Error | ||
if !tt.wantErr { | ||
check = require.NoError | ||
} | ||
|
||
err := checkImportableType(tt.typ) | ||
defer func() { | ||
t.Logf("Kind: %q", tt.typ.Kind()) | ||
t.Logf("Package: %q", tt.typ.PkgPath()) | ||
t.Logf("Name: %q", tt.typ.Name()) | ||
if err != nil { | ||
t.Logf("Error: %v", err) | ||
} | ||
}() | ||
|
||
check(t, err) | ||
}) | ||
} | ||
} |
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.