Skip to content

Commit 444811e

Browse files
eullerpereira94kyleconroy
authored andcommitted
fixed generation of json tags, fixed some tests and fixed a minor detail on a single for ranging a slice (sqlc-dev#87)
1 parent 35c6471 commit 444811e

File tree

4 files changed

+20
-27
lines changed

4 files changed

+20
-27
lines changed

internal/dinosql/checks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (v *funcCallVisitor) Visit(node nodes.Node) Visitor {
7676
}
7777

7878
var sig []string
79-
for _, _ = range funcCall.Args.Items {
79+
for range funcCall.Args.Items {
8080
sig = append(sig, "unknown")
8181
}
8282

internal/dinosql/gen.go

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ func (r Result) Structs() []GoStruct {
394394
s.Fields = append(s.Fields, GoField{
395395
Name: r.structName(column.Name),
396396
Type: r.goType(column),
397-
Tags: map[string]string{"json": column.Name},
397+
Tags: map[string]string{"json:": column.Name},
398398
})
399399
}
400400
structs = append(structs, s)
@@ -429,9 +429,8 @@ func (r Result) goInnerType(columnType string, notNull bool) string {
429429
case "bigserial", "pg_catalog.serial8":
430430
if notNull {
431431
return "int64"
432-
} else {
433-
return "sql.NullInt64"
434432
}
433+
return "sql.NullInt64" // unnecessay else
435434

436435
case "smallserial", "pg_catalog.serial2":
437436
return "int16"
@@ -442,50 +441,44 @@ func (r Result) goInnerType(columnType string, notNull bool) string {
442441
case "bigint", "pg_catalog.int8":
443442
if notNull {
444443
return "int64"
445-
} else {
446-
return "sql.NullInt64"
447444
}
445+
return "sql.NullInt64" // unnecessary else
448446

449447
case "smallint", "pg_catalog.int2":
450448
return "int16"
451-
449+
452450
case "float", "double precision", "pg_catalog.float8":
453451
if notNull {
454452
return "float64"
455-
} else {
456-
return "sql.NullFloat64"
457453
}
458-
454+
return "sql.NullFloat64" // unnecessary else
455+
459456
case "real", "pg_catalog.float4":
460457
if notNull {
461458
return "float32"
462-
} else {
463-
return "sql.NullFloat64" //IMPORTANT: Change to sql.NullFloat32 after updating the go.mod file
464-
}
465-
459+
} // unnecessary else
460+
return "sql.NullFloat64" //IMPORTANT: Change to sql.NullFloat32 after updating the go.mod file
461+
466462
case "bool", "pg_catalog.bool":
467463
if notNull {
468464
return "bool"
469-
} else {
470-
return "sql.NullBool"
471465
}
466+
return "sql.NullBool" // unnecessary else
472467

473468
case "jsonb":
474469
return "json.RawMessage"
475470

476471
case "pg_catalog.timestamp", "pg_catalog.timestamptz":
477472
if notNull {
478473
return "time.Time"
479-
} else {
480-
return "pq.NullTime"
481474
}
475+
return "pq.NullTime" // unnecessary else
482476

483477
case "text", "pg_catalog.varchar", "pg_catalog.bpchar":
484478
if notNull {
485479
return "string"
486-
} else {
487-
return "sql.NullString"
488480
}
481+
return "sql.NullString" // unnecessary else
489482

490483
case "uuid":
491484
return "uuid.UUID"
@@ -531,7 +524,7 @@ func (r Result) columnsToStruct(name string, columns []core.Column) *GoStruct {
531524
gs.Fields = append(gs.Fields, GoField{
532525
Name: fieldName,
533526
Type: r.goType(c),
534-
Tags: map[string]string{"json": tagName},
527+
Tags: map[string]string{"json:": tagName},
535528
})
536529
seen[c.Name] += 1
537530
}
@@ -915,7 +908,7 @@ func Generate(r *Result, global GenerateSettings, settings PackageSettings) (map
915908
tctx := tmplCtx{
916909
Settings: global,
917910
EmitPreparedQueries: settings.EmitPreparedQueries,
918-
EmitJSONTags: settings.EmitTags,
911+
EmitJSONTags: settings.EmitJSONTags,
919912
Q: "`",
920913
Package: pkg,
921914
GoQueries: r.GoQueries(),

internal/dinosql/gen_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ func TestColumnsToStruct(t *testing.T) {
3737
expected := &GoStruct{
3838
Name: "Foo",
3939
Fields: []GoField{
40-
{Name: "Other", Type: "string", Tags: map[string]string{"json": "other"}},
41-
{Name: "Count", Type: "int64", Tags: map[string]string{"json": "count"}},
42-
{Name: "Count_2", Type: "int64", Tags: map[string]string{"json": "count_2"}},
43-
{Name: "Tags", Type: "[]string", Tags: map[string]string{"json": "tags"}},
40+
{Name: "Other", Type: "string", Tags: map[string]string{"json:": "other"}},
41+
{Name: "Count", Type: "int64", Tags: map[string]string{"json:": "count"}},
42+
{Name: "Count_2", Type: "int64", Tags: map[string]string{"json:": "count_2"}},
43+
{Name: "Tags", Type: "[]string", Tags: map[string]string{"json:": "tags"}},
4444
},
4545
}
4646
if diff := cmp.Diff(expected, actual); diff != "" {

internal/dinosql/parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,7 @@ type PackageSettings struct {
10001000
Schema string `json:"schema"`
10011001
Queries string `json:"queries"`
10021002
EmitPreparedQueries bool `json:"emit_prepared_queries"`
1003-
EmitTags bool `json:"emit_tags"`
1003+
EmitJSONTags bool `json:"emit_json_tags"`
10041004
}
10051005

10061006
type GenerateSettings struct {

0 commit comments

Comments
 (0)