Skip to content

Commit b28f242

Browse files
author
Miguel Molina
authored
Merge pull request #116 from erizocosmico/bugfix/alias-address
generator: cast address of aliased basic types
2 parents c212f69 + dcb31a1 commit b28f242

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

generator/template.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,12 @@ func (td *TemplateData) genFieldsColumnAddresses(buf *bytes.Buffer, fields []*Fi
7474
if (f.IsJSON || f.Kind == Interface) && f.IsPtr {
7575
buf.WriteString(fmt.Sprintf(initNilPtrTpl, f.Name, f.Name, td.GenTypeName(f)))
7676
}
77-
buf.WriteString(fmt.Sprintf("return %s, nil\n", f.Address()))
77+
78+
if f.Kind == Basic && f.IsAlias {
79+
buf.WriteString(fmt.Sprintf("return (*%s)(%s), nil\n", f.Type, f.Address()))
80+
} else {
81+
buf.WriteString(fmt.Sprintf("return %s, nil\n", f.Address()))
82+
}
7883
}
7984
}
8085
}

generator/template_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ case "url_no_ptr":
6666
return (*types.URL)(&r.UrlNoPtr), nil
6767
case "foo_id":
6868
return kallax.VirtualColumn("foo_id", r, new(kallax.NumericID)), nil
69+
case "basic_alias":
70+
return (*int)(&r.BasicAlias), nil
6971
`
7072

7173
const baseTpl = `
@@ -88,6 +90,8 @@ const baseTpl = `
8890
8991
type URLs []*url.URL
9092
93+
type Qux int
94+
9195
type Foo struct {
9296
kallax.Model
9397
ID int64 ` + "`pk:\"autoincr\"`" + `
@@ -102,6 +106,7 @@ const baseTpl = `
102106
URL *url.URL
103107
UrlNoPtr url.URL
104108
RelInverse Rel ` + "`fk:\",inverse\"`" + `
109+
BasicAlias Qux
105110
}
106111
`
107112

@@ -189,6 +194,7 @@ kallax.NewSchemaField("json"),
189194
kallax.NewSchemaField("url"),
190195
kallax.NewSchemaField("url_no_ptr"),
191196
kallax.NewSchemaField("foo_id"),
197+
kallax.NewSchemaField("basic_alias"),
192198
`
193199

194200
func (s *TemplateSuite) TestGenModelColumns() {

tests/kallax.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4290,9 +4290,9 @@ func (r *QueryFixture) ColumnAddress(col string) (interface{}, error) {
42904290
case "alias_slice_param":
42914291
return types.Slice((*[]string)(&r.AliasSliceParam)), nil
42924292
case "alias_string_param":
4293-
return &r.AliasStringParam, nil
4293+
return (*string)(&r.AliasStringParam), nil
42944294
case "alias_int_param":
4295-
return &r.AliasIntParam, nil
4295+
return (*int)(&r.AliasIntParam), nil
42964296
case "dummy_param":
42974297
return types.JSON(&r.DummyParam), nil
42984298
case "alias_dummy_param":

0 commit comments

Comments
 (0)