From b5d1af3c98e0ef7e06818711906f3ffceb5b37bb Mon Sep 17 00:00:00 2001 From: andrey Date: Mon, 12 Jan 2026 11:47:47 +0300 Subject: [PATCH] Kotlin: check methods to contains name 'ids' and with type List convert to List --- kotlin/kotlin_client.go | 20 ++++++++++---------- kotlin/testdata/arith.go | 8 ++++++-- kotlin/testdata/protocol.generated.kt | 10 +++++++++- kotlin/testdata/rpc.generated.kt | 2 +- kotlin/testdata/testdata_zenrpc.go | 18 ++++++++++++++++-- 5 files changed, 42 insertions(+), 16 deletions(-) diff --git a/kotlin/kotlin_client.go b/kotlin/kotlin_client.go index 1723de4..de4810e 100644 --- a/kotlin/kotlin_client.go +++ b/kotlin/kotlin_client.go @@ -36,7 +36,7 @@ const ( DefaultMap = "emptyMap()" DefaultLocalTime = "LocalTime.now()" - version = "1.0.0" + version = "1.0.1" ) var ( @@ -139,7 +139,6 @@ func (g *Generator) prepareTemplateData() templateData { data := templateData{GeneratorData: gen.DefaultGeneratorData().AddLangAndLocalVersion(version, "kotlin"), PackageAPI: g.settings.PackageAPI, Imports: g.settings.Imports, Class: g.settings.Class} modelsMap := make(map[string]Model) - servicesMap := make(map[string][]Method) for serviceName, service := range g.schema.Services { var ( @@ -166,14 +165,8 @@ func (g *Generator) prepareTemplateData() templateData { params = append(params, p) } method.Parameters = params + normalizeMethodReturnType(&method) data.Methods = append(data.Methods, method) - if g.settings.IsProtocol { - parts := strings.SplitN(method.Name, ".", 2) - if len(parts) != 2 { - continue - } - servicesMap[parts[0]] = append(servicesMap[parts[0]], method) - } } for _, v := range modelsMap { @@ -188,6 +181,13 @@ func (g *Generator) prepareTemplateData() templateData { return data } +// normalizeMethodReturnType check suffix ids and type list convert to List +func normalizeMethodReturnType(method *Method) { + if strings.HasSuffix(strings.ToLower(method.Name), "ids") && method.Returns.Type == "List" { + method.Returns.Type = "List" + } +} + func (g *Generator) fillIsInitial(data *templateData) { modelByName := make(map[string]*Model, len(data.Models)) for i := range data.Models { @@ -468,7 +468,7 @@ func kotlinDefault(smdType string) string { } if strings.HasPrefix(smdType, Map) { - return DefaultList + return DefaultMap } return smdType diff --git a/kotlin/testdata/arith.go b/kotlin/testdata/arith.go index fdee625..6e9cc87 100644 --- a/kotlin/testdata/arith.go +++ b/kotlin/testdata/arith.go @@ -17,8 +17,8 @@ type Point struct { 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 + BaseID int64 `json:"baseId"` // version id - 2 + SecondID int32 `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 @@ -48,6 +48,10 @@ func (as ArithService) GetByID(ctx context.Context, cartId string, categoryId in return &Point{}, nil } +func (as ArithService) GetSecondIds(ctx context.Context) []int { + return nil +} + func (as ArithService) GetByLatLong(ctx context.Context, categoryId int, baseID int, lat, lon float64) (*Point, error) { return &Point{}, nil } diff --git a/kotlin/testdata/protocol.generated.kt b/kotlin/testdata/protocol.generated.kt index 6840db5..2feb4de 100644 --- a/kotlin/testdata/protocol.generated.kt +++ b/kotlin/testdata/protocol.generated.kt @@ -1,4 +1,4 @@ -/// Code generated from jsonrpc schema by rpcgen v2.5.x with kotlin v1.0.0; DO NOT EDIT. +/// Code generated from jsonrpc schema by rpcgen v2.5.x with kotlin v1.0.1; DO NOT EDIT. package api import com.google.gson.reflect.TypeToken @@ -171,6 +171,14 @@ interface Api : Transport { "arith.GetPoints", ) + fun arithGetSecondIds( + vararg transportOptions: TransportOption, + ) = request( + transportOptions, + object : TypeToken>>() {}, + "arith.GetSecondIds", + ) + /** * Multiply multiples two digits and returns result. * @return diff --git a/kotlin/testdata/rpc.generated.kt b/kotlin/testdata/rpc.generated.kt index 26156c8..ef61d6c 100644 --- a/kotlin/testdata/rpc.generated.kt +++ b/kotlin/testdata/rpc.generated.kt @@ -1,4 +1,4 @@ -/// Code generated from jsonrpc schema by rpcgen v2.5.x with kotlin v1.0.0; DO NOT EDIT. +/// Code generated from jsonrpc schema by rpcgen v2.5.x with kotlin v1.0.1; DO NOT EDIT. package api.model import java.time.LocalTime diff --git a/kotlin/testdata/testdata_zenrpc.go b/kotlin/testdata/testdata_zenrpc.go index 86ecdf2..55ff2c8 100644 --- a/kotlin/testdata/testdata_zenrpc.go +++ b/kotlin/testdata/testdata_zenrpc.go @@ -11,10 +11,11 @@ import ( ) var RPC = struct { - ArithService struct{ GetByID, GetByLatLong, GetByTime, Sum, Positive, DoSomething, DoSomethingV2, GetPoints, DoSomethingWithPoint, Multiply, CheckError, CheckZenRPCError, Divide, Pow, Pi, SumArray string } + ArithService struct{ GetByID, GetSecondIds, GetByLatLong, GetByTime, Sum, Positive, DoSomething, DoSomethingV2, GetPoints, DoSomethingWithPoint, Multiply, CheckError, CheckZenRPCError, Divide, Pow, Pi, SumArray string } }{ - ArithService: struct{ GetByID, GetByLatLong, GetByTime, Sum, Positive, DoSomething, DoSomethingV2, GetPoints, DoSomethingWithPoint, Multiply, CheckError, CheckZenRPCError, Divide, Pow, Pi, SumArray string }{ + ArithService: struct{ GetByID, GetSecondIds, GetByLatLong, GetByTime, Sum, Positive, DoSomething, DoSomethingV2, GetPoints, DoSomethingWithPoint, Multiply, CheckError, CheckZenRPCError, Divide, Pow, Pi, SumArray string }{ GetByID: "getbyid", + GetSecondIds: "getsecondids", GetByLatLong: "getbylatlong", GetByTime: "getbytime", Sum: "sum", @@ -382,6 +383,16 @@ func (ArithService) SMD() smd.ServiceInfo { }, }, }, + "GetSecondIds": { + Parameters: []smd.JSONSchema{}, + Returns: smd.JSONSchema{ + Type: smd.Array, + TypeName: "[]", + Items: map[string]string{ + "type": smd.Integer, + }, + }, + }, "GetByLatLong": { Parameters: []smd.JSONSchema{ { @@ -2322,6 +2333,9 @@ func (s ArithService) Invoke(ctx context.Context, method string, params json.Raw resp.Set(s.GetByID(ctx, args.CartId, args.CategoryId, args.BaseID, args.Id)) + case RPC.ArithService.GetSecondIds: + resp.Set(s.GetSecondIds(ctx)) + case RPC.ArithService.GetByLatLong: var args = struct { CategoryId int `json:"categoryId"`