Skip to content

Commit

Permalink
fix: Add support for strings that look like numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
miku1958 committed Jan 9, 2024
1 parent 3f602d0 commit 8c2108c
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 31 deletions.
6 changes: 6 additions & 0 deletions demo/basic/generated/kotlin/BridgeTypes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ data class OverriddenFullSize(
@JvmField val numEnum: NumEnum,
@JvmField val defEnum: DefaultEnum,
@JvmField val stringUnion: OverriddenFullSizeMembersStringUnionType,
@JvmField val numberStringUnion: OverriddenFullSizeMembersNumberStringUnionType,
@JvmField val nullableStringUnion: OverriddenFullSizeMembersNullableStringUnionType?,
@JvmField val numUnion1: OverriddenFullSizeMembersNumUnion1Type,
@JvmField val foo: OverriddenFullSizeMembersFooType,
Expand Down Expand Up @@ -83,6 +84,11 @@ enum class OverriddenFullSizeMembersStringUnionType {
@SerializedName("B1") B1
}

enum class OverriddenFullSizeMembersNumberStringUnionType {
@SerializedName("11") _11,
@SerializedName("21") _21
}

enum class OverriddenFullSizeMembersNullableStringUnionType {
@SerializedName("A1") A1,
@SerializedName("B1") B1
Expand Down
9 changes: 8 additions & 1 deletion demo/basic/generated/swift/SharedTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public struct OverriddenFullSize: Codable {
public var numEnum: NumEnum
public var defEnum: DefaultEnum
public var stringUnion: OverriddenFullSizeMembersStringUnionType
public var numberStringUnion: OverriddenFullSizeMembersNumberStringUnionType
public var nullableStringUnion: OverriddenFullSizeMembersNullableStringUnionType?
public var numUnion1: OverriddenFullSizeMembersNumUnion1Type
public var foo: OverriddenFullSizeMembersFooType
Expand All @@ -24,13 +25,14 @@ public struct OverriddenFullSize: Codable {
/// Example documentation for member
private var member: NumEnum = .one

public init(size: Double, count: Int, stringEnum: StringEnum, numEnum: NumEnum, defEnum: DefaultEnum, stringUnion: OverriddenFullSizeMembersStringUnionType, nullableStringUnion: OverriddenFullSizeMembersNullableStringUnionType?, numUnion1: OverriddenFullSizeMembersNumUnion1Type, foo: OverriddenFullSizeMembersFooType, width: Double, height: Double, scale: Double) {
public init(size: Double, count: Int, stringEnum: StringEnum, numEnum: NumEnum, defEnum: DefaultEnum, stringUnion: OverriddenFullSizeMembersStringUnionType, numberStringUnion: OverriddenFullSizeMembersNumberStringUnionType, nullableStringUnion: OverriddenFullSizeMembersNullableStringUnionType?, numUnion1: OverriddenFullSizeMembersNumUnion1Type, foo: OverriddenFullSizeMembersFooType, width: Double, height: Double, scale: Double) {
self.size = size
self.count = count
self.stringEnum = stringEnum
self.numEnum = numEnum
self.defEnum = defEnum
self.stringUnion = stringUnion
self.numberStringUnion = numberStringUnion
self.nullableStringUnion = nullableStringUnion
self.numUnion1 = numUnion1
self.foo = foo
Expand Down Expand Up @@ -61,6 +63,11 @@ public enum OverriddenFullSizeMembersStringUnionType: String, Codable {
case b1 = "B1"
}

public enum OverriddenFullSizeMembersNumberStringUnionType: String, Codable {
case _11 = "11"
case _21 = "21"
}

public enum OverriddenFullSizeMembersNullableStringUnionType: String, Codable {
case a1 = "A1"
case b1 = "B1"
Expand Down
1 change: 1 addition & 0 deletions demo/basic/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ interface FullSize extends BaseSize, CustomSize {
numEnum: NumEnum;
defEnum: DefaultEnum;
stringUnion: 'A1' | 'B1';
numberStringUnion: '11' | '21';
nullableStringUnion: 'A1' | 'B1' | null;
numUnion1: 11 | 21;
foo: { stringField: string } | { numberField: number };
Expand Down
17 changes: 15 additions & 2 deletions src/parser/ValueParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,10 @@ export class ValueParser {

let nullable = false;
let valueType: ValueType | undefined;
const literalValues: UnionLiteralType[] = [];
const literalValues: {
type: BasicTypeValue.string | BasicTypeValue.number;
value: Value;
}[] = [];

node.types.forEach((typeNode) => {
if (isUndefinedOrNull(typeNode)) {
Expand Down Expand Up @@ -303,9 +306,19 @@ export class ValueParser {
'Union type must contain only one supported literal type'
);
}
const members: UnionLiteralType[] = [];
literalValues.forEach((obj) => {
if (typeof obj.value === 'string') {
members.push(obj.value);
}
if (typeof obj.value === 'number') {
members.push(obj.value);
}
});
const unionKind: UnionType = {
kind: ValueTypeKind.unionType,
members: literalValues,
memberType: literalValues[0].type,
members,
};
if (nullable) {
const optionalType: OptionalType = {
Expand Down
6 changes: 2 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,11 @@ export interface PredefinedType extends BaseValueType {
name: string;
}

export interface UnionLiteralType {
type: BasicTypeValue.string | BasicTypeValue.number;
value: Value;
}
export type UnionLiteralType = string | number;

export interface UnionType extends BaseValueType {
kind: ValueTypeKind.unionType;
memberType: BasicTypeValue.string | BasicTypeValue.number;
members: UnionLiteralType[];
}

Expand Down
34 changes: 10 additions & 24 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,36 +38,22 @@ export function uniquePathWithMethodReturnType(ownerName: string, methodName: st
}

export function basicTypeOfUnion(union: UnionType): BasicTypeValue {
return union.members[0].type;
return union.memberType;
}

export function membersOfUnion(union: UnionType): EnumField[] {
const result: EnumField[] = [];
union.members.forEach((value) => {
switch (value.type) {
case BasicTypeValue.string:
if (typeof value.value === 'string') {
const enumField: EnumField = {
key: value.value,
value: value.value,
documentation: '',
};
result.push(enumField);
}
break;
case BasicTypeValue.number:
if (typeof value.value === 'number') {
const enumField: EnumField = {
key: `_${value.value}`,
value: value.value,
documentation: '',
};
result.push(enumField);
}
break;
default:
break;
let key = `${value}`;
if (!Number.isNaN(Number(value))) {
key = `_${key}`;
}
const enumField: EnumField = {
key,
value,
documentation: '',
};
result.push(enumField);
});
return result;
}

0 comments on commit 8c2108c

Please sign in to comment.