Skip to content

Commit

Permalink
change proto field type
Browse files Browse the repository at this point in the history
  • Loading branch information
zhufuyi committed Mar 10, 2024
1 parent adf3750 commit a68584f
Show file tree
Hide file tree
Showing 19 changed files with 260 additions and 222 deletions.
2 changes: 2 additions & 0 deletions .github/RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@

- Upgrade `go.opentelemetry.io/otel` and `golang.org/x/crypto` third-party libraries.
- Adjusting the order of import packages.
- Fix generate code based on postgresql bug [#25](https://github.com/zhufuyi/sponge/issues/25).
- Modify type,go type `time.Time` --> protobuf `string RFC3339`
338 changes: 169 additions & 169 deletions api/serverNameExample/v1/userExample.pb.go

Large diffs are not rendered by default.

11 changes: 10 additions & 1 deletion api/serverNameExample/v1/userExample.pb.validate.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions api/serverNameExample/v1/userExample.proto
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,8 @@ message UserExample {
GenderType gender = 7; // gender, 1:Male, 2:Female, other values:unknown
int32 status = 8; // account status
int64 loginAt = 9; // login timestamp
int64 createdAt = 10; // creation time
int64 updatedAt = 11; // update time
string createdAt = 10; // creation time
string updatedAt = 11; // update time
}

message GetUserExampleByIDRequest {
Expand Down
2 changes: 1 addition & 1 deletion api/serverNameExample/v1/userExample_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/types/types.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions cmd/sponge/commands/generate/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,6 @@ func InitSqlite() {
}
}`

embedTimeCode = `value.CreatedAt = record.CreatedAt.Unix()
value.UpdatedAt = record.UpdatedAt.Unix()`
embedTimeCode = `value.CreatedAt = record.CreatedAt.Format(time.RFC3339)
value.UpdatedAt = record.UpdatedAt.Format(time.RFC3339)`
)
6 changes: 2 additions & 4 deletions docs/apis.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -631,12 +631,10 @@
"format": "int64"
},
"createdAt": {
"type": "integer",
"format": "int64"
"type": "string"
},
"updatedAt": {
"type": "integer",
"format": "int64"
"type": "string"
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/config/serverNameExample_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package config
import (
"testing"

"github.com/zhufuyi/sponge/configs"
"github.com/stretchr/testify/assert"

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

"github.com/stretchr/testify/assert"
"github.com/zhufuyi/sponge/configs"
)

func TestInit(t *testing.T) {
Expand Down
6 changes: 4 additions & 2 deletions internal/handler/userExample_logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"math"
"strings"
"time"

"github.com/jinzhu/copier"

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

var _ time.Time // import time
var _ serverNameExampleV1.UserExampleLogicer = (*userExamplePbHandler)(nil)

type userExamplePbHandler struct {
Expand Down Expand Up @@ -300,8 +302,8 @@ func convertUserExamplePb(record *model.UserExample) (*serverNameExampleV1.UserE
// todo if copier.Copy cannot assign a value to a field, add it here, e.g. CreatedAt, UpdatedAt
// todo generate the conversion createdAt and updatedAt code here
// delete the templates code start
value.CreatedAt = record.CreatedAt.Unix()
value.UpdatedAt = record.UpdatedAt.Unix()
value.CreatedAt = record.CreatedAt.Format(time.RFC3339)
value.UpdatedAt = record.UpdatedAt.Format(time.RFC3339)
// delete the templates code end
return value, nil
}
6 changes: 4 additions & 2 deletions internal/handler/userExample_logic.go.mgo
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"strings"
"time"

"github.com/jinzhu/copier"

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

var _ time.Time // import time
var _ serverNameExampleV1.UserExampleLogicer = (*userExamplePbHandler)(nil)

type userExamplePbHandler struct {
Expand Down Expand Up @@ -303,8 +305,8 @@ func convertUserExamplePb(record *model.UserExample) (*serverNameExampleV1.UserE
// todo if copier.Copy cannot assign a value to a field, add it here, e.g. CreatedAt, UpdatedAt
// todo generate the conversion createdAt and updatedAt code here
// delete the templates code start
value.CreatedAt = record.CreatedAt.Unix()
value.UpdatedAt = record.UpdatedAt.Unix()
value.CreatedAt = record.CreatedAt.Format(time.RFC3339)
value.UpdatedAt = record.UpdatedAt.Format(time.RFC3339)
// delete the templates code end
return value, nil
}
6 changes: 3 additions & 3 deletions internal/handler/userExample_logic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func newUserExamplePbHandler() *gotest.Handler {
Path: "/userExample/list",
HandlerFunc: func(c *gin.Context) {
req := &serverNameExampleV1.ListUserExampleByLastIDRequest{}
_ = c.ShouldBindJSON(req)
_ = c.ShouldBindQuery(req)
_, err := iHandler.ListByLastID(c, req)
if err != nil {
response.Error(c, ecode.ErrListByLastIDUserExample)
Expand Down Expand Up @@ -456,7 +456,7 @@ func Test_userExamplePbHandler_ListByLastID(t *testing.T) {
h.MockDao.SQLMock.ExpectQuery("SELECT .*").WillReturnRows(rows)

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

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

Expand Down
6 changes: 4 additions & 2 deletions internal/service/userExample.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"math"
"strings"
"time"

"github.com/jinzhu/copier"
"google.golang.org/grpc"
Expand All @@ -26,6 +27,7 @@ func init() {
})
}

var _ time.Time // import time
var _ serverNameExampleV1.UserExampleServer = (*userExample)(nil)

type userExample struct {
Expand Down Expand Up @@ -316,8 +318,8 @@ func convertUserExample(record *model.UserExample) (*serverNameExampleV1.UserExa
// todo if copier.Copy cannot assign a value to a field, add it here, e.g. CreatedAt, UpdatedAt
// todo generate the conversion createdAt and updatedAt code here
// delete the templates code start
value.CreatedAt = record.CreatedAt.Unix()
value.UpdatedAt = record.UpdatedAt.Unix()
value.CreatedAt = record.CreatedAt.Format(time.RFC3339)
value.UpdatedAt = record.UpdatedAt.Format(time.RFC3339)
// delete the templates code end
return value, nil
}
6 changes: 4 additions & 2 deletions internal/service/userExample.go.mgo
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"strings"
"time"

"github.com/jinzhu/copier"
"google.golang.org/grpc"
Expand All @@ -25,6 +26,7 @@ func init() {
})
}

var _ time.Time // import time
var _ serverNameExampleV1.UserExampleServer = (*userExample)(nil)

type userExample struct {
Expand Down Expand Up @@ -319,8 +321,8 @@ func convertUserExample(record *model.UserExample) (*serverNameExampleV1.UserExa
// todo if copier.Copy cannot assign a value to a field, add it here, e.g. CreatedAt, UpdatedAt
// todo generate the conversion createdAt and updatedAt code here
// delete the templates code start
value.CreatedAt = record.CreatedAt.Unix()
value.UpdatedAt = record.UpdatedAt.Unix()
value.CreatedAt = record.CreatedAt.Format(time.RFC3339)
value.UpdatedAt = record.UpdatedAt.Format(time.RFC3339)
// delete the templates code end
return value, nil
}
2 changes: 1 addition & 1 deletion internal/service/userExample_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func Test_userExampleService_ListByLastID(t *testing.T) {

reply, err := s.IServiceClient.(serverNameExampleV1.UserExampleClient).ListByLastID(s.Ctx, &serverNameExampleV1.ListUserExampleByLastIDRequest{
LastID: 0,
Limit: 0,
Limit: 10,
Sort: "",
})
assert.NoError(t, err)
Expand Down
Binary file modified pkg/ggorm/test_sqlite.db
Binary file not shown.
11 changes: 9 additions & 2 deletions pkg/sql2code/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -956,8 +956,8 @@ func goTypeToProto(fields []tmplField) []tmplField {
field.GoType = "int32"
case "uint":
field.GoType = "uint32"
case "time.Time":
field.GoType = "int64"
case "time.Time", "*time.Time":
field.GoType = "string"
case "float32":
field.GoType = "float"
case "float64":
Expand All @@ -978,6 +978,13 @@ func goTypeToProto(fields []tmplField) []tmplField {
} else if strings.Contains(field.GoType, "[]*") {
field.GoType = "repeated " + strings.ReplaceAll(field.GoType, "[]*", "")
}
if field.GoType == "[]time.Time" {
field.GoType = "repeated string"
}
} else {
if strings.ToLower(field.Name) == "id" {
field.GoType = "uint64"
}
}

newFields = append(newFields, field)
Expand Down
12 changes: 0 additions & 12 deletions pkg/sql2code/parser/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,9 @@ func GetSqliteTableInfo(dbFile string, tableName string) (string, error) {
return "", err
}

//sql = handleID(sql)
for k, v := range sqliteToMysqlTypeMap {
sql = strings.ReplaceAll(sql, k, v)
}

return sql, nil
}

//func handleID(sql string) string {
// re := regexp.MustCompile(`id\s+INTEGER`)
// matches := re.FindAllStringSubmatch(sql, -1)
//
// for _, match := range matches {
// sql = strings.ReplaceAll(sql, match[0], " id bigint unsigned")
// }
//
// return sql
//}
Loading

0 comments on commit a68584f

Please sign in to comment.