Skip to content

Commit

Permalink
Merge pull request #31 from GoNZooo/r.add-bigint-serializer-in-kotlin
Browse files Browse the repository at this point in the history
fix(kt): fix BigInt serialization for structs
  • Loading branch information
GoNZooo authored Oct 27, 2022
2 parents 722f185 + 025604e commit faeb152
Show file tree
Hide file tree
Showing 16 changed files with 38 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 2.2.3

### Fixes

- Fix bug where structures were not serializable in Kotlin if they had `{I,U}64`
fields.

## 2.2.2

### Fixes
Expand Down
2 changes: 1 addition & 1 deletion gotyno-hs.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ cabal-version: 1.12
-- see: https://github.com/sol/hpack

name: gotyno-hs
version: 2.2.2
version: 2.2.3
synopsis: A type definition compiler supporting multiple output languages.
description: Compiles type definitions into F#, TypeScript and Python, with validators, decoders and encoders.
category: Compiler
Expand Down
2 changes: 1 addition & 1 deletion package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: gotyno-hs
version: 2.2.2
version: 2.2.3
synopsis: A type definition compiler supporting multiple output languages.
description: Compiles type definitions into F#, TypeScript and Python, with validators, decoders and encoders.
license: BSD2
Expand Down
18 changes: 16 additions & 2 deletions src/CodeGeneration/Kotlin.hs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ modulePrelude =
"import com.fasterxml.jackson.databind.deser.std.*\n",
"import java.text.ParseException\n",
"import java.math.BigInteger\n",
"import kotlinx.serialization.Serializable"
"import kotlinx.serialization.Serializable\n",
"import org.gotynoDeclarations.BigIntegerSerializer"
]

outputDefinition :: TypeDefinition -> Maybe Text
Expand All @@ -74,7 +75,6 @@ outputDefinition (TypeDefinition name (Struct (GenericStruct typeVariables field
pure $ outputGenericStruct name typeVariables fields
outputDefinition (TypeDefinition name (Union typeTag unionType)) =
pure $ outputUnion name typeTag unionType
-- @TODO: use the type here to set the type of the enumeration
outputDefinition (TypeDefinition name (Enumeration type' enumerationValues)) =
pure $ outputEnumeration type' name enumerationValues
outputDefinition (TypeDefinition name (UntaggedUnion unionCases)) =
Expand Down Expand Up @@ -332,6 +332,9 @@ outputField (StructField fieldName fieldType) =
[ "@get:JsonProperty(\"",
unFieldName fieldName,
"\")\n",
if isBigIntType fieldType
then " @Serializable(with = BigIntegerSerializer::class)\n"
else "",
" val ",
unFieldName fieldName,
": ",
Expand Down Expand Up @@ -483,6 +486,17 @@ fieldTypeName
(DefinitionReferenceType (DeclarationReference _moduleName' (DefinitionName definitionName))) =
definitionName

isBigIntType :: FieldType -> Bool
isBigIntType (ComplexType (SliceType fieldType)) = isBigIntType fieldType
isBigIntType (ComplexType (ArrayType _size fieldType)) = isBigIntType fieldType
isBigIntType (ComplexType (OptionalType fieldType)) = isBigIntType fieldType
isBigIntType (ComplexType (PointerType fieldType)) = isBigIntType fieldType
isBigIntType (BasicType U64) = True
isBigIntType (BasicType U128) = True
isBigIntType (BasicType I64) = True
isBigIntType (BasicType I128) = True
isBigIntType _ = False

joinTypeVariables :: [TypeVariable] -> Text
joinTypeVariables = fmap unTypeVariable >>> Text.intercalate ", "

Expand Down
2 changes: 2 additions & 0 deletions test/reference-output/basic.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.deser.std.*
import java.text.ParseException
import java.math.BigInteger
import kotlinx.serialization.Serializable
import org.gotynoDeclarations.BigIntegerSerializer

class Basic {
@Serializable
Expand All @@ -20,6 +21,7 @@ data class Recruiter(
@get:JsonProperty("recruiter")
val recruiter: Recruiter?,
@get:JsonProperty("created")
@Serializable(with = BigIntegerSerializer::class)
val created: BigInteger,
@get:JsonProperty("type")
val type: String = "Recruiter"
Expand Down
1 change: 1 addition & 0 deletions test/reference-output/basicEnumeration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.deser.std.*
import java.text.ParseException
import java.math.BigInteger
import kotlinx.serialization.Serializable
import org.gotynoDeclarations.BigIntegerSerializer

class BasicEnumeration {
enum class StringValues(val data: String) : java.io.Serializable {
Expand Down
1 change: 1 addition & 0 deletions test/reference-output/basicImport.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.deser.std.*
import java.text.ParseException
import java.math.BigInteger
import kotlinx.serialization.Serializable
import org.gotynoDeclarations.BigIntegerSerializer

import org.gotynoOutput.BasicStruct

Expand Down
1 change: 1 addition & 0 deletions test/reference-output/basicOptional.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.deser.std.*
import java.text.ParseException
import java.math.BigInteger
import kotlinx.serialization.Serializable
import org.gotynoDeclarations.BigIntegerSerializer

class BasicOptional {
@Serializable
Expand Down
1 change: 1 addition & 0 deletions test/reference-output/basicStruct.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.deser.std.*
import java.text.ParseException
import java.math.BigInteger
import kotlinx.serialization.Serializable
import org.gotynoDeclarations.BigIntegerSerializer

class BasicStruct {
@Serializable
Expand Down
1 change: 1 addition & 0 deletions test/reference-output/basicUnion.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.deser.std.*
import java.text.ParseException
import java.math.BigInteger
import kotlinx.serialization.Serializable
import org.gotynoDeclarations.BigIntegerSerializer

class BasicUnion {
@Serializable
Expand Down
1 change: 1 addition & 0 deletions test/reference-output/genericStruct.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.deser.std.*
import java.text.ParseException
import java.math.BigInteger
import kotlinx.serialization.Serializable
import org.gotynoDeclarations.BigIntegerSerializer

class GenericStruct {
@Serializable
Expand Down
1 change: 1 addition & 0 deletions test/reference-output/genericUnion.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.deser.std.*
import java.text.ParseException
import java.math.BigInteger
import kotlinx.serialization.Serializable
import org.gotynoDeclarations.BigIntegerSerializer

class GenericUnion {
@Serializable
Expand Down
1 change: 1 addition & 0 deletions test/reference-output/generics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.deser.std.*
import java.text.ParseException
import java.math.BigInteger
import kotlinx.serialization.Serializable
import org.gotynoDeclarations.BigIntegerSerializer

import org.gotynoOutput.Basic
import org.gotynoOutput.HasGeneric
Expand Down
1 change: 1 addition & 0 deletions test/reference-output/github.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.deser.std.*
import java.text.ParseException
import java.math.BigInteger
import kotlinx.serialization.Serializable
import org.gotynoDeclarations.BigIntegerSerializer

class Github {
@Serializable
Expand Down
1 change: 1 addition & 0 deletions test/reference-output/hasGeneric.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.deser.std.*
import java.text.ParseException
import java.math.BigInteger
import kotlinx.serialization.Serializable
import org.gotynoDeclarations.BigIntegerSerializer

import org.gotynoDeclarations.*

Expand Down
1 change: 1 addition & 0 deletions test/reference-output/importExample.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.deser.std.*
import java.text.ParseException
import java.math.BigInteger
import kotlinx.serialization.Serializable
import org.gotynoDeclarations.BigIntegerSerializer

import org.gotynoOutput.Basic

Expand Down

0 comments on commit faeb152

Please sign in to comment.