Skip to content

Commit 8086037

Browse files
authored
Merge pull request #10 from swiftcarrot/custom-string
improve model stringer output
2 parents 6c169b7 + 06607e0 commit 8086037

26 files changed

+231
-135
lines changed

generator/client/golang/templates/[model].gotmpl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@ func ({{ $m }} *{{.model.Name}}) String() string {
3333
var b strings.Builder
3434
b.WriteString("({{ $.model.Name }} ")
3535
{{- range $i, $c := $.model.Columns }}
36-
b.WriteString(fmt.Sprintf("{{ $c.Name }}: \"%+v\"", {{ $m }}.{{ pascal $c.Name }}))
36+
{{- if $c.Null }}
37+
b.WriteString(fmt.Sprintf("{{ $c.Name }}: %s", {{ $m }}.{{ pascal $c.Name }}))
38+
{{- else if eq $c.Type "string" }}
39+
b.WriteString(fmt.Sprintf(`{{ $c.Name }}: "%s"`, {{ $m }}.{{ pascal $c.Name }}))
40+
{{- else }}
41+
b.WriteString(fmt.Sprintf("{{ $c.Name }}: %v", {{ $m }}.{{ pascal $c.Name }}))
42+
{{- end }}
3743
{{- $l := len $.model.Columns }}
3844
{{- if ne $i (sub $l 1) }}
3945
b.WriteString(", ")

generator/client/golang/templates/[model]_query.gotmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ func (q *{{ $.model.Name }}Query) preload{{ pascal $b.Name }}(rows []*{{ $.model
254254
ids := []int64{}
255255
for _, r := range rows {
256256
{{- if $b.Null }}
257-
if !r.{{ $b.ForeignKey | pascal }}.Null {
257+
if r.{{ $b.ForeignKey | pascal }}.Valid {
258258
ids = append(ids, r.{{ $b.ForeignKey | pascal }}.Val)
259259
}
260260
{{- else }}
@@ -272,7 +272,7 @@ func (q *{{ $.model.Name }}Query) preload{{ pascal $b.Name }}(rows []*{{ $.model
272272
}
273273
for _, r := range rows {
274274
{{- if $b.Null }}
275-
if !r.{{ $b.ForeignKey | pascal }}.Null {
275+
if r.{{ $b.ForeignKey | pascal }}.Valid {
276276
r.{{ $b.Name | pascal }} = m[r.{{ $b.ForeignKey | pascal }}.Val]
277277
}
278278
{{- else }}

generator/client/golang/templates/queryx/bigint.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,24 @@ import (
66
"database/sql"
77
"database/sql/driver"
88
"encoding/json"
9+
"strconv"
910
)
1011

1112
type BigInt struct {
12-
Val int64
13-
Null bool
14-
Set bool
13+
Val int64
14+
Valid bool
15+
Set bool
1516
}
1617

1718
func NewBigInt(v int64) BigInt {
18-
return BigInt{Val: v, Set: true}
19+
return BigInt{Val: v, Valid: true, Set: true}
1920
}
2021

2122
func NewNullableBigInt(v *int64) BigInt {
2223
if v != nil {
2324
return NewBigInt(*v)
2425
}
25-
return BigInt{Null: true, Set: true}
26+
return BigInt{Set: true}
2627
}
2728

2829
// Scan implements the Scanner interface.
@@ -32,21 +33,21 @@ func (b *BigInt) Scan(value interface{}) error {
3233
if err != nil {
3334
return err
3435
}
35-
b.Val, b.Null = n.Int64, !n.Valid
36+
b.Val, b.Valid = n.Int64, n.Valid
3637
return nil
3738
}
3839

3940
// Value implements the driver Valuer interface.
4041
func (b BigInt) Value() (driver.Value, error) {
41-
if b.Null {
42+
if !b.Valid {
4243
return nil, nil
4344
}
4445
return b.Val, nil
4546
}
4647

4748
// MarshalJSON implements the json.Marshaler interface.
4849
func (b BigInt) MarshalJSON() ([]byte, error) {
49-
if b.Null {
50+
if !b.Valid {
5051
return json.Marshal(nil)
5152
}
5253
return json.Marshal(b.Val)
@@ -56,11 +57,19 @@ func (b BigInt) MarshalJSON() ([]byte, error) {
5657
func (b *BigInt) UnmarshalJSON(data []byte) error {
5758
b.Set = true
5859
if string(data) == "null" {
59-
b.Null = true
6060
return nil
6161
}
62+
b.Valid = true
6263
if err := json.Unmarshal(data, &b.Val); err != nil {
6364
return err
6465
}
6566
return nil
6667
}
68+
69+
// String implements the stringer interface.
70+
func (b BigInt) String() string {
71+
if !b.Valid {
72+
return "null"
73+
}
74+
return strconv.FormatInt(b.Val, 10)
75+
}

generator/client/golang/templates/queryx/bigint_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import (
1212
func TestNewBigInt(t *testing.T) {
1313
b1 := NewBigInt(2)
1414
require.Equal(t, int64(2), b1.Val)
15-
require.Equal(t, false, b1.Null)
15+
require.True(t, b1.Valid)
1616

1717
b2 := NewNullableBigInt(nil)
18-
require.Equal(t, true, b2.Null)
18+
require.False(t, b2.Valid)
1919
}
2020

2121
func TestBigIntJSON(t *testing.T) {

generator/client/golang/templates/queryx/boolean.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ import (
99
)
1010

1111
type Boolean struct {
12-
Val bool
13-
Null bool
14-
Set bool
12+
Val bool
13+
Valid bool
14+
Set bool
1515
}
1616

1717
func NewBoolean(v bool) Boolean {
18-
return Boolean{Val: v, Set: true}
18+
return Boolean{Val: v, Valid: true, Set: true}
1919
}
2020

2121
func NewNullableBoolean(v *bool) Boolean {
2222
if v != nil {
2323
return NewBoolean(*v)
2424
}
25-
return Boolean{Null: true, Set: true}
25+
return Boolean{Set: true}
2626
}
2727

2828
// Scan implements the Scanner interface.
@@ -32,21 +32,21 @@ func (b *Boolean) Scan(value interface{}) error {
3232
if err != nil {
3333
return err
3434
}
35-
b.Val, b.Null = n.Bool, !n.Valid
35+
b.Val, b.Valid = n.Bool, n.Valid
3636
return nil
3737
}
3838

3939
// Value implements the driver Valuer interface.
4040
func (b Boolean) Value() (driver.Value, error) {
41-
if b.Null {
41+
if !b.Valid {
4242
return nil, nil
4343
}
4444
return b.Val, nil
4545
}
4646

4747
// MarshalJSON implements the json.Marshaler interface.
4848
func (b Boolean) MarshalJSON() ([]byte, error) {
49-
if b.Null {
49+
if !b.Valid {
5050
return json.Marshal(nil)
5151
}
5252
return json.Marshal(b.Val)
@@ -56,11 +56,22 @@ func (b Boolean) MarshalJSON() ([]byte, error) {
5656
func (b *Boolean) UnmarshalJSON(data []byte) error {
5757
b.Set = true
5858
if string(data) == "null" {
59-
b.Null = true
6059
return nil
6160
}
61+
b.Valid = true
6262
if err := json.Unmarshal(data, &b.Val); err != nil {
6363
return err
6464
}
6565
return nil
6666
}
67+
68+
// String implements the stringer interface.
69+
func (b Boolean) String() string {
70+
if !b.Valid {
71+
return "null"
72+
}
73+
if b.Val {
74+
return "true"
75+
}
76+
return "false"
77+
}

generator/client/golang/templates/queryx/boolean_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import (
1212
func TestNewBoolean(t *testing.T) {
1313
b1 := NewBoolean(true)
1414
require.Equal(t, true, b1.Val)
15-
require.Equal(t, false, b1.Null)
15+
require.True(t, b1.Valid)
1616

1717
b2 := NewNullableBoolean(nil)
18-
require.Equal(t, true, b2.Null)
18+
require.False(t, b2.Valid)
1919
}
2020

2121
func TestBooleanJSON(t *testing.T) {

generator/client/golang/templates/queryx/date.gotmpl

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
)
1111

1212
type Date struct {
13-
Val time.Time
14-
Null bool
15-
Set bool
13+
Val time.Time
14+
Valid bool
15+
Set bool
1616
}
1717

1818
func parseDate(s string) (*time.Time, error) {
@@ -30,17 +30,17 @@ func parseDate(s string) (*time.Time, error) {
3030
func NewDate(v string) Date {
3131
t, err := parseDate(v)
3232
if err != nil {
33-
return Date{Null: true, Set: true}
33+
return Date{Set: true}
3434
}
3535

36-
return Date{Val: *t, Set: true}
36+
return Date{Val: *t, Valid: true, Set: true}
3737
}
3838

3939
func NewNullableDate(v *string) Date {
4040
if v != nil {
4141
return NewDate(*v)
4242
}
43-
return Date{Null: true, Set: true}
43+
return Date{Set: true}
4444
}
4545

4646
// Scan implements the Scanner interface.
@@ -50,21 +50,21 @@ func (d *Date) Scan(value interface{}) error {
5050
if err != nil {
5151
return err
5252
}
53-
d.Val, d.Null = n.Time, !n.Valid
53+
d.Val, d.Valid = n.Time, n.Valid
5454
return nil
5555
}
5656

5757
// Value implements the driver Valuer interface.
5858
func (d Date) Value() (driver.Value, error) {
59-
if d.Null {
59+
if !d.Valid {
6060
return nil, nil
6161
}
6262
return d.Val, nil
6363
}
6464

6565
// MarshalJSON implements the json.Marshaler interface.
6666
func (d Date) MarshalJSON() ([]byte, error) {
67-
if d.Null {
67+
if !d.Valid {
6868
return json.Marshal(nil)
6969
}
7070
return json.Marshal(d.Val.Format("2006-01-02"))
@@ -75,10 +75,9 @@ func (d *Date) UnmarshalJSON(data []byte) error {
7575
d.Set = true
7676
s := string(data)
7777
if s == "null" || s == "" {
78-
d.Null = true
7978
return nil
8079
}
81-
80+
d.Valid = true
8281
s = s[len(`"`) : len(s)-len(`"`)]
8382
t, err := parseDate(s)
8483
if err != nil {
@@ -88,3 +87,11 @@ func (d *Date) UnmarshalJSON(data []byte) error {
8887
d.Val = *t
8988
return nil
9089
}
90+
91+
// String implements the stringer interface.
92+
func (d Date) String() string {
93+
if !d.Valid {
94+
return "null"
95+
}
96+
return d.Val.Format("2006-01-02")
97+
}

generator/client/golang/templates/queryx/date_test.gotmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import (
1212
func TestNewDate(t *testing.T) {
1313
d1 := NewDate("2012-11-10")
1414
require.Equal(t, "2012-11-10", d1.Val.Format("2006-01-02"))
15-
require.Equal(t, false, d1.Null)
15+
require.True(t, d1.Valid)
1616

1717
d2 := NewNullableDate(nil)
18-
require.Equal(t, true, d2.Null)
18+
require.False(t, d2.Valid)
1919
}
2020

2121
func TestDateJSON(t *testing.T) {

generator/client/golang/templates/queryx/datetime.gotmpl

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
)
1111

1212
type Datetime struct {
13-
Val time.Time
14-
Null bool
15-
Set bool
13+
Val time.Time
14+
Valid bool
15+
Set bool
1616
}
1717

1818
func loadLocation() (*time.Location, error) {
@@ -39,16 +39,16 @@ func Now(layout string) string {
3939
func NewDatetime(v string) Datetime {
4040
t, err := parseDatetime(v)
4141
if err != nil {
42-
return Datetime{Null: true, Set: true}
42+
return Datetime{Set: true}
4343
}
44-
return Datetime{Val: *t, Set: true}
44+
return Datetime{Val: *t, Valid: true, Set: true}
4545
}
4646

4747
func NewNullableDatetime(v *string) Datetime {
4848
if v != nil {
4949
return NewDatetime(*v)
5050
}
51-
return Datetime{Null: true, Set: true}
51+
return Datetime{Set: true}
5252
}
5353

5454
// Scan implements the Scanner interface.
@@ -58,7 +58,7 @@ func (d *Datetime) Scan(value interface{}) error {
5858
if err != nil {
5959
return err
6060
}
61-
d.Val, d.Null = n.Time, !n.Valid
61+
d.Val, d.Valid = n.Time, n.Valid
6262
loc, err := loadLocation()
6363
if err != nil {
6464
return err
@@ -69,15 +69,15 @@ func (d *Datetime) Scan(value interface{}) error {
6969

7070
// Value implements the driver Valuer interface.
7171
func (d Datetime) Value() (driver.Value, error) {
72-
if d.Null {
72+
if !d.Valid {
7373
return nil, nil
7474
}
7575
return d.Val.UTC(), nil
7676
}
7777

7878
// MarshalJSON implements the json.Marshaler interface.
7979
func (d Datetime) MarshalJSON() ([]byte, error) {
80-
if d.Null {
80+
if !d.Valid {
8181
return json.Marshal(nil)
8282
}
8383
return json.Marshal(d.Val.UTC())
@@ -88,10 +88,9 @@ func (d *Datetime) UnmarshalJSON(data []byte) error {
8888
d.Set = true
8989
s := string(data)
9090
if s == "null" || s == "" {
91-
d.Null = true
9291
return nil
9392
}
94-
93+
d.Valid = true
9594
t := time.Time{}
9695
err := t.UnmarshalJSON(data)
9796
if err != nil {
@@ -106,3 +105,11 @@ func (d *Datetime) UnmarshalJSON(data []byte) error {
106105

107106
return nil
108107
}
108+
109+
// String implements the stringer interface.
110+
func (d Datetime) String() string {
111+
if !d.Valid {
112+
return "null"
113+
}
114+
return d.Val.Format("2006-01-02 15:04:05")
115+
}

0 commit comments

Comments
 (0)