Skip to content

Commit dca3285

Browse files
hosseinkarami-devGitHub Actions Bot
andauthored
chore: regenerate client from OpenAPI (#90)
Co-authored-by: GitHub Actions Bot <automation@github.com>
1 parent 8324ec3 commit dca3285

File tree

2 files changed

+47
-46
lines changed

2 files changed

+47
-46
lines changed
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
package io.github.hosseinkarami_dev.near.rpc.models
22

33
import io.github.hosseinkarami_dev.near.rpc.serializers.GlobalContractIdentifierViewSerializer
4+
import kotlinx.serialization.SerialName
45
import kotlinx.serialization.Serializable
56

67
@Serializable(with = GlobalContractIdentifierViewSerializer::class)
78
public sealed class GlobalContractIdentifierView {
89
@Serializable
9-
public data class CryptoHash(
10-
public val `value`: io.github.hosseinkarami_dev.near.rpc.models.CryptoHash,
10+
public data class Hash(
11+
@SerialName("hash")
12+
public val hash: CryptoHash,
1113
) : GlobalContractIdentifierView()
1214

1315
@Serializable
1416
public data class AccountId(
15-
public val `value`: io.github.hosseinkarami_dev.near.rpc.models.AccountId,
17+
@SerialName("account_id")
18+
public val accountId: io.github.hosseinkarami_dev.near.rpc.models.AccountId,
1619
) : GlobalContractIdentifierView()
1720
}

models/src/main/kotlin/io/github/hosseinkarami_dev/near/rpc/serializers/GlobalContractIdentifierViewSerializer.kt

Lines changed: 41 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,32 @@ import kotlinx.serialization.json.*
2424
object GlobalContractIdentifierViewSerializer : KSerializer<GlobalContractIdentifierView> {
2525

2626
override val descriptor: SerialDescriptor = buildClassSerialDescriptor("io.github.hosseinkarami_dev.near.rpc.models.GlobalContractIdentifierView") {
27-
element("CryptoHash", serializer<JsonElement>().descriptor)
28-
element("AccountId", serializer<JsonElement>().descriptor)
27+
element("hash", serializer<JsonElement>().descriptor)
28+
element("account_id", serializer<JsonElement>().descriptor)
2929
}
3030

3131
override fun serialize(encoder: Encoder, value: GlobalContractIdentifierView) {
3232
if (encoder is JsonEncoder) {
3333
val jsonEncoder = encoder
3434
when (value) {
35-
is io.github.hosseinkarami_dev.near.rpc.models.GlobalContractIdentifierView.CryptoHash -> {
36-
val innerElem = jsonEncoder.json.encodeToJsonElement(serializer<io.github.hosseinkarami_dev.near.rpc.models.CryptoHash>(), value.value)
37-
jsonEncoder.encodeJsonElement(innerElem)
35+
is io.github.hosseinkarami_dev.near.rpc.models.GlobalContractIdentifierView.Hash -> {
36+
val map = mutableMapOf<String, JsonElement>()
37+
map["hash"] = jsonEncoder.json.encodeToJsonElement(serializer<io.github.hosseinkarami_dev.near.rpc.models.CryptoHash>(), value.hash)
38+
val payload = JsonObject(map)
39+
jsonEncoder.encodeJsonElement(payload)
3840
}
3941
is io.github.hosseinkarami_dev.near.rpc.models.GlobalContractIdentifierView.AccountId -> {
40-
val innerElem = jsonEncoder.json.encodeToJsonElement(serializer<io.github.hosseinkarami_dev.near.rpc.models.AccountId>(), value.value)
41-
jsonEncoder.encodeJsonElement(innerElem)
42+
val map = mutableMapOf<String, JsonElement>()
43+
map["account_id"] = jsonEncoder.json.encodeToJsonElement(serializer<io.github.hosseinkarami_dev.near.rpc.models.AccountId>(), value.accountId)
44+
val payload = JsonObject(map)
45+
jsonEncoder.encodeJsonElement(payload)
4246
}
4347
}
4448
return
4549
}
4650
val out = encoder.beginStructure(descriptor)
4751
when (value) {
48-
is io.github.hosseinkarami_dev.near.rpc.models.GlobalContractIdentifierView.CryptoHash -> out.encodeSerializableElement(descriptor, 0, serializer<io.github.hosseinkarami_dev.near.rpc.models.GlobalContractIdentifierView.CryptoHash>(), value)
52+
is io.github.hosseinkarami_dev.near.rpc.models.GlobalContractIdentifierView.Hash -> out.encodeSerializableElement(descriptor, 0, serializer<io.github.hosseinkarami_dev.near.rpc.models.GlobalContractIdentifierView.Hash>(), value)
4953
is io.github.hosseinkarami_dev.near.rpc.models.GlobalContractIdentifierView.AccountId -> out.encodeSerializableElement(descriptor, 1, serializer<io.github.hosseinkarami_dev.near.rpc.models.GlobalContractIdentifierView.AccountId>(), value)
5054
}
5155
out.endStructure(descriptor)
@@ -59,39 +63,33 @@ object GlobalContractIdentifierViewSerializer : KSerializer<GlobalContractIdenti
5963
if (element.isString) {
6064
val s = element.content
6165
}
62-
try {
63-
val payload = decoder.json.decodeFromJsonElement(serializer<io.github.hosseinkarami_dev.near.rpc.models.CryptoHash>(), element)
64-
return io.github.hosseinkarami_dev.near.rpc.models.GlobalContractIdentifierView.CryptoHash(payload)
65-
} catch (_: Exception) { }
66-
try {
67-
val payload = decoder.json.decodeFromJsonElement(serializer<io.github.hosseinkarami_dev.near.rpc.models.AccountId>(), element)
68-
return io.github.hosseinkarami_dev.near.rpc.models.GlobalContractIdentifierView.AccountId(payload)
69-
} catch (_: Exception) { }
7066
throw SerializationException("Unknown discriminator (primitive) for GlobalContractIdentifierView")
7167
}
7268

7369
is JsonArray -> throw SerializationException("Unexpected JSON array while deserializing GlobalContractIdentifierView")
7470

7571
is JsonObject -> {
7672
val jobj = element
77-
val knownVariantNames = setOf("CryptoHash", "AccountId")
78-
if (jobj["CryptoHash"] != null) {
79-
return io.github.hosseinkarami_dev.near.rpc.models.GlobalContractIdentifierView.CryptoHash(decoder.json.decodeFromJsonElement(serializer<io.github.hosseinkarami_dev.near.rpc.models.CryptoHash>(), jobj["CryptoHash"]!!))
73+
val knownVariantNames = setOf("hash", "account_id")
74+
if (jobj["hash"] != null) {
75+
return io.github.hosseinkarami_dev.near.rpc.models.GlobalContractIdentifierView.Hash(decoder.json.decodeFromJsonElement(serializer<io.github.hosseinkarami_dev.near.rpc.models.CryptoHash>(), jobj["hash"]!!))
8076
}
81-
if (jobj["AccountId"] != null) {
82-
return io.github.hosseinkarami_dev.near.rpc.models.GlobalContractIdentifierView.AccountId(decoder.json.decodeFromJsonElement(serializer<io.github.hosseinkarami_dev.near.rpc.models.AccountId>(), jobj["AccountId"]!!))
77+
if (jobj["account_id"] != null) {
78+
return io.github.hosseinkarami_dev.near.rpc.models.GlobalContractIdentifierView.AccountId(decoder.json.decodeFromJsonElement(serializer<io.github.hosseinkarami_dev.near.rpc.models.AccountId>(), jobj["account_id"]!!))
8379
}
8480
if (jobj.size == 1) {
8581
val entry = jobj.entries.first()
8682
val key = entry.key
8783
val valueElem = entry.value
8884
if (knownVariantNames.contains(key)) {
8985
when (key) {
90-
"CryptoHash" -> {
91-
return io.github.hosseinkarami_dev.near.rpc.models.GlobalContractIdentifierView.CryptoHash(decoder.json.decodeFromJsonElement(serializer<io.github.hosseinkarami_dev.near.rpc.models.CryptoHash>(), valueElem))
86+
"hash" -> {
87+
val obj = valueElem as? JsonObject ?: throw SerializationException("Expected object payload for variant hash: " + key)
88+
return decoder.json.decodeFromJsonElement(serializer<io.github.hosseinkarami_dev.near.rpc.models.GlobalContractIdentifierView.Hash>(), obj)
9289
}
93-
"AccountId" -> {
94-
return io.github.hosseinkarami_dev.near.rpc.models.GlobalContractIdentifierView.AccountId(decoder.json.decodeFromJsonElement(serializer<io.github.hosseinkarami_dev.near.rpc.models.AccountId>(), valueElem))
90+
"account_id" -> {
91+
val obj = valueElem as? JsonObject ?: throw SerializationException("Expected object payload for variant account_id: " + key)
92+
return decoder.json.decodeFromJsonElement(serializer<io.github.hosseinkarami_dev.near.rpc.models.GlobalContractIdentifierView.AccountId>(), obj)
9593
}
9694
else -> { /* knownVariantNames.contains(key) guards this branch; shouldn't reach here */ }
9795
}
@@ -112,39 +110,39 @@ object GlobalContractIdentifierViewSerializer : KSerializer<GlobalContractIdenti
112110
val tf = typeField.trim()
113111
// try exact match of full variant name first
114112
when (tf) {
115-
"CryptoHash" -> return decoder.json.decodeFromJsonElement(serializer<io.github.hosseinkarami_dev.near.rpc.models.GlobalContractIdentifierView.CryptoHash>(), jobj)
116-
"AccountId" -> return decoder.json.decodeFromJsonElement(serializer<io.github.hosseinkarami_dev.near.rpc.models.GlobalContractIdentifierView.AccountId>(), jobj)
113+
"hash" -> return decoder.json.decodeFromJsonElement(serializer<io.github.hosseinkarami_dev.near.rpc.models.GlobalContractIdentifierView.Hash>(), jobj)
114+
"account_id" -> return decoder.json.decodeFromJsonElement(serializer<io.github.hosseinkarami_dev.near.rpc.models.GlobalContractIdentifierView.AccountId>(), jobj)
117115
else -> { /* fallthrough to grouped handling */ }
118116
}
119117
// grouped handling by tf content (if any)
120118
val tfLower = tf.lowercase()
121119
var chosenGroupKey: String? = null
122-
if (chosenGroupKey == null && ("CryptoHash".lowercase() == tfLower || tfLower.contains("CryptoHash".lowercase()) || "CryptoHash".lowercase().contains(tfLower))) { chosenGroupKey = "CryptoHash" }
123-
if (chosenGroupKey == null && ("AccountId".lowercase() == tfLower || tfLower.contains("AccountId".lowercase()) || "AccountId".lowercase().contains(tfLower))) { chosenGroupKey = "AccountId" }
120+
if (chosenGroupKey == null && ("hash".lowercase() == tfLower || tfLower.contains("hash".lowercase()) || "hash".lowercase().contains(tfLower))) { chosenGroupKey = "hash" }
121+
if (chosenGroupKey == null && ("account_id".lowercase() == tfLower || tfLower.contains("account_id".lowercase()) || "account_id".lowercase().contains(tfLower))) { chosenGroupKey = "account_id" }
124122
if (chosenGroupKey != null) {
125123
when (chosenGroupKey) {
126-
"CryptoHash" -> {
127-
try { return decoder.json.decodeFromJsonElement(serializer<io.github.hosseinkarami_dev.near.rpc.models.GlobalContractIdentifierView.CryptoHash>(), jobj) } catch (_: Exception) { }
128-
throw SerializationException("Cannot disambiguate variant for base token 'CryptoHash' and tf='\$tf'")
124+
"hash" -> {
125+
try { return decoder.json.decodeFromJsonElement(serializer<io.github.hosseinkarami_dev.near.rpc.models.GlobalContractIdentifierView.Hash>(), jobj) } catch (_: Exception) { }
126+
throw SerializationException("Cannot disambiguate variant for base token 'hash' and tf='\$tf'")
129127
}
130-
"AccountId" -> {
128+
"account_id" -> {
131129
try { return decoder.json.decodeFromJsonElement(serializer<io.github.hosseinkarami_dev.near.rpc.models.GlobalContractIdentifierView.AccountId>(), jobj) } catch (_: Exception) { }
132-
throw SerializationException("Cannot disambiguate variant for base token 'AccountId' and tf='\$tf'")
130+
throw SerializationException("Cannot disambiguate variant for base token 'account_id' and tf='\$tf'")
133131
}
134132
else -> { /* no group matched */ }
135133
}
136134
}
137135
}
138136
// grouped handling by presence of distinguishing keys (no discriminator value available)
139-
// group: CryptoHash
140-
// group: AccountId
137+
// group: hash
138+
// group: account_id
141139

142140
val requiredMatches = mutableListOf<Int>()
143-
if (jobj.containsKey("CryptoHash")) requiredMatches.add(0)
144-
if (jobj.containsKey("AccountId")) requiredMatches.add(1)
141+
if (jobj.containsKey("hash")) requiredMatches.add(0)
142+
if (jobj.containsKey("account_id")) requiredMatches.add(1)
145143
if (requiredMatches.size == 1) {
146144
when (requiredMatches[0]) {
147-
0 -> return decoder.json.decodeFromJsonElement(serializer<io.github.hosseinkarami_dev.near.rpc.models.GlobalContractIdentifierView.CryptoHash>(), jobj)
145+
0 -> return decoder.json.decodeFromJsonElement(serializer<io.github.hosseinkarami_dev.near.rpc.models.GlobalContractIdentifierView.Hash>(), jobj)
148146
1 -> return decoder.json.decodeFromJsonElement(serializer<io.github.hosseinkarami_dev.near.rpc.models.GlobalContractIdentifierView.AccountId>(), jobj)
149147
else -> throw SerializationException("Internal required-match dispatch error")
150148
}
@@ -153,24 +151,24 @@ object GlobalContractIdentifierViewSerializer : KSerializer<GlobalContractIdenti
153151
var bestScore = -1.0
154152
run {
155153
var matchCount = 0
156-
if (jobj["CryptoHash"] != null) matchCount++
154+
if (jobj["hash"] != null) matchCount++
157155
val score = matchCount.toDouble() / 1.toDouble()
158156
if (score > bestScore) { bestScore = score; bestIdx = 0 } else if (score == bestScore) { bestIdx = null }
159157
}
160158
run {
161159
var matchCount = 0
162-
if (jobj["AccountId"] != null) matchCount++
160+
if (jobj["account_id"] != null) matchCount++
163161
val score = matchCount.toDouble() / 1.toDouble()
164162
if (score > bestScore) { bestScore = score; bestIdx = 1 } else if (score == bestScore) { bestIdx = null }
165163
}
166164
if (bestIdx != null && bestScore > 0.0) {
167165
when (bestIdx) {
168-
0 -> return decoder.json.decodeFromJsonElement(serializer<io.github.hosseinkarami_dev.near.rpc.models.GlobalContractIdentifierView.CryptoHash>(), jobj)
166+
0 -> return decoder.json.decodeFromJsonElement(serializer<io.github.hosseinkarami_dev.near.rpc.models.GlobalContractIdentifierView.Hash>(), jobj)
169167
1 -> return decoder.json.decodeFromJsonElement(serializer<io.github.hosseinkarami_dev.near.rpc.models.GlobalContractIdentifierView.AccountId>(), jobj)
170168
else -> throw SerializationException("Internal scoring dispatch error")
171169
}
172170
}
173-
try { return decoder.json.decodeFromJsonElement(serializer<io.github.hosseinkarami_dev.near.rpc.models.GlobalContractIdentifierView.CryptoHash>(), jobj) } catch (_: Exception) { }
171+
try { return decoder.json.decodeFromJsonElement(serializer<io.github.hosseinkarami_dev.near.rpc.models.GlobalContractIdentifierView.Hash>(), jobj) } catch (_: Exception) { }
174172
try { return decoder.json.decodeFromJsonElement(serializer<io.github.hosseinkarami_dev.near.rpc.models.GlobalContractIdentifierView.AccountId>(), jobj) } catch (_: Exception) { }
175173
throw SerializationException("Missing discriminator or recognizable variant in GlobalContractIdentifierView")
176174
}

0 commit comments

Comments
 (0)