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
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@ public struct TypeMapConverter: TypeConverter {
public func name(for target: GenerationTarget) throws -> String {
switch target {
case .entity:
return entry.name
return entry.entityType
case .json:
if let jsonName = entry.jsonType {
return jsonName
}
return try `default`.name(for: .json)
return entry.jsonType
}
}

Expand Down
45 changes: 27 additions & 18 deletions Sources/CodableToTypeScript/Value/TypeMap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,29 @@ import SwiftTypeReader

public struct TypeMap {
public struct Entry {
public init(
name: String,
jsonType: String? = nil,
decode: String? = nil,
encode: String? = nil
) {
self.name = name
self.jsonType = jsonType
self.decode = decode
self.encode = encode
public static func identity(name: String) -> Entry {
return Entry(
entityType: name,
jsonType: name
)
}

public var name: String
public var jsonType: String?
public static func coding(
entityType: String,
jsonType: String,
decode: String?,
encode: String?
) -> Entry {
return Entry(
entityType: entityType,
jsonType: jsonType,
decode: decode,
encode: encode
)
}

public var entityType: String
public var jsonType: String
public var decode: String?
public var encode: String?
}
Expand All @@ -27,12 +36,12 @@ public struct TypeMap {
)

public static let defaultTable: [String: Entry] = [
"Void": Entry(name: "void"),
"Bool": Entry(name: "boolean"),
"Int": Entry(name: "number"),
"Float": Entry(name: "number"),
"Double": Entry(name: "number"),
"String": Entry(name: "string")
"Void": .identity(name: "void"),
"Bool": .identity(name: "boolean"),
"Int": .identity(name: "number"),
"Float": .identity(name: "number"),
"Double": .identity(name: "number"),
"String": .identity(name: "string")
]

public init(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import TypeScriptAST
final class GenerateCustomTypeMapTests: GenerateTestCaseBase {
func testCustomName() throws {
var typeMap = TypeMap.default
typeMap.table["URL"] = .init(name: "string")
typeMap.table["URL"] = .identity(name: "string")

try assertGenerate(
source: """
Expand All @@ -30,7 +30,10 @@ export type S = {

func testCustomDecodeSimple() throws {
var typeMap = TypeMap.default
typeMap.table["Date"] = .init(name: "Date", decode: "Date_decode")
typeMap.table["Date"] = .coding(
entityType: "Date", jsonType: "string",
decode: "Date_decode", encode: nil
)

try assertGenerate(
source: """
Expand All @@ -45,7 +48,7 @@ export type S = {
};
""", """
export type S_JSON = {
a: Date_JSON;
a: string;
};
""", """
export function S_decode(json: S_JSON): S {
Expand All @@ -63,7 +66,10 @@ export function S_encode

func testCustomDecodeComplex() throws {
var typeMap = TypeMap.default
typeMap.table["Date"] = .init(name: "Date", decode: "Date_decode")
typeMap.table["Date"] = .coding(
entityType: "Date", jsonType: "string",
decode: "Date_decode", encode: nil
)

try assertGenerate(
source: """
Expand All @@ -82,16 +88,16 @@ export type S = {
};
""", """
export type S_JSON = {
a: Date_JSON;
b: Date_JSON[];
c: Date_JSON[][];
a: string;
b: string[];
c: string[][];
};
""", """
export function S_decode(json: S_JSON): S {
return {
a: Date_decode(json.a),
b: Array_decode(json.b, Date_decode),
c: Array_decode(json.c, (json: Date_JSON[]): Date[] => {
c: Array_decode(json.c, (json: string[]): Date[] => {
return Array_decode(json, Date_decode);
})
};
Expand All @@ -103,7 +109,10 @@ export function S_decode(json: S_JSON): S {

func testCustomEncode() throws {
var typeMap = TypeMap.default
typeMap.table["Date"] = .init(name: "Date", encode: "Date_encode")
typeMap.table["Date"] = .coding(
entityType: "Date", jsonType: "string",
decode: nil, encode: "Date_encode"
)

try assertGenerate(
source: """
Expand All @@ -118,7 +127,7 @@ export type S = {
};
""", """
export type S_JSON = {
a: Date_JSON;
a: string;
};
""", """
export function S_encode(entity: S): S_JSON {
Expand All @@ -136,11 +145,9 @@ export function S_decode

func testCustomCoding() throws {
var typeMap = TypeMap.default
typeMap.table["Date"] = .init(
name: "Date",
jsonType: "string",
decode: "Date_decode",
encode: "Date_encode"
typeMap.table["Date"] = .coding(
entityType: "Date", jsonType: "string",
decode: "Date_decode", encode: "Date_encode"
)

try assertGenerate(
Expand All @@ -151,6 +158,8 @@ struct S {
""",
typeMap: typeMap,
expecteds: ["""
import { Date_decode, Date_encode }
""", """
export type S = {
a: Date;
};
Expand All @@ -170,17 +179,21 @@ export function S_encode(entity: S): S_JSON {
a: Date_encode(entity.a)
};
}
""", """
import { Date_decode, Date_encode }
"""
]
)
}

func testCustomGenericDecode() throws {
var typeMap = TypeMap.default
typeMap.table["Date"] = .init(name: "Date", decode: "Date_decode")
typeMap.table["Vector2"] = .init(name: "Vector2", decode: "Vector2_decode")
typeMap.table["Date"] = .coding(
entityType: "Date", jsonType: "string",
decode: "Date_decode", encode: "Date_encode"
)
typeMap.table["Vector2"] = .coding(
entityType: "Vector2", jsonType: "Vector2_JSON",
decode: "Vector2_decode", encode: "Vector2_encode"
)

try assertGenerate(
source: """
Expand All @@ -200,7 +213,7 @@ export type S = {
""", """
export type S_JSON = {
a: Vector2_JSON<number>;
b: Vector2_JSON<Date_JSON>;
b: Vector2_JSON<string>;
c: Vector2_JSON<Vector2_JSON<number>>[];
};
""", """
Expand All @@ -227,7 +240,7 @@ export function S_decode(json: S_JSON): S {
guard let repr = repr.asIdent,
let element = repr.elements.last else { return nil }
if element.name.hasSuffix("ID") {
return .init(name: "string")
return .identity(name: "string")
}
return nil
}
Expand All @@ -254,7 +267,7 @@ export type S = {

func testMapUserType() throws {
var typeMap = TypeMap()
typeMap.table["S"] = .init(name: "V")
typeMap.table["S"] = .identity(name: "V")
try assertGenerate(
source: """
struct S {
Expand All @@ -271,7 +284,10 @@ export type S

func testMapUserTypeCodec() throws {
var typeMap = TypeMap()
typeMap.table["S"] = .init(name: "V", decode: "V_decode", encode: "V_encode")
typeMap.table["S"] = .coding(
entityType: "V", jsonType: "V_JSON",
decode: "V_decode", encode: "V_encode"
)
try assertGenerate(
source: """
struct S {
Expand All @@ -288,7 +304,7 @@ export type S

func testMapNestedUserType() throws {
var typeMap = TypeMap()
typeMap.table["K"] = .init(name: "V")
typeMap.table["K"] = .identity(name: "V")
try assertGenerate(
source: """
struct S {
Expand Down
17 changes: 10 additions & 7 deletions Tests/CodableToTypeScriptTests/Generate/GenerateEncodeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import CodableToTypeScript
final class GenerateEncodeTests: GenerateTestCaseBase {
func dateTypeMap() -> TypeMap {
var typeMap = TypeMap()
typeMap.table["Date"] = .init(name: "Date", decode: "Date_decode", encode: "Date_encode")
typeMap.table["Date"] = .coding(
entityType: "Date", jsonType: "string",
decode: "Date_decode", encode: "Date_encode"
)
return typeMap
}

Expand Down Expand Up @@ -32,7 +35,7 @@ export type E_JSON = {
a: {};
} | {
b: {
_0: Date_JSON;
_0: string;
};
};
""", """
Expand Down Expand Up @@ -82,17 +85,17 @@ export type S = {
};
""", """
export type S_JSON = {
a: Date_JSON;
b?: Date_JSON;
c?: Date_JSON | null;
d: Date_JSON[];
a: string;
b?: string;
c?: string | null;
d: string[];
};
""", """
export function S_encode(entity: S): S_JSON {
return {
a: Date_encode(entity.a),
b: OptionalField_encode(entity.b, Date_encode),
c: OptionalField_encode(entity.c, (entity: Date | null): Date_JSON | null => {
c: OptionalField_encode(entity.c, (entity: Date | null): string | null => {
return Optional_encode(entity, Date_encode);
}),
d: Array_encode(entity.d, Date_encode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ struct S {
if let ident = repr.asIdent,
ident.elements.last?.name == "D"
{
return TypeMap.Entry(name: "string")
return .identity(name: "string")
}

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ final class GenerateImportTests: GenerateTestCaseBase {
var typeMap = TypeMap.default

typeMap.table.merge([
"A": .init(name: "A"),
"B": .init(name: "B"),
"C": .init(name: "C"),
"Y": .init(name: "Y")
"A": .identity(name: "A"),
"B": .identity(name: "B"),
"C": .identity(name: "C"),
"Y": .identity(name: "Y")
], uniquingKeysWith: { $1 })

try assertGenerate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ final class GenerateNestedTests: GenerateTestCaseBase {
if let ident = repr.asIdent,
ident.elements.last?.name == "ID"
{
return .init(name: "string")
return .identity(name: "string")
}

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import CodableToTypeScript
final class GenerateRawRepresentableTests: GenerateTestCaseBase {
func dateTypeMap() -> TypeMap {
var typeMap = TypeMap()
typeMap.table["Date"] = .init(name: "Date", decode: "Date_decode", encode: "Date_encode")
typeMap.table["Date"] = .coding(
entityType: "Date", jsonType: "string",
decode: "Date_decode", encode: "Date_encode"
)
return typeMap
}

Expand Down Expand Up @@ -221,7 +224,7 @@ export type S = Date & {
S: never;
};
""", """
export type S_JSON = Date_JSON;
export type S_JSON = string;
""", """
export function S_decode(json: S_JSON): S {
return Date_decode(json) as S;
Expand Down Expand Up @@ -352,7 +355,7 @@ export type User = {
""", """
export type User_JSON = {
id: User_ID_JSON;
date: Date_JSON;
date: string;
};
""", """
export function User_decode(json: User_JSON): User {
Expand Down