Skip to content

Commit a68584f

Browse files
committed
change proto field type
1 parent adf3750 commit a68584f

19 files changed

+260
-222
lines changed

.github/RELEASE.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22

33
- Upgrade `go.opentelemetry.io/otel` and `golang.org/x/crypto` third-party libraries.
44
- Adjusting the order of import packages.
5+
- Fix generate code based on postgresql bug [#25](https://github.com/zhufuyi/sponge/issues/25).
6+
- Modify type,go type `time.Time` --> protobuf `string RFC3339`

api/serverNameExample/v1/userExample.pb.go

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

api/serverNameExample/v1/userExample.pb.validate.go

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

api/serverNameExample/v1/userExample.proto

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,8 @@ message UserExample {
271271
GenderType gender = 7; // gender, 1:Male, 2:Female, other values:unknown
272272
int32 status = 8; // account status
273273
int64 loginAt = 9; // login timestamp
274-
int64 createdAt = 10; // creation time
275-
int64 updatedAt = 11; // update time
274+
string createdAt = 10; // creation time
275+
string updatedAt = 11; // update time
276276
}
277277

278278
message GetUserExampleByIDRequest {

api/serverNameExample/v1/userExample_grpc.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/types/types.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/sponge/commands/generate/template.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,6 @@ func InitSqlite() {
586586
}
587587
}`
588588

589-
embedTimeCode = `value.CreatedAt = record.CreatedAt.Unix()
590-
value.UpdatedAt = record.UpdatedAt.Unix()`
589+
embedTimeCode = `value.CreatedAt = record.CreatedAt.Format(time.RFC3339)
590+
value.UpdatedAt = record.UpdatedAt.Format(time.RFC3339)`
591591
)

docs/apis.swagger.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -631,12 +631,10 @@
631631
"format": "int64"
632632
},
633633
"createdAt": {
634-
"type": "integer",
635-
"format": "int64"
634+
"type": "string"
636635
},
637636
"updatedAt": {
638-
"type": "integer",
639-
"format": "int64"
637+
"type": "string"
640638
}
641639
}
642640
}

internal/config/serverNameExample_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package config
33
import (
44
"testing"
55

6-
"github.com/zhufuyi/sponge/configs"
6+
"github.com/stretchr/testify/assert"
77

88
"github.com/zhufuyi/sponge/pkg/gofile"
99

10-
"github.com/stretchr/testify/assert"
10+
"github.com/zhufuyi/sponge/configs"
1111
)
1212

1313
func TestInit(t *testing.T) {

internal/handler/userExample_logic.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"math"
77
"strings"
8+
"time"
89

910
"github.com/jinzhu/copier"
1011

@@ -19,6 +20,7 @@ import (
1920
"github.com/zhufuyi/sponge/internal/model"
2021
)
2122

23+
var _ time.Time // import time
2224
var _ serverNameExampleV1.UserExampleLogicer = (*userExamplePbHandler)(nil)
2325

2426
type userExamplePbHandler struct {
@@ -300,8 +302,8 @@ func convertUserExamplePb(record *model.UserExample) (*serverNameExampleV1.UserE
300302
// todo if copier.Copy cannot assign a value to a field, add it here, e.g. CreatedAt, UpdatedAt
301303
// todo generate the conversion createdAt and updatedAt code here
302304
// delete the templates code start
303-
value.CreatedAt = record.CreatedAt.Unix()
304-
value.UpdatedAt = record.UpdatedAt.Unix()
305+
value.CreatedAt = record.CreatedAt.Format(time.RFC3339)
306+
value.UpdatedAt = record.UpdatedAt.Format(time.RFC3339)
305307
// delete the templates code end
306308
return value, nil
307309
}

internal/handler/userExample_logic.go.mgo

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"strings"
7+
"time"
78

89
"github.com/jinzhu/copier"
910

@@ -18,6 +19,7 @@ import (
1819
"github.com/zhufuyi/sponge/internal/model"
1920
)
2021

22+
var _ time.Time // import time
2123
var _ serverNameExampleV1.UserExampleLogicer = (*userExamplePbHandler)(nil)
2224

2325
type userExamplePbHandler struct {
@@ -303,8 +305,8 @@ func convertUserExamplePb(record *model.UserExample) (*serverNameExampleV1.UserE
303305
// todo if copier.Copy cannot assign a value to a field, add it here, e.g. CreatedAt, UpdatedAt
304306
// todo generate the conversion createdAt and updatedAt code here
305307
// delete the templates code start
306-
value.CreatedAt = record.CreatedAt.Unix()
307-
value.UpdatedAt = record.UpdatedAt.Unix()
308+
value.CreatedAt = record.CreatedAt.Format(time.RFC3339)
309+
value.UpdatedAt = record.UpdatedAt.Format(time.RFC3339)
308310
// delete the templates code end
309311
return value, nil
310312
}

internal/handler/userExample_logic_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func newUserExamplePbHandler() *gotest.Handler {
161161
Path: "/userExample/list",
162162
HandlerFunc: func(c *gin.Context) {
163163
req := &serverNameExampleV1.ListUserExampleByLastIDRequest{}
164-
_ = c.ShouldBindJSON(req)
164+
_ = c.ShouldBindQuery(req)
165165
_, err := iHandler.ListByLastID(c, req)
166166
if err != nil {
167167
response.Error(c, ecode.ErrListByLastIDUserExample)
@@ -456,7 +456,7 @@ func Test_userExamplePbHandler_ListByLastID(t *testing.T) {
456456
h.MockDao.SQLMock.ExpectQuery("SELECT .*").WillReturnRows(rows)
457457

458458
result := &gohttp.StdResult{}
459-
err := gohttp.Get(result, h.GetRequestURL("ListByLastID"), gohttp.KV{"lastID": 0, "size": 10})
459+
err := gohttp.Get(result, h.GetRequestURL("ListByLastID"), gohttp.KV{"lastID": 0, "limit": 10})
460460
if err != nil {
461461
t.Fatal(err)
462462
}
@@ -465,7 +465,7 @@ func Test_userExamplePbHandler_ListByLastID(t *testing.T) {
465465
}
466466

467467
// get error test
468-
err = gohttp.Get(result, h.GetRequestURL("ListByLastID"), gohttp.KV{"lastID": 0, "size": 10, "sort": "unknown-column"})
468+
err = gohttp.Get(result, h.GetRequestURL("ListByLastID"), gohttp.KV{"lastID": 0, "limit": 10, "sort": "unknown-column"})
469469
assert.NoError(t, err)
470470
}
471471

internal/service/userExample.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"math"
77
"strings"
8+
"time"
89

910
"github.com/jinzhu/copier"
1011
"google.golang.org/grpc"
@@ -26,6 +27,7 @@ func init() {
2627
})
2728
}
2829

30+
var _ time.Time // import time
2931
var _ serverNameExampleV1.UserExampleServer = (*userExample)(nil)
3032

3133
type userExample struct {
@@ -316,8 +318,8 @@ func convertUserExample(record *model.UserExample) (*serverNameExampleV1.UserExa
316318
// todo if copier.Copy cannot assign a value to a field, add it here, e.g. CreatedAt, UpdatedAt
317319
// todo generate the conversion createdAt and updatedAt code here
318320
// delete the templates code start
319-
value.CreatedAt = record.CreatedAt.Unix()
320-
value.UpdatedAt = record.UpdatedAt.Unix()
321+
value.CreatedAt = record.CreatedAt.Format(time.RFC3339)
322+
value.UpdatedAt = record.UpdatedAt.Format(time.RFC3339)
321323
// delete the templates code end
322324
return value, nil
323325
}

internal/service/userExample.go.mgo

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"strings"
7+
"time"
78

89
"github.com/jinzhu/copier"
910
"google.golang.org/grpc"
@@ -25,6 +26,7 @@ func init() {
2526
})
2627
}
2728

29+
var _ time.Time // import time
2830
var _ serverNameExampleV1.UserExampleServer = (*userExample)(nil)
2931

3032
type userExample struct {
@@ -319,8 +321,8 @@ func convertUserExample(record *model.UserExample) (*serverNameExampleV1.UserExa
319321
// todo if copier.Copy cannot assign a value to a field, add it here, e.g. CreatedAt, UpdatedAt
320322
// todo generate the conversion createdAt and updatedAt code here
321323
// delete the templates code start
322-
value.CreatedAt = record.CreatedAt.Unix()
323-
value.UpdatedAt = record.UpdatedAt.Unix()
324+
value.CreatedAt = record.CreatedAt.Format(time.RFC3339)
325+
value.UpdatedAt = record.UpdatedAt.Format(time.RFC3339)
324326
// delete the templates code end
325327
return value, nil
326328
}

internal/service/userExample_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ func Test_userExampleService_ListByLastID(t *testing.T) {
300300

301301
reply, err := s.IServiceClient.(serverNameExampleV1.UserExampleClient).ListByLastID(s.Ctx, &serverNameExampleV1.ListUserExampleByLastIDRequest{
302302
LastID: 0,
303-
Limit: 0,
303+
Limit: 10,
304304
Sort: "",
305305
})
306306
assert.NoError(t, err)

pkg/ggorm/test_sqlite.db

0 Bytes
Binary file not shown.

pkg/sql2code/parser/parser.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -956,8 +956,8 @@ func goTypeToProto(fields []tmplField) []tmplField {
956956
field.GoType = "int32"
957957
case "uint":
958958
field.GoType = "uint32"
959-
case "time.Time":
960-
field.GoType = "int64"
959+
case "time.Time", "*time.Time":
960+
field.GoType = "string"
961961
case "float32":
962962
field.GoType = "float"
963963
case "float64":
@@ -978,6 +978,13 @@ func goTypeToProto(fields []tmplField) []tmplField {
978978
} else if strings.Contains(field.GoType, "[]*") {
979979
field.GoType = "repeated " + strings.ReplaceAll(field.GoType, "[]*", "")
980980
}
981+
if field.GoType == "[]time.Time" {
982+
field.GoType = "repeated string"
983+
}
984+
} else {
985+
if strings.ToLower(field.Name) == "id" {
986+
field.GoType = "uint64"
987+
}
981988
}
982989

983990
newFields = append(newFields, field)

pkg/sql2code/parser/sqlite.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,9 @@ func GetSqliteTableInfo(dbFile string, tableName string) (string, error) {
2929
return "", err
3030
}
3131

32-
//sql = handleID(sql)
3332
for k, v := range sqliteToMysqlTypeMap {
3433
sql = strings.ReplaceAll(sql, k, v)
3534
}
3635

3736
return sql, nil
3837
}
39-
40-
//func handleID(sql string) string {
41-
// re := regexp.MustCompile(`id\s+INTEGER`)
42-
// matches := re.FindAllStringSubmatch(sql, -1)
43-
//
44-
// for _, match := range matches {
45-
// sql = strings.ReplaceAll(sql, match[0], " id bigint unsigned")
46-
// }
47-
//
48-
// return sql
49-
//}

0 commit comments

Comments
 (0)