diff --git a/Sources/CodableToTypeScript/TypeConverter/TypeMapConverter.swift b/Sources/CodableToTypeScript/TypeConverter/TypeMapConverter.swift index df8b774..5a54a9b 100644 --- a/Sources/CodableToTypeScript/TypeConverter/TypeMapConverter.swift +++ b/Sources/CodableToTypeScript/TypeConverter/TypeMapConverter.swift @@ -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 } } diff --git a/Sources/CodableToTypeScript/Value/TypeMap.swift b/Sources/CodableToTypeScript/Value/TypeMap.swift index 5096cc1..d85b82c 100644 --- a/Sources/CodableToTypeScript/Value/TypeMap.swift +++ b/Sources/CodableToTypeScript/Value/TypeMap.swift @@ -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? } @@ -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( diff --git a/Tests/CodableToTypeScriptTests/Generate/GenerateCustomTypeMapTests.swift b/Tests/CodableToTypeScriptTests/Generate/GenerateCustomTypeMapTests.swift index 8495270..5e4d2cd 100644 --- a/Tests/CodableToTypeScriptTests/Generate/GenerateCustomTypeMapTests.swift +++ b/Tests/CodableToTypeScriptTests/Generate/GenerateCustomTypeMapTests.swift @@ -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: """ @@ -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: """ @@ -45,7 +48,7 @@ export type S = { }; """, """ export type S_JSON = { - a: Date_JSON; + a: string; }; """, """ export function S_decode(json: S_JSON): S { @@ -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: """ @@ -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); }) }; @@ -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: """ @@ -118,7 +127,7 @@ export type S = { }; """, """ export type S_JSON = { - a: Date_JSON; + a: string; }; """, """ export function S_encode(entity: S): S_JSON { @@ -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( @@ -151,6 +158,8 @@ struct S { """, typeMap: typeMap, expecteds: [""" +import { Date_decode, Date_encode } +""", """ export type S = { a: Date; }; @@ -170,8 +179,6 @@ export function S_encode(entity: S): S_JSON { a: Date_encode(entity.a) }; } -""", """ -import { Date_decode, Date_encode } """ ] ) @@ -179,8 +186,14 @@ 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: """ @@ -200,7 +213,7 @@ export type S = { """, """ export type S_JSON = { a: Vector2_JSON; - b: Vector2_JSON; + b: Vector2_JSON; c: Vector2_JSON>[]; }; """, """ @@ -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 } @@ -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 { @@ -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 { @@ -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 { diff --git a/Tests/CodableToTypeScriptTests/Generate/GenerateEncodeTests.swift b/Tests/CodableToTypeScriptTests/Generate/GenerateEncodeTests.swift index 2e42c56..642fc0e 100644 --- a/Tests/CodableToTypeScriptTests/Generate/GenerateEncodeTests.swift +++ b/Tests/CodableToTypeScriptTests/Generate/GenerateEncodeTests.swift @@ -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 } @@ -32,7 +35,7 @@ export type E_JSON = { a: {}; } | { b: { - _0: Date_JSON; + _0: string; }; }; """, """ @@ -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) diff --git a/Tests/CodableToTypeScriptTests/Generate/GenerateExampleTests.swift b/Tests/CodableToTypeScriptTests/Generate/GenerateExampleTests.swift index fa12588..6f2f96c 100644 --- a/Tests/CodableToTypeScriptTests/Generate/GenerateExampleTests.swift +++ b/Tests/CodableToTypeScriptTests/Generate/GenerateExampleTests.swift @@ -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 diff --git a/Tests/CodableToTypeScriptTests/Generate/GenerateImportTests.swift b/Tests/CodableToTypeScriptTests/Generate/GenerateImportTests.swift index 3cd328c..f4cf634 100644 --- a/Tests/CodableToTypeScriptTests/Generate/GenerateImportTests.swift +++ b/Tests/CodableToTypeScriptTests/Generate/GenerateImportTests.swift @@ -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( diff --git a/Tests/CodableToTypeScriptTests/Generate/GenerateNestedTests.swift b/Tests/CodableToTypeScriptTests/Generate/GenerateNestedTests.swift index ccd4e04..fbac893 100644 --- a/Tests/CodableToTypeScriptTests/Generate/GenerateNestedTests.swift +++ b/Tests/CodableToTypeScriptTests/Generate/GenerateNestedTests.swift @@ -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 diff --git a/Tests/CodableToTypeScriptTests/Generate/GenerateRawRepresentableTests.swift b/Tests/CodableToTypeScriptTests/Generate/GenerateRawRepresentableTests.swift index f8bf472..e41dda6 100644 --- a/Tests/CodableToTypeScriptTests/Generate/GenerateRawRepresentableTests.swift +++ b/Tests/CodableToTypeScriptTests/Generate/GenerateRawRepresentableTests.swift @@ -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 } @@ -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; @@ -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 {