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
46 changes: 23 additions & 23 deletions Sources/CodableToTypeScript/Generator/HelperLibraryGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ struct HelperLibraryGenerator {
return TSFunctionDecl(
modifiers: [.export],
name: name(.optionalFieldDecode),
genericParams: [.init("T"), .init("T_JSON")],
genericParams: [.init("T"), .init("T$JSON")],
params: [
.init(name: "json", type: TSUnionType(TSIdentType("T_JSON"), TSIdentType.undefined)),
.init(name: "json", type: TSUnionType(TSIdentType("T$JSON"), TSIdentType.undefined)),
tDecodeParameter()
],
result: TSUnionType(TSIdentType("T"), TSIdentType.undefined),
Expand All @@ -108,12 +108,12 @@ struct HelperLibraryGenerator {
return TSFunctionDecl(
modifiers: [.export],
name: name(.optionalFieldEncode),
genericParams: [.init("T"), .init("T_JSON")],
genericParams: [.init("T"), .init("T$JSON")],
params: [
.init(name: "entity", type: TSUnionType(TSIdentType("T"), TSIdentType.undefined)),
tEncodeParameter()
],
result: TSUnionType(TSIdentType("T_JSON"), TSIdentType.undefined),
result: TSUnionType(TSIdentType("T$JSON"), TSIdentType.undefined),
body: TSBlockStmt([
TSIfStmt(
condition: TSInfixOperatorExpr(
Expand All @@ -130,9 +130,9 @@ struct HelperLibraryGenerator {
return TSFunctionDecl(
modifiers: [.export],
name: name(.optionalDecode),
genericParams: [.init("T"), .init("T_JSON")],
genericParams: [.init("T"), .init("T$JSON")],
params: [
.init(name: "json", type: TSUnionType(TSIdentType("T_JSON"), TSIdentType.null)),
.init(name: "json", type: TSUnionType(TSIdentType("T$JSON"), TSIdentType.null)),
tDecodeParameter()
],
result: TSUnionType(TSIdentType("T"), TSIdentType.null),
Expand All @@ -152,12 +152,12 @@ struct HelperLibraryGenerator {
return TSFunctionDecl(
modifiers: [.export],
name: name(.optionalEncode),
genericParams: [.init("T"), .init("T_JSON")],
genericParams: [.init("T"), .init("T$JSON")],
params: [
.init(name: "entity", type: TSUnionType(TSIdentType("T"), TSIdentType.null)),
tEncodeParameter()
],
result: TSUnionType(TSIdentType("T_JSON"), TSIdentType.null),
result: TSUnionType(TSIdentType("T$JSON"), TSIdentType.null),
body: TSBlockStmt([
TSIfStmt(
condition: TSInfixOperatorExpr(
Expand All @@ -174,9 +174,9 @@ struct HelperLibraryGenerator {
return TSFunctionDecl(
modifiers: [.export],
name: name(.arrayDecode),
genericParams: [.init("T"), .init("T_JSON")],
genericParams: [.init("T"), .init("T$JSON")],
params: [
.init(name: "json", type: TSArrayType(TSIdentType("T_JSON"))),
.init(name: "json", type: TSArrayType(TSIdentType("T$JSON"))),
tDecodeParameter()
],
result: TSArrayType(TSIdentType("T")),
Expand All @@ -197,12 +197,12 @@ struct HelperLibraryGenerator {
return TSFunctionDecl(
modifiers: [.export],
name: name(.arrayEncode),
genericParams: [.init("T"), .init("T_JSON")],
genericParams: [.init("T"), .init("T$JSON")],
params: [
.init(name: "entity", type: TSArrayType(TSIdentType("T"))),
tEncodeParameter()
],
result: TSArrayType(TSIdentType("T_JSON")),
result: TSArrayType(TSIdentType("T$JSON")),
body: TSBlockStmt([
TSReturnStmt(
TSCallExpr(
Expand All @@ -220,9 +220,9 @@ struct HelperLibraryGenerator {
return TSFunctionDecl(
modifiers: [.export],
name: name(.setDecode),
genericParams: [.init("T"), .init("T_JSON")],
genericParams: [.init("T"), .init("T$JSON")],
params: [
.init(name: "json", type: TSArrayType(TSIdentType("T_JSON"))),
.init(name: "json", type: TSArrayType(TSIdentType("T$JSON"))),
tDecodeParameter()
],
result: TSIdentType("Set", genericArgs: [TSIdentType("T")]),
Expand All @@ -246,12 +246,12 @@ struct HelperLibraryGenerator {
return TSFunctionDecl(
modifiers: [.export],
name: name(.setEncode),
genericParams: [.init("T"), .init("T_JSON")],
genericParams: [.init("T"), .init("T$JSON")],
params: [
.init(name: "entity", type: TSIdentType("Set", genericArgs: [TSIdentType("T")])),
tEncodeParameter()
],
result: TSArrayType(TSIdentType("T_JSON")),
result: TSArrayType(TSIdentType("T$JSON")),
body: TSBlockStmt([
TSReturnStmt(
TSCallExpr(
Expand All @@ -272,9 +272,9 @@ struct HelperLibraryGenerator {
return TSFunctionDecl(
modifiers: [.export],
name: name(.dictionaryDecode),
genericParams: [.init("T"), .init("T_JSON")],
genericParams: [.init("T"), .init("T$JSON")],
params: [
.init(name: "json", type: TSObjectType.dictionary(TSIdentType("T_JSON"))),
.init(name: "json", type: TSObjectType.dictionary(TSIdentType("T$JSON"))),
tDecodeParameter()
],
result: TSIdentType.map(TSIdentType.string, TSIdentType("T")),
Expand Down Expand Up @@ -322,15 +322,15 @@ struct HelperLibraryGenerator {
return TSFunctionDecl(
modifiers: [.export],
name: name(.dictionaryEncode),
genericParams: [.init("T"), .init("T_JSON")],
genericParams: [.init("T"), .init("T$JSON")],
params: [
.init(name: "entity", type: TSIdentType.map(TSIdentType.string, TSIdentType("T"))),
tEncodeParameter()
],
result: TSObjectType.dictionary(TSIdentType("T_JSON")),
result: TSObjectType.dictionary(TSIdentType("T$JSON")),
body: TSBlockStmt([
TSVarDecl(
kind: .const, name: "json", type: TSObjectType.dictionary(TSIdentType("T_JSON")),
kind: .const, name: "json", type: TSObjectType.dictionary(TSIdentType("T$JSON")),
initializer: TSObjectExpr([])
),
TSForInStmt(
Expand Down Expand Up @@ -439,7 +439,7 @@ struct HelperLibraryGenerator {
return TSFunctionType.Param(
name: tDecode().name,
type: TSFunctionType(
params: [.init(name: "json", type: TSIdentType("T_JSON"))],
params: [.init(name: "json", type: TSIdentType("T$JSON"))],
result: TSIdentType("T")
)
)
Expand All @@ -463,7 +463,7 @@ struct HelperLibraryGenerator {
name: tEncode().name,
type: TSFunctionType(
params: [.init(name: "entity", type: TSIdentType("T"))],
result: TSIdentType("T_JSON")
result: TSIdentType("T$JSON")
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public struct DefaultTypeConverter {
}

public static func jsonName(entityName: String) -> String {
return "\(entityName)_JSON"
return "\(entityName)$JSON"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

本質的な変更はここだけです

}

public func hasJSONType() throws -> Bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ struct S {
""",
typeConverterProvider: typeConverterProvider,
externalReference: ExternalReference(
symbols: ["Custom", "Custom_JSON", "Custom_decode", "Custom_encode"],
symbols: ["Custom", "Custom$JSON", "Custom_decode", "Custom_encode"],
code: """
export type Custom = {};
export type Custom_JSON = string;
export function Custom_decode(json: Custom_JSON): Custom { throw 0; }
export function Custom_encode(entity: Custom): Custom_JSON { throw 0; }
export type Custom$JSON = string;
export function Custom_decode(json: Custom$JSON): Custom { throw 0; }
export function Custom_encode(entity: Custom): Custom$JSON { throw 0; }
"""
),
expecteds: ["""
Expand All @@ -84,17 +84,17 @@ export type S = {
c: Custom[][];
} & TagRecord<"S">;
""", """
export type S_JSON = {
a: Custom_JSON;
b: Custom_JSON[];
c: Custom_JSON[][];
export type S$JSON = {
a: Custom$JSON;
b: Custom$JSON[];
c: Custom$JSON[][];
};
""", """
export function S_decode(json: S_JSON): S {
export function S_decode(json: S$JSON): S {
const a = Custom_decode(json.a);
const b = Array_decode<Custom, Custom_JSON>(json.b, Custom_decode);
const c = Array_decode<Custom[], Custom_JSON[]>(json.c, (json: Custom_JSON[]): Custom[] => {
return Array_decode<Custom, Custom_JSON>(json, Custom_decode);
const b = Array_decode<Custom, Custom$JSON>(json.b, Custom_decode);
const c = Array_decode<Custom[], Custom$JSON[]>(json.c, (json: Custom$JSON[]): Custom[] => {
return Array_decode<Custom, Custom$JSON>(json, Custom_decode);
});
return {
a: a,
Expand All @@ -103,11 +103,11 @@ export function S_decode(json: S_JSON): S {
};
}
""", """
export function S_encode(entity: S): S_JSON {
export function S_encode(entity: S): S$JSON {
const a = Custom_encode(entity.a);
const b = Array_encode<Custom, Custom_JSON>(entity.b, Custom_encode);
const c = Array_encode<Custom[], Custom_JSON[]>(entity.c, (entity: Custom[]): Custom_JSON[] => {
return Array_encode<Custom, Custom_JSON>(entity, Custom_encode);
const b = Array_encode<Custom, Custom$JSON>(entity.b, Custom_encode);
const c = Array_encode<Custom[], Custom$JSON[]>(entity.c, (entity: Custom[]): Custom$JSON[] => {
return Array_encode<Custom, Custom$JSON>(entity, Custom_encode);
});
return {
a: a,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ export type S = {
a: Date;
} & TagRecord<"S">;
""", """
export type S_JSON = {
export type S$JSON = {
a: string;
};
""", """
export function S_decode(json: S_JSON): S {
export function S_decode(json: S$JSON): S {
const a = Date_decode(json.a);
return {
a: a
Expand Down Expand Up @@ -100,13 +100,13 @@ export type S = {
c: Date[][];
} & TagRecord<"S">;
""", """
export type S_JSON = {
export type S$JSON = {
a: string;
b: string[];
c: string[][];
};
""", """
export function S_decode(json: S_JSON): S {
export function S_decode(json: S$JSON): S {
const a = Date_decode(json.a);
const b = Array_decode<Date, string>(json.b, Date_decode);
const c = Array_decode<Date[], string[]>(json.c, (json: string[]): Date[] => {
Expand Down Expand Up @@ -148,11 +148,11 @@ export type S = {
a: Date;
} & TagRecord<"S">;
""", """
export type S_JSON = {
export type S$JSON = {
a: string;
};
""", """
export function S_encode(entity: S): S_JSON {
export function S_encode(entity: S): S$JSON {
const a = Date_encode(entity.a);
return {
a: a
Expand Down Expand Up @@ -194,18 +194,18 @@ export type S = {
a: Date;
} & TagRecord<"S">;
""", """
export type S_JSON = {
export type S$JSON = {
a: string;
};
""", """
export function S_decode(json: S_JSON): S {
export function S_decode(json: S$JSON): S {
const a = Date_decode(json.a);
return {
a: a
};
}
""", """
export function S_encode(entity: S): S_JSON {
export function S_encode(entity: S): S$JSON {
const a = Date_encode(entity.a);
return {
a: a
Expand All @@ -223,7 +223,7 @@ export function S_encode(entity: S): S_JSON {
decode: "Date_decode", encode: "Date_encode"
)
typeMap.table["Vector2"] = .coding(
entityType: "Vector2", jsonType: "Vector2_JSON",
entityType: "Vector2", jsonType: "Vector2$JSON",
decode: "Vector2_decode", encode: "Vector2_encode"
)

Expand All @@ -239,16 +239,16 @@ struct S {
externalReference: ExternalReference(
symbols: [
"Date_decode", "Date_encode",
"Vector2", "Vector2_JSON",
"Vector2", "Vector2$JSON",
"Vector2_decode", "Vector2_encode"
],
code: """
export function Date_decode(json: string): Date { throw 0; }
export function Date_encode(date: Date): string { throw 0; }
export type Vector2<T> = {};
export type Vector2_JSON<T> = string;
export function Vector2_decode<T, TJ>(json: Vector2_JSON<TJ>, t: (j: TJ) => T): Vector2<T> { throw 0; }
export function Vector2_encode<T, TJ>(date: Vector2<T>, t: (e: T) => TJ): Vector2_JSON<TJ> { throw 0; }
export type Vector2$JSON<T> = string;
export function Vector2_decode<T, TJ>(json: Vector2$JSON<TJ>, t: (j: TJ) => T): Vector2<T> { throw 0; }
export function Vector2_encode<T, TJ>(date: Vector2<T>, t: (e: T) => TJ): Vector2$JSON<TJ> { throw 0; }
"""
),
expecteds: ["""
Expand All @@ -258,17 +258,17 @@ export type S = {
c: Vector2<Vector2<number>>[];
} & TagRecord<"S">;
""", """
export type S_JSON = {
a: Vector2_JSON<number>;
b: Vector2_JSON<string>;
c: Vector2_JSON<Vector2_JSON<number>>[];
export type S$JSON = {
a: Vector2$JSON<number>;
b: Vector2$JSON<string>;
c: Vector2$JSON<Vector2$JSON<number>>[];
};
""", """
export function S_decode(json: S_JSON): S {
export function S_decode(json: S$JSON): S {
const a = Vector2_decode<number, number>(json.a, identity);
const b = Vector2_decode<Date, string>(json.b, Date_decode);
const c = Array_decode<Vector2<Vector2<number>>, Vector2_JSON<Vector2_JSON<number>>>(json.c, (json: Vector2_JSON<Vector2_JSON<number>>): Vector2<Vector2<number>> => {
return Vector2_decode<Vector2<number>, Vector2_JSON<number>>(json, (json: Vector2_JSON<number>): Vector2<number> => {
const c = Array_decode<Vector2<Vector2<number>>, Vector2$JSON<Vector2$JSON<number>>>(json.c, (json: Vector2$JSON<Vector2$JSON<number>>): Vector2<Vector2<number>> => {
return Vector2_decode<Vector2<number>, Vector2$JSON<number>>(json, (json: Vector2$JSON<number>): Vector2<number> => {
return Vector2_decode<number, number>(json, identity);
});
});
Expand Down Expand Up @@ -335,7 +335,7 @@ export type S
func testMapUserTypeCodec() throws {
var typeMap = TypeMap()
typeMap.table["S"] = .coding(
entityType: "V", jsonType: "V_JSON",
entityType: "V", jsonType: "V$JSON",
decode: "V_decode", encode: "V_encode"
)
try assertGenerate(
Expand Down
Loading