Skip to content

Commit 471d445

Browse files
qnqatopandrey
andauthored
Update kotlin package, update test (#33)
Co-authored-by: andrey <palchikov@uteka.ru>
1 parent 275ec0b commit 471d445

File tree

6 files changed

+1415
-19
lines changed

6 files changed

+1415
-19
lines changed

kotlin/kotlin_client.go

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ var (
5050
Long: DefaultInteger,
5151
}
5252
doubleSuffixes = map[string]struct{}{
53-
"Lat": {},
54-
"Lon": {},
55-
"Latitude": {},
56-
"Longitude": {},
53+
"lat": {},
54+
"lon": {},
55+
"latitude": {},
56+
"longitude": {},
5757
}
5858
reservedKeywords = []string{"as", "as?", "break", "class", "continue", "do", "else", "false", "for", "fun", "if",
5959
"in", "!in", "interface", "is", "!is", "null", "object", "package", "return", "super", "this", "throw", "true",
@@ -182,6 +182,7 @@ func (g *Generator) prepareTemplateData() templateData {
182182
g.sortTemplateData(&data)
183183
g.prepareModelFieldName(&data)
184184

185+
g.resolveTypes(&data)
185186
return data
186187
}
187188

@@ -288,6 +289,44 @@ func (g *Generator) executeTemplate(data templateData) ([]byte, error) {
288289
return buf.Bytes(), nil
289290
}
290291

292+
// resolveTypes last check and update params in methods
293+
func (g *Generator) resolveTypes(data *templateData) {
294+
for i := range data.Methods {
295+
method := &data.Methods[i]
296+
for j := range method.Parameters {
297+
param := &method.Parameters[j]
298+
if newType, ok := inferKotlinType(param.Type, param.Name); ok {
299+
param.setType(newType)
300+
}
301+
}
302+
}
303+
}
304+
305+
func inferKotlinType(valType, name string) (string, bool) {
306+
switch valType {
307+
case Int:
308+
if kotlinTypeID(name) {
309+
return Long, true
310+
}
311+
case Float:
312+
if kotlinTypeDouble(name) {
313+
return Double, true
314+
}
315+
case String:
316+
if kotlinTypeTimestamp(name) {
317+
return Timestamp, true
318+
}
319+
}
320+
return "", false
321+
}
322+
323+
// setType update all types
324+
func (p *Parameter) setType(t string) {
325+
p.Type = t
326+
p.BaseType = t
327+
p.ReturnType = t
328+
}
329+
291330
// propertiesToParams convert smd.PropertyList to []Parameter
292331
func (g *Generator) propertiesToParams(typeName string, list smd.PropertyList) []Parameter {
293332
parameters := make([]Parameter, 0, len(list))
@@ -435,6 +474,7 @@ func kotlinDefault(smdType string) string {
435474

436475
// kotlinTypeDouble check if property need set type Double
437476
func kotlinTypeDouble(name string) bool {
477+
name = strings.ToLower(name)
438478
if _, ok := doubleSuffixes[name]; ok {
439479
return true
440480
}
@@ -448,17 +488,18 @@ func kotlinTypeDouble(name string) bool {
448488
return false
449489
}
450490

451-
// kotlinTypeID check if property is ID set type Long
491+
// kotlinTypeID check if property is ID or IDs set type Long
452492
func kotlinTypeID(name string) bool {
453493
return name == id ||
454494
strings.HasSuffix(name, strings.ToUpper(id)) ||
455495
strings.HasSuffix(name, "Id") ||
456-
strings.HasSuffix(name, "Ids")
496+
strings.HasSuffix(name, "Id"+"s") ||
497+
strings.HasSuffix(name, strings.ToUpper(id)+"s")
457498
}
458499

459500
// kotlinTypeTimestamp check if is time property set Timestamp
460501
func kotlinTypeTimestamp(name string) bool {
461-
return strings.HasSuffix(name, "edAt")
502+
return strings.HasSuffix(name, "At")
462503
}
463504

464505
func arrayType(items map[string]string, isReturnType bool) string {

kotlin/kotlin_client_test.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package kotlin
22

33
import (
44
"bytes"
5+
"flag"
56
"os"
67
"testing"
78

@@ -12,12 +13,13 @@ import (
1213
const testDataPath = "./testdata/rpc.generated.kt"
1314
const testDataProtocolPath = "./testdata/protocol.generated.kt"
1415

16+
var update = flag.Bool("update", false, "update .kt files")
17+
1518
func TestGenerator_Generate(t *testing.T) {
1619
type fields struct {
1720
settings Settings
1821
servicesMap map[string]zenrpc.Invoker
1922
}
20-
var replace bool
2123

2224
tests := []struct {
2325
name string
@@ -62,7 +64,7 @@ func TestGenerator_Generate(t *testing.T) {
6264
t.Fatalf("generate client: %v", err)
6365
}
6466

65-
if replace {
67+
if *update {
6668
var f *os.File
6769
f, err = os.Create(tt.outputFile)
6870
if err != nil {
@@ -91,8 +93,6 @@ func TestGenerator_Generate(t *testing.T) {
9193
}
9294

9395
func Test_kotlinTypeID(t *testing.T) {
94-
type args struct {
95-
}
9696
tests := []struct {
9797
name string
9898
typeName string
@@ -113,6 +113,11 @@ func Test_kotlinTypeID(t *testing.T) {
113113
typeName: "nameIds",
114114
want: true,
115115
},
116+
{
117+
name: "nameIDs",
118+
typeName: "nameIDs",
119+
want: true,
120+
},
116121
{
117122
name: "ID",
118123
typeName: "ID",
@@ -123,6 +128,11 @@ func Test_kotlinTypeID(t *testing.T) {
123128
typeName: "nameId",
124129
want: true,
125130
},
131+
{
132+
name: "nameID",
133+
typeName: "nameID",
134+
want: true,
135+
},
126136
}
127137
for _, tt := range tests {
128138
t.Run(tt.name, func(t *testing.T) {

kotlin/testdata/arith.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@ type ArithService struct{ zenrpc.Service }
1515
type Point struct {
1616
X, Y int // coordinate
1717
Z int `json:"-"`
18+
CartId string `json:"cartOd"` // version string id - 1
1819
ID int `json:"id"` // version id - 1
1920
BaseID int `json:"baseId"` // version id - 2
2021
SecondID int `json:"secondID"` // version id - 3
22+
SecondIDs []int `json:"secondIDs"` // version multi id
23+
CartIDs []string `json:"cartIDs"` // version multi string id
2124
CreatedAt string `json:"createdAt"` // version date - 1
2225
UpdatedAt string `json:"updatedAt"` // version date - 2
2326
ManualChangedAt string `json:"manualChangedAt"` // version date - 3
27+
StartAt string `json:"startAt"` // version date - 4
2428
NewLat float64 `json:"newLat"` // version group geo coordinate № - 1
2529
NewLon float64 `json:"newLon"` // version group geo coordinate № - 1
2630
Lat float64 `json:"lat"` // version group geo coordinate № - 2
@@ -40,6 +44,18 @@ type Point struct {
4044
type SecondPoint struct {
4145
}
4246

47+
func (as ArithService) GetByID(ctx context.Context, cartId string, categoryId int, baseID int, id int) (*Point, error) {
48+
return &Point{}, nil
49+
}
50+
51+
func (as ArithService) GetByLatLong(ctx context.Context, categoryId int, baseID int, lat, lon float64) (*Point, error) {
52+
return &Point{}, nil
53+
}
54+
55+
func (as ArithService) GetByTime(ctx context.Context, createdAt string, updateAt string, startAt string) (*Point, error) {
56+
return &Point{}, nil
57+
}
58+
4359
// Sum sums two digits and returns error with error code as result and IP from context.
4460
func (as ArithService) Sum(ctx context.Context, a, b int) (bool, *zenrpc.Error) {
4561
r, _ := zenrpc.RequestFromContext(ctx)

kotlin/testdata/protocol.generated.kt

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// Code generated from jsonrpc schema by rpcgen v2.4.8; DO NOT EDIT.
1+
/// Code generated from jsonrpc schema by rpcgen v2.7.0; DO NOT EDIT.
22
package api
33

44
import com.google.gson.reflect.TypeToken
@@ -108,6 +108,61 @@ interface Api : Transport {
108108
"pp" to pp,
109109
)
110110

111+
/**
112+
* @return
113+
*/
114+
fun arithGetByID(
115+
cartId: String,
116+
categoryId: Long,
117+
baseID: Long,
118+
id: Long,
119+
vararg transportOptions: TransportOption,
120+
) = request(
121+
transportOptions,
122+
object : TypeToken<ApiResponse<Point>>() {},
123+
"arith.GetByID",
124+
"cartId" to cartId,
125+
"categoryId" to categoryId,
126+
"baseID" to baseID,
127+
"id" to id,
128+
)
129+
130+
/**
131+
* @return
132+
*/
133+
fun arithGetByLatLong(
134+
categoryId: Long,
135+
baseID: Long,
136+
lat: Double,
137+
lon: Double,
138+
vararg transportOptions: TransportOption,
139+
) = request(
140+
transportOptions,
141+
object : TypeToken<ApiResponse<Point>>() {},
142+
"arith.GetByLatLong",
143+
"categoryId" to categoryId,
144+
"baseID" to baseID,
145+
"lat" to lat,
146+
"lon" to lon,
147+
)
148+
149+
/**
150+
* @return
151+
*/
152+
fun arithGetByTime(
153+
createdAt: ZonedDateTime,
154+
updateAt: ZonedDateTime,
155+
startAt: ZonedDateTime,
156+
vararg transportOptions: TransportOption,
157+
) = request(
158+
transportOptions,
159+
object : TypeToken<ApiResponse<Point>>() {},
160+
"arith.GetByTime",
161+
"createdAt" to createdAt,
162+
"updateAt" to updateAt,
163+
"startAt" to startAt,
164+
)
165+
111166
fun arithGetPoints(
112167
vararg transportOptions: TransportOption,
113168
) = request(

kotlin/testdata/rpc.generated.kt

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// Code generated from jsonrpc schema by rpcgen v2.4.8; DO NOT EDIT.
1+
/// Code generated from jsonrpc schema by rpcgen v2.7.0; DO NOT EDIT.
22
package api.model
33

44
import java.time.LocalTime
@@ -30,6 +30,14 @@ data class Point(
3030
* version id - 2
3131
*/
3232
val baseId: Long = 0,
33+
/**
34+
* version multi string id
35+
*/
36+
val cartIDs: List<String> = emptyList(),
37+
/**
38+
* version string id - 1
39+
*/
40+
val cartOd: String = "",
3341
val `class`: String = "",
3442
/**
3543
* version date - 1
@@ -43,19 +51,19 @@ data class Point(
4351
/**
4452
* version group geo coordinate № - 2
4553
*/
46-
val lat: Float = 0f,
54+
val lat: Double = 0.0,
4755
/**
4856
* version group geo coordinate № - 3
4957
*/
50-
val latitude: Float = 0f,
58+
val latitude: Double = 0.0,
5159
/**
5260
* version group geo coordinate № - 2
5361
*/
54-
val lon: Float = 0f,
62+
val lon: Double = 0.0,
5563
/**
5664
* version group geo coordinate № - 3
5765
*/
58-
val longitude: Float = 0f,
66+
val longitude: Double = 0.0,
5967
/**
6068
* version date - 3
6169
*/
@@ -78,8 +86,16 @@ data class Point(
7886
* version id - 3
7987
*/
8088
val secondID: Long = 0,
89+
/**
90+
* version multi id
91+
*/
92+
val secondIDs: List<Long> = emptyList(),
8193
val secondPoints: List<Point> = emptyList(),
8294
val secondQuotient: Quotient? = null,
95+
/**
96+
* version date - 4
97+
*/
98+
val startAt: ZonedDateTime = ZonedDateTime.now(),
8399
/**
84100
* version date - 2
85101
*/

0 commit comments

Comments
 (0)