Skip to content

Commit 1075f32

Browse files
committed
use generic type aliases
1 parent ee6bae8 commit 1075f32

File tree

9 files changed

+154
-299
lines changed

9 files changed

+154
-299
lines changed

README.md

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,16 @@ func (ShortStr) Validate(v string) error {
9999
return nil
100100
}
101101

102-
// that's it, basically. now we can use this validator in our request.
103-
104-
// but we can go extreme! we can combine multiple validators using types
105-
type ASCIIShortStr struct {
106-
// both ASCII and ShortStr must be satisfied.
107-
// you can also use [validate.Or] to ensure that at least one condition is satisfied.
108-
validate.And[
109-
string,
110-
validate.Charset0[string, charset.ASCII],
111-
ShortStr,
112-
]
113-
}
102+
// That's it, basically. Now we can use this validator in our request.
103+
104+
// But we can go extreme! It is possible to combine multiple validators using types.
105+
// both ASCII and ShortStr must be satisfied.
106+
// you can also use [validate.Or] to ensure that at least one condition is satisfied.
107+
type ASCIIShortStr = validate.And[
108+
string,
109+
validate.Charset0[string, charset.ASCII],
110+
ShortStr,
111+
]
114112

115113
// Now, our final request may look something like that
116114
type Request struct {
@@ -326,7 +324,7 @@ func main() {
326324
&example,
327325

328326
// this function also accepts variadic options.
329-
// here we say that unknown fields will result parsing error (by default they won't. just like json)
327+
// here we say that unknown fields will result parsing error (by default they won't, just like json)
330328
parse.WithDisallowUnknownFields(),
331329
)
332330
if err != nil {

bench/DataWithGen_schema_test.go

Lines changed: 20 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/codegen/User_schema.go

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/tour/main.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,16 @@ func (ShortStr) Validate(v string) error {
5959
return nil
6060
}
6161

62-
// that's it, basically. now we can use this validator in our request.
63-
64-
// but we can go extreme! we can combine multiple validators using types
65-
type ASCIIShortStr struct {
66-
// both ASCII and ShortStr must be satisfied.
67-
// you can also use [validate.Or] to ensure that at least one condition is satisfied.
68-
validate.And[
69-
string,
70-
validate.Charset0[string, charset.ASCII],
71-
ShortStr,
72-
]
73-
}
62+
// That's it, basically. Now we can use this validator in our request.
63+
64+
// But we can go extreme! It is possible to combine multiple validators using types.
65+
// both ASCII and ShortStr must be satisfied.
66+
// you can also use [validate.Or] to ensure that at least one condition is satisfied.
67+
type ASCIIShortStr = validate.And[
68+
string,
69+
validate.Charset0[string, charset.ASCII],
70+
ShortStr,
71+
]
7472

7573
// Now, our final request may look something like that
7674
type Request struct {

internal/typeconv/typeconv.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ func (c *TypeConverter) ConvertType(t types.Type) jen.Code {
2929
case *types.Named:
3030
return c.convertTypeNamed(t)
3131

32+
case *types.Alias:
33+
return c.ConvertType(t.Rhs())
34+
3235
case *types.Basic:
3336
return jen.Id(t.Name())
3437

@@ -42,27 +45,31 @@ func (c *TypeConverter) ConvertType(t types.Type) jen.Code {
4245
return jen.Map(c.ConvertType(t.Key())).Add(c.ConvertType(t.Elem()))
4346

4447
case *types.Struct:
45-
fields := make([]jen.Code, 0, t.NumFields())
48+
return c.convertTypeStruct(t)
4649

47-
for i := range t.NumFields() {
48-
field := t.Field(i)
49-
tag := t.Tag(i)
50-
fieldCode := jen.Id(field.Name()).Add(c.ConvertType(field.Type()))
50+
default:
51+
// Fallback for any other types
52+
return jen.Id(t.String())
53+
}
54+
}
5155

52-
if tag != "" {
53-
tagMap := parseStructTags(tag)
54-
fieldCode = fieldCode.Tag(tagMap)
55-
}
56+
func (c *TypeConverter) convertTypeStruct(t *types.Struct) jen.Code {
57+
fields := make([]jen.Code, 0, t.NumFields())
5658

57-
fields = append(fields, fieldCode)
58-
}
59+
for i := range t.NumFields() {
60+
field := t.Field(i)
61+
tag := t.Tag(i)
62+
fieldCode := jen.Id(field.Name()).Add(c.ConvertType(field.Type()))
5963

60-
return jen.Struct(fields...)
64+
if tag != "" {
65+
tagMap := parseStructTags(tag)
66+
fieldCode = fieldCode.Tag(tagMap)
67+
}
6168

62-
default:
63-
// Fallback for any other types
64-
return jen.Id(t.String())
69+
fields = append(fields, fieldCode)
6570
}
71+
72+
return jen.Struct(fields...)
6673
}
6774

6875
func (c *TypeConverter) convertTypeNamed(t *types.Named) jen.Code {

0 commit comments

Comments
 (0)