Skip to content

Commit 8371003

Browse files
authored
fix(types): fixing type mismatch (#73)
fixing type mismatch
1 parent 53f31d0 commit 8371003

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

pkg/entities/column.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ type Column struct {
3939

4040
func (c *Column) setTypeInfo(tp *types.FieldType) {
4141
c.Type = tp.EvalType().String()
42+
if tp.GetType() == mysql.TypeLonglong {
43+
c.Type = "bigint"
44+
}
4245
c.TypeSize = tp.GetFlen()
4346
c.TypePrecision = tp.GetDecimal()
4447
if tp.GetType() == mysql.TypeEnum {
@@ -85,7 +88,7 @@ func (c *Column) setOptions(col *ast.ColumnDef) error {
8588
c.InUniqueKey = true
8689
default:
8790
// Ignore other options
88-
slog.Warn("Unknown column option", slog.Int("type", int(opt.Tp)))
91+
slog.Warn("Unhandled column option", slog.Int("type", int(opt.Tp)))
8992
}
9093
}
9194
return nil

pkg/generation/template_helpers.go

+2
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ func getType(col *entities.Column) string {
257257
return "usql.NullDuration"
258258
}
259259
return "usql.Duration"
260+
case "json":
261+
return "json.RawMessage"
260262
default:
261263
fmt.Println("Unknown type:", col.Type)
262264
return "unknown"

templates/model.tmpl

+5-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ const (
2828
{{- end }}
2929
type {{ $struct }} struct {
3030
{{ range $column := .Columns -}}
31-
{{ .Name | structify }} {{ get_type $column }} {{ get_tags $column }} {{ if .Comment }}// {{ .Comment }}{{ end }}
31+
{{- if .Comment -}}
32+
// {{ .Comment }}
33+
{{- end -}}
34+
{{ .Name | structify }} {{ get_type $column }} {{ get_tags $column }}
3235
{{ end -}}
3336
}
3437

@@ -182,7 +185,7 @@ func (m *{{ $struct }}) Get{{ $local_col | structify }}{{ $foreign_struct }}(db
182185
}
183186

184187
{{ end -}}
185-
return {{ $foreign_struct }}By{{ $foreign_col | structify }}(db, m.{{ $local_col | structify }}{{ if $col_data.Nullable }}.{{ get_type $col_data }}{{ end }})
188+
return {{ $foreign_struct }}By{{ $foreign_col | structify }}(db, m.{{ $local_col | structify }}{{ if $col_data.Nullable }}.Val(){{ end }})
186189
}
187190
{{ end -}}
188191
{{ end -}}

usql/types.go

+26-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type NullBool struct {
1919
}
2020

2121
// MarshalJSON implements the json.Marshaler interface for a NullBool.
22-
func (r NullBool) MarshalJSON() ([]byte, error) {
22+
func (r *NullBool) MarshalJSON() ([]byte, error) {
2323
if r.Valid {
2424
return json.Marshal(r.Bool)
2525
}
@@ -44,6 +44,10 @@ func (r *NullBool) UnmarshalJSON(data []byte) error {
4444
return nil
4545
}
4646

47+
func (r NullBool) Val() bool {
48+
return r.Bool
49+
}
50+
4751
// NewNullBool returns a valid new NullBool for a given boolean value.
4852
func NewNullBool(b bool) *NullBool {
4953
return &NullBool{NullBool: sql.NullBool{Bool: b, Valid: true}}
@@ -80,6 +84,10 @@ func (r *NullFloat64) UnmarshalJSON(data []byte) error {
8084
return nil
8185
}
8286

87+
func (r *NullFloat64) Val() float64 {
88+
return r.Float64
89+
}
90+
8391
// NewNullFloat64 returns a valid new NullFloat64 for a given float64 value.
8492
func NewNullFloat64(f float64) *NullFloat64 {
8593
return &NullFloat64{NullFloat64: sql.NullFloat64{Float64: f, Valid: true}}
@@ -116,6 +124,10 @@ func (r *NullInt64) UnmarshalJSON(data []byte) error {
116124
return nil
117125
}
118126

127+
func (r NullInt64) Val() int64 {
128+
return r.Int64
129+
}
130+
119131
// RedisArg implements redis.Argument.
120132
//
121133
// The caller should explicitly check the Valid field when putting NullString
@@ -185,6 +197,10 @@ func (r *NullString) UnmarshalJSON(data []byte) error {
185197
return nil
186198
}
187199

200+
func (r NullString) Val() string {
201+
return r.String
202+
}
203+
188204
// RedisArg implements redis.Argument.
189205
//
190206
// The caller should explicitly check the Valid field when putting NullString
@@ -305,6 +321,10 @@ type NullDuration struct {
305321
Valid bool
306322
}
307323

324+
func (r NullDuration) Val() time.Duration {
325+
return time.Duration(r.Duration)
326+
}
327+
308328
// MarshalJSON implements the json.Marshaler interface for a NullDuration.
309329
func (r NullDuration) MarshalJSON() ([]byte, error) {
310330
if r.Valid {
@@ -341,6 +361,11 @@ type NullTime struct {
341361
sql.NullTime
342362
}
343363

364+
// Val returns the time.Time value of the NullTime.
365+
func (r NullTime) Val() time.Time {
366+
return r.Time
367+
}
368+
344369
// MarshalJSON implements json.Marshaller.
345370
func (r NullTime) MarshalJSON() ([]byte, error) {
346371
if r.Valid && !r.Time.IsZero() {

0 commit comments

Comments
 (0)