Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 48 additions & 7 deletions kotlin/kotlin_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ var (
Long: DefaultInteger,
}
doubleSuffixes = map[string]struct{}{
"Lat": {},
"Lon": {},
"Latitude": {},
"Longitude": {},
"lat": {},
"lon": {},
"latitude": {},
"longitude": {},
}
reservedKeywords = []string{"as", "as?", "break", "class", "continue", "do", "else", "false", "for", "fun", "if",
"in", "!in", "interface", "is", "!is", "null", "object", "package", "return", "super", "this", "throw", "true",
Expand Down Expand Up @@ -182,6 +182,7 @@ func (g *Generator) prepareTemplateData() templateData {
g.sortTemplateData(&data)
g.prepareModelFieldName(&data)

g.resolveTypes(&data)
return data
}

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

// resolveTypes last check and update params in methods
func (g *Generator) resolveTypes(data *templateData) {
for i := range data.Methods {
method := &data.Methods[i]
for j := range method.Parameters {
param := &method.Parameters[j]
if newType, ok := inferKotlinType(param.Type, param.Name); ok {
param.setType(newType)
}
}
}
}

func inferKotlinType(valType, name string) (string, bool) {
switch valType {
case Int:
if kotlinTypeID(name) {
return Long, true
}
case Float:
if kotlinTypeDouble(name) {
return Double, true
}
case String:
if kotlinTypeTimestamp(name) {
return Timestamp, true
}
}
return "", false
}

// setType update all types
func (p *Parameter) setType(t string) {
p.Type = t
p.BaseType = t
p.ReturnType = t
}

// propertiesToParams convert smd.PropertyList to []Parameter
func (g *Generator) propertiesToParams(typeName string, list smd.PropertyList) []Parameter {
parameters := make([]Parameter, 0, len(list))
Expand Down Expand Up @@ -435,6 +474,7 @@ func kotlinDefault(smdType string) string {

// kotlinTypeDouble check if property need set type Double
func kotlinTypeDouble(name string) bool {
name = strings.ToLower(name)
if _, ok := doubleSuffixes[name]; ok {
return true
}
Expand All @@ -448,17 +488,18 @@ func kotlinTypeDouble(name string) bool {
return false
}

// kotlinTypeID check if property is ID set type Long
// kotlinTypeID check if property is ID or IDs set type Long
func kotlinTypeID(name string) bool {
return name == id ||
strings.HasSuffix(name, strings.ToUpper(id)) ||
strings.HasSuffix(name, "Id") ||
strings.HasSuffix(name, "Ids")
strings.HasSuffix(name, "Id"+"s") ||
strings.HasSuffix(name, strings.ToUpper(id)+"s")
}

// kotlinTypeTimestamp check if is time property set Timestamp
func kotlinTypeTimestamp(name string) bool {
return strings.HasSuffix(name, "edAt")
return strings.HasSuffix(name, "At")
}

func arrayType(items map[string]string, isReturnType bool) string {
Expand Down
18 changes: 14 additions & 4 deletions kotlin/kotlin_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kotlin

import (
"bytes"
"flag"
"os"
"testing"

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

var update = flag.Bool("update", false, "update .kt files")

func TestGenerator_Generate(t *testing.T) {
type fields struct {
settings Settings
servicesMap map[string]zenrpc.Invoker
}
var replace bool

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

if replace {
if *update {
var f *os.File
f, err = os.Create(tt.outputFile)
if err != nil {
Expand Down Expand Up @@ -91,8 +93,6 @@ func TestGenerator_Generate(t *testing.T) {
}

func Test_kotlinTypeID(t *testing.T) {
type args struct {
}
tests := []struct {
name string
typeName string
Expand All @@ -113,6 +113,11 @@ func Test_kotlinTypeID(t *testing.T) {
typeName: "nameIds",
want: true,
},
{
name: "nameIDs",
typeName: "nameIDs",
want: true,
},
{
name: "ID",
typeName: "ID",
Expand All @@ -123,6 +128,11 @@ func Test_kotlinTypeID(t *testing.T) {
typeName: "nameId",
want: true,
},
{
name: "nameID",
typeName: "nameID",
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
16 changes: 16 additions & 0 deletions kotlin/testdata/arith.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ type ArithService struct{ zenrpc.Service }
type Point struct {
X, Y int // coordinate
Z int `json:"-"`
CartId string `json:"cartOd"` // version string id - 1
ID int `json:"id"` // version id - 1
BaseID int `json:"baseId"` // version id - 2
SecondID int `json:"secondID"` // version id - 3
SecondIDs []int `json:"secondIDs"` // version multi id
CartIDs []string `json:"cartIDs"` // version multi string id
CreatedAt string `json:"createdAt"` // version date - 1
UpdatedAt string `json:"updatedAt"` // version date - 2
ManualChangedAt string `json:"manualChangedAt"` // version date - 3
StartAt string `json:"startAt"` // version date - 4
NewLat float64 `json:"newLat"` // version group geo coordinate № - 1
NewLon float64 `json:"newLon"` // version group geo coordinate № - 1
Lat float64 `json:"lat"` // version group geo coordinate № - 2
Expand All @@ -40,6 +44,18 @@ type Point struct {
type SecondPoint struct {
}

func (as ArithService) GetByID(ctx context.Context, cartId string, categoryId int, baseID int, id int) (*Point, error) {
return &Point{}, nil
}

func (as ArithService) GetByLatLong(ctx context.Context, categoryId int, baseID int, lat, lon float64) (*Point, error) {
return &Point{}, nil
}

func (as ArithService) GetByTime(ctx context.Context, createdAt string, updateAt string, startAt string) (*Point, error) {
return &Point{}, nil
}

// Sum sums two digits and returns error with error code as result and IP from context.
func (as ArithService) Sum(ctx context.Context, a, b int) (bool, *zenrpc.Error) {
r, _ := zenrpc.RequestFromContext(ctx)
Expand Down
57 changes: 56 additions & 1 deletion kotlin/testdata/protocol.generated.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// Code generated from jsonrpc schema by rpcgen v2.4.8; DO NOT EDIT.
/// Code generated from jsonrpc schema by rpcgen v2.7.0; DO NOT EDIT.
package api

import com.google.gson.reflect.TypeToken
Expand Down Expand Up @@ -108,6 +108,61 @@ interface Api : Transport {
"pp" to pp,
)

/**
* @return
*/
fun arithGetByID(
cartId: String,
categoryId: Long,
baseID: Long,
id: Long,
vararg transportOptions: TransportOption,
) = request(
transportOptions,
object : TypeToken<ApiResponse<Point>>() {},
"arith.GetByID",
"cartId" to cartId,
"categoryId" to categoryId,
"baseID" to baseID,
"id" to id,
)

/**
* @return
*/
fun arithGetByLatLong(
categoryId: Long,
baseID: Long,
lat: Double,
lon: Double,
vararg transportOptions: TransportOption,
) = request(
transportOptions,
object : TypeToken<ApiResponse<Point>>() {},
"arith.GetByLatLong",
"categoryId" to categoryId,
"baseID" to baseID,
"lat" to lat,
"lon" to lon,
)

/**
* @return
*/
fun arithGetByTime(
createdAt: ZonedDateTime,
updateAt: ZonedDateTime,
startAt: ZonedDateTime,
vararg transportOptions: TransportOption,
) = request(
transportOptions,
object : TypeToken<ApiResponse<Point>>() {},
"arith.GetByTime",
"createdAt" to createdAt,
"updateAt" to updateAt,
"startAt" to startAt,
)

fun arithGetPoints(
vararg transportOptions: TransportOption,
) = request(
Expand Down
26 changes: 21 additions & 5 deletions kotlin/testdata/rpc.generated.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// Code generated from jsonrpc schema by rpcgen v2.4.8; DO NOT EDIT.
/// Code generated from jsonrpc schema by rpcgen v2.7.0; DO NOT EDIT.
package api.model

import java.time.LocalTime
Expand Down Expand Up @@ -30,6 +30,14 @@ data class Point(
* version id - 2
*/
val baseId: Long = 0,
/**
* version multi string id
*/
val cartIDs: List<String> = emptyList(),
/**
* version string id - 1
*/
val cartOd: String = "",
val `class`: String = "",
/**
* version date - 1
Expand All @@ -43,19 +51,19 @@ data class Point(
/**
* version group geo coordinate № - 2
*/
val lat: Float = 0f,
val lat: Double = 0.0,
/**
* version group geo coordinate № - 3
*/
val latitude: Float = 0f,
val latitude: Double = 0.0,
/**
* version group geo coordinate № - 2
*/
val lon: Float = 0f,
val lon: Double = 0.0,
/**
* version group geo coordinate № - 3
*/
val longitude: Float = 0f,
val longitude: Double = 0.0,
/**
* version date - 3
*/
Expand All @@ -78,8 +86,16 @@ data class Point(
* version id - 3
*/
val secondID: Long = 0,
/**
* version multi id
*/
val secondIDs: List<Long> = emptyList(),
val secondPoints: List<Point> = emptyList(),
val secondQuotient: Quotient? = null,
/**
* version date - 4
*/
val startAt: ZonedDateTime = ZonedDateTime.now(),
/**
* version date - 2
*/
Expand Down
Loading