diff --git a/Package.resolved b/Package.resolved index 56713c3..d98639b 100644 --- a/Package.resolved +++ b/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "47b101acb5dce72e2df300bb54213e799129143e7ff1f087c6474d20f149c22f", + "originHash" : "49e633b3248491775b3e78a39e2300b207ade2ef8c7fa57307d0e1c93efa8d02", "pins" : [ { "identity" : "async-http-client", @@ -24,8 +24,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/omochi/CodableToTypeScript", "state" : { - "revision" : "8654d6ff35c9823cff38a6f9dd2aa0e6aed2769b", - "version" : "2.11.0" + "revision" : "e5a24cd47970a97700223fbfa6ba5d52113a0273", + "version" : "3.0.1" } }, { @@ -256,10 +256,10 @@ { "identity" : "swifttypereader", "kind" : "remoteSourceControl", - "location" : "https://github.com/omochi/SwiftTypeReader", + "location" : "https://github.com/omochi/SwiftTypeReader.git", "state" : { - "revision" : "12390490318962cad1b82a5d4ee57363ff2dbdcf", - "version" : "2.8.0" + "revision" : "be6697207cd9ed660499ea1a180919be8c857b8e", + "version" : "3.1.0" } }, { @@ -267,8 +267,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/omochi/TypeScriptAST", "state" : { - "revision" : "c64e4a060a3daa9f8c964699ca4d8210235df20b", - "version" : "1.8.7" + "revision" : "4f6420d45d6dabc79b30954ea3aa524b7810a68b", + "version" : "2.0.1" } }, { diff --git a/Package.swift b/Package.swift index eba8dc8..0d95980 100644 --- a/Package.swift +++ b/Package.swift @@ -15,8 +15,8 @@ let package = Package( dependencies: [ .package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.2.2"), .package(url: "https://github.com/swiftlang/swift-syntax.git", from: "600.0.1"), - .package(url: "https://github.com/omochi/CodableToTypeScript.git", from: "2.11.0"), - .package(url: "https://github.com/omochi/SwiftTypeReader.git", from: "2.8.0"), + .package(url: "https://github.com/omochi/CodableToTypeScript.git", from: "3.0.1"), + .package(url: "https://github.com/omochi/SwiftTypeReader.git", from: "3.1.0"), .package(url: "https://github.com/vapor/vapor.git", from: "4.106.7"), .package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.5.0"), ], diff --git a/Sources/CallableKitMacros/CallableMacro.swift b/Sources/CallableKitMacros/CallableMacro.swift index 3d5ff91..cd95446 100644 --- a/Sources/CallableKitMacros/CallableMacro.swift +++ b/Sources/CallableKitMacros/CallableMacro.swift @@ -13,7 +13,7 @@ public struct CallableMacro: PeerMacro { } let protocolName = `protocol`.name.trimmedDescription - let serviceName = protocolName.replacingOccurrences(of: "ServiceProtocol", with: "") + let serviceName = protocolName.trimmingSuffix("Protocol").trimmingSuffix("Service") let functions = `protocol`.memberBlock.members.compactMap { item in return item.decl.as(FunctionDeclSyntax.self) @@ -42,3 +42,14 @@ public struct CallableMacro: PeerMacro { return [DeclSyntax(configureFunc)] } } + +extension String { + fileprivate func trimmingSuffix(_ suffix: String) -> String { + if self.hasSuffix(suffix) { + var copy = self + copy.removeLast(suffix.count) + return copy + } + return self + } +} diff --git a/Sources/CodegenImpl/GenerateTSClient.swift b/Sources/CodegenImpl/GenerateTSClient.swift index d87f928..90b4d85 100644 --- a/Sources/CodegenImpl/GenerateTSClient.swift +++ b/Sources/CodegenImpl/GenerateTSClient.swift @@ -271,8 +271,8 @@ struct FlatRawRepresentableConverter: TypeConverter { } } - func decodePresence() throws -> CodecPresence { - return isTransferringRawValueType ? .identity : .required + func hasDecode() -> Bool { + return !isTransferringRawValueType } func decodeDecl() throws -> TSFunctionDecl? { @@ -288,8 +288,8 @@ struct FlatRawRepresentableConverter: TypeConverter { return decl } - func encodePresence() throws -> CodecPresence { - return isTransferringRawValueType ? .identity : .required + func hasEncode() -> Bool { + return !isTransferringRawValueType } func encodeDecl() throws -> TSFunctionDecl? { diff --git a/Sources/CodegenImpl/ServiceProtocolScanner.swift b/Sources/CodegenImpl/ServiceProtocolScanner.swift index 8959a3d..d905885 100644 --- a/Sources/CodegenImpl/ServiceProtocolScanner.swift +++ b/Sources/CodegenImpl/ServiceProtocolScanner.swift @@ -38,11 +38,11 @@ enum ServiceProtocolScanner { static func scan(_ stype: any SType) -> ServiceProtocolType? { guard let ptype = stype as? ProtocolType, - ptype.name.hasSuffix("ServiceProtocol"), + ptype.decl.attributes.contains(where: { $0.name == "Callable" }), !ptype.decl.functions.isEmpty else { return nil } - let serviceName = ptype.name.replacingOccurrences(of: "ServiceProtocol", with: "") + let serviceName = ptype.name.trimmingSuffix("Protocol").trimmingSuffix("Service") let functions = ptype.decl.functions.compactMap { fdecl -> ServiceProtocolType.Function? in guard fdecl.parameters.count <= 1 else { @@ -77,3 +77,14 @@ enum ServiceProtocolScanner { ) } } + +extension String { + fileprivate func trimmingSuffix(_ suffix: String) -> String { + if self.hasSuffix(suffix) { + var copy = self + copy.removeLast(suffix.count) + return copy + } + return self + } +} diff --git a/example/Package.resolved b/example/Package.resolved index a5c1c5a..7309776 100644 --- a/example/Package.resolved +++ b/example/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "72222c8a5eb70b05fc61636aee7c555f6591c06635ec1a72b200e8f1ece362cb", + "originHash" : "f17cbec788f41a8520f2a0678c0cafe43ec3cc9c912babd1084f8df66e461370", "pins" : [ { "identity" : "async-http-client", @@ -24,8 +24,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/omochi/CodableToTypeScript", "state" : { - "revision" : "8654d6ff35c9823cff38a6f9dd2aa0e6aed2769b", - "version" : "2.11.0" + "revision" : "e5a24cd47970a97700223fbfa6ba5d52113a0273", + "version" : "3.0.1" } }, { @@ -247,10 +247,10 @@ { "identity" : "swifttypereader", "kind" : "remoteSourceControl", - "location" : "https://github.com/omochi/SwiftTypeReader", + "location" : "https://github.com/omochi/SwiftTypeReader.git", "state" : { - "revision" : "12390490318962cad1b82a5d4ee57363ff2dbdcf", - "version" : "2.8.0" + "revision" : "be6697207cd9ed660499ea1a180919be8c857b8e", + "version" : "3.1.0" } }, { @@ -258,8 +258,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/omochi/TypeScriptAST", "state" : { - "revision" : "c64e4a060a3daa9f8c964699ca4d8210235df20b", - "version" : "1.8.7" + "revision" : "4f6420d45d6dabc79b30954ea3aa524b7810a68b", + "version" : "2.0.1" } }, { diff --git a/example/TSClient/src/Gen/APIDefinition/Account.gen.ts b/example/TSClient/src/Gen/APIDefinition/Account.gen.ts index 1a1cc5e..3e4fd88 100644 --- a/example/TSClient/src/Gen/APIDefinition/Account.gen.ts +++ b/example/TSClient/src/Gen/APIDefinition/Account.gen.ts @@ -1,5 +1,5 @@ import { IStubClient } from "../CallableKit.gen.js"; -import { CodableResult, CodableResult_JSON, CodableResult_decode } from "../OtherDependency/CodableResult.gen.js"; +import { CodableResult, CodableResult$JSON, CodableResult_decode } from "../OtherDependency/CodableResult.gen.js"; import { TagRecord, identity } from "../common.gen.js"; import { SubmitError } from "./Entity/SubmitError.gen.js"; @@ -10,7 +10,7 @@ export interface IAccountClient { export const bindAccount = (stub: IStubClient): IAccountClient => { return { async signin(request: AccountSignin_Request): Promise>> { - const json = await stub.send(request, "Account/signin") as CodableResult_JSON>; + const json = await stub.send(request, "Account/signin") as CodableResult$JSON>; return CodableResult_decode< AccountSignin_Response, AccountSignin_Response, diff --git a/example/TSClient/src/Gen/APIDefinition/Echo.gen.ts b/example/TSClient/src/Gen/APIDefinition/Echo.gen.ts index c354b07..f92b5eb 100644 --- a/example/TSClient/src/Gen/APIDefinition/Echo.gen.ts +++ b/example/TSClient/src/Gen/APIDefinition/Echo.gen.ts @@ -9,15 +9,15 @@ import { import { Student, Student2, - Student2_JSON, + Student2$JSON, Student2_decode, Student2_encode, Student3, - Student3_JSON, + Student3$JSON, Student3_decode, Student3_encode, Student4, - Student4_JSON, + Student4$JSON, Student4_decode, Student4_encode, Student5 @@ -50,7 +50,7 @@ export const bindEcho = (stub: IStubClient): IEchoClient => { return await stub.send(request, "Echo/testTypicalEntity") as User; }, async testComplexType(request: TestComplexType_Request): Promise { - const json = await stub.send(request, "Echo/testComplexType") as TestComplexType_Response_JSON; + const json = await stub.send(request, "Echo/testComplexType") as TestComplexType_Response$JSON; return TestComplexType_Response_decode(json); }, async emptyRequestAndResponse(): Promise { @@ -60,15 +60,15 @@ export const bindEcho = (stub: IStubClient): IEchoClient => { return await stub.send(request, "Echo/testTypeAliasToRawRepr") as Student; }, async testRawRepr(request: Student2): Promise { - const json = await stub.send(Student2_encode(request), "Echo/testRawRepr") as Student2_JSON; + const json = await stub.send(Student2_encode(request), "Echo/testRawRepr") as Student2$JSON; return Student2_decode(json); }, async testRawRepr2(request: Student3): Promise { - const json = await stub.send(Student3_encode(request), "Echo/testRawRepr2") as Student3_JSON; + const json = await stub.send(Student3_encode(request), "Echo/testRawRepr2") as Student3$JSON; return Student3_decode(json); }, async testRawRepr3(request: Student4): Promise { - const json = await stub.send(Student4_encode(request), "Echo/testRawRepr3") as Student4_JSON; + const json = await stub.send(Student4_encode(request), "Echo/testRawRepr3") as Student4$JSON; return Student4_decode(json); }, async testRawRepr4(request: Student5): Promise { @@ -91,18 +91,18 @@ export type TestComplexType_K = { x: T; } & TagRecord<"TestComplexType_K", [T]>; -export type TestComplexType_K_JSON = { - x: T_JSON; +export type TestComplexType_K$JSON = { + x: T$JSON; }; -export function TestComplexType_K_decode(json: TestComplexType_K_JSON, T_decode: (json: T_JSON) => T): TestComplexType_K { +export function TestComplexType_K_decode(json: TestComplexType_K$JSON, T_decode: (json: T$JSON) => T): TestComplexType_K { const x = T_decode(json.x); return { x: x }; } -export function TestComplexType_K_encode(entity: TestComplexType_K, T_encode: (entity: T) => T_JSON): TestComplexType_K_JSON { +export function TestComplexType_K_encode(entity: TestComplexType_K, T_encode: (entity: T) => T$JSON): TestComplexType_K$JSON { const x = T_encode(entity.x); return { x: x @@ -124,9 +124,9 @@ export type TestComplexType_E = ({ n: {}; }) & TagRecord<"TestComplexType_E", [T]>; -export type TestComplexType_E_JSON = { +export type TestComplexType_E$JSON = { k: { - _0: TestComplexType_K_JSON; + _0: TestComplexType_K$JSON; }; } | { i: { @@ -136,10 +136,10 @@ export type TestComplexType_E_JSON = { n: {}; }; -export function TestComplexType_E_decode(json: TestComplexType_E_JSON, T_decode: (json: T_JSON) => T): TestComplexType_E { +export function TestComplexType_E_decode(json: TestComplexType_E$JSON, T_decode: (json: T$JSON) => T): TestComplexType_E { if ("k" in json) { const j = json.k; - const _0 = TestComplexType_K_decode(j._0, T_decode); + const _0 = TestComplexType_K_decode(j._0, T_decode); return { kind: "k", k: { @@ -165,12 +165,12 @@ export function TestComplexType_E_decode(json: TestComplexType_E_JSON } } -export function TestComplexType_E_encode(entity: TestComplexType_E, T_encode: (entity: T) => T_JSON): TestComplexType_E_JSON { +export function TestComplexType_E_encode(entity: TestComplexType_E, T_encode: (entity: T) => T$JSON): TestComplexType_E$JSON { switch (entity.kind) { case "k": { const e = entity.k; - const _0 = TestComplexType_K_encode(e._0, T_encode); + const _0 = TestComplexType_K_encode(e._0, T_encode); return { k: { _0: _0 @@ -207,15 +207,15 @@ export type TestComplexType_Request = { a?: TestComplexType_K<(TestComplexType_E | null)[]>; } & TagRecord<"TestComplexType_Request">; -export type TestComplexType_Request_JSON = { - a?: TestComplexType_K_JSON<(TestComplexType_E_JSON | null)[]>; +export type TestComplexType_Request$JSON = { + a?: TestComplexType_K$JSON<(TestComplexType_E$JSON | null)[]>; }; -export function TestComplexType_Request_decode(json: TestComplexType_Request_JSON): TestComplexType_Request { - const a = OptionalField_decode | null)[]>, TestComplexType_K_JSON<(TestComplexType_E_JSON | null)[]>>(json.a, (json: TestComplexType_K_JSON<(TestComplexType_E_JSON | null)[]>): TestComplexType_K<(TestComplexType_E | null)[]> => { - return TestComplexType_K_decode<(TestComplexType_E | null)[], (TestComplexType_E_JSON | null)[]>(json, (json: (TestComplexType_E_JSON | null)[]): (TestComplexType_E | null)[] => { - return Array_decode | null, TestComplexType_E_JSON | null>(json, (json: TestComplexType_E_JSON | null): TestComplexType_E | null => { - return Optional_decode, TestComplexType_E_JSON>(json, (json: TestComplexType_E_JSON): TestComplexType_E => { +export function TestComplexType_Request_decode(json: TestComplexType_Request$JSON): TestComplexType_Request { + const a = OptionalField_decode | null)[]>, TestComplexType_K$JSON<(TestComplexType_E$JSON | null)[]>>(json.a, (json: TestComplexType_K$JSON<(TestComplexType_E$JSON | null)[]>): TestComplexType_K<(TestComplexType_E | null)[]> => { + return TestComplexType_K_decode<(TestComplexType_E | null)[], (TestComplexType_E$JSON | null)[]>(json, (json: (TestComplexType_E$JSON | null)[]): (TestComplexType_E | null)[] => { + return Array_decode | null, TestComplexType_E$JSON | null>(json, (json: TestComplexType_E$JSON | null): TestComplexType_E | null => { + return Optional_decode, TestComplexType_E$JSON>(json, (json: TestComplexType_E$JSON): TestComplexType_E => { return TestComplexType_E_decode(json, identity); }); }); @@ -230,15 +230,15 @@ export type TestComplexType_Response = { a?: TestComplexType_K<(TestComplexType_E | null)[]>; } & TagRecord<"TestComplexType_Response">; -export type TestComplexType_Response_JSON = { - a?: TestComplexType_K_JSON<(TestComplexType_E_JSON | null)[]>; +export type TestComplexType_Response$JSON = { + a?: TestComplexType_K$JSON<(TestComplexType_E$JSON | null)[]>; }; -export function TestComplexType_Response_decode(json: TestComplexType_Response_JSON): TestComplexType_Response { - const a = OptionalField_decode | null)[]>, TestComplexType_K_JSON<(TestComplexType_E_JSON | null)[]>>(json.a, (json: TestComplexType_K_JSON<(TestComplexType_E_JSON | null)[]>): TestComplexType_K<(TestComplexType_E | null)[]> => { - return TestComplexType_K_decode<(TestComplexType_E | null)[], (TestComplexType_E_JSON | null)[]>(json, (json: (TestComplexType_E_JSON | null)[]): (TestComplexType_E | null)[] => { - return Array_decode | null, TestComplexType_E_JSON | null>(json, (json: TestComplexType_E_JSON | null): TestComplexType_E | null => { - return Optional_decode, TestComplexType_E_JSON>(json, (json: TestComplexType_E_JSON): TestComplexType_E => { +export function TestComplexType_Response_decode(json: TestComplexType_Response$JSON): TestComplexType_Response { + const a = OptionalField_decode | null)[]>, TestComplexType_K$JSON<(TestComplexType_E$JSON | null)[]>>(json.a, (json: TestComplexType_K$JSON<(TestComplexType_E$JSON | null)[]>): TestComplexType_K<(TestComplexType_E | null)[]> => { + return TestComplexType_K_decode<(TestComplexType_E | null)[], (TestComplexType_E$JSON | null)[]>(json, (json: (TestComplexType_E$JSON | null)[]): (TestComplexType_E | null)[] => { + return Array_decode | null, TestComplexType_E$JSON | null>(json, (json: TestComplexType_E$JSON | null): TestComplexType_E | null => { + return Optional_decode, TestComplexType_E$JSON>(json, (json: TestComplexType_E$JSON): TestComplexType_E => { return TestComplexType_E_decode(json, identity); }); }); diff --git a/example/TSClient/src/Gen/APIDefinition/Entity/GenericID.gen.ts b/example/TSClient/src/Gen/APIDefinition/Entity/GenericID.gen.ts index 10ccdcf..79a3715 100644 --- a/example/TSClient/src/Gen/APIDefinition/Entity/GenericID.gen.ts +++ b/example/TSClient/src/Gen/APIDefinition/Entity/GenericID.gen.ts @@ -4,25 +4,25 @@ export type GenericID = string & TagRecord<"GenericID", [T]>; export type GenericID2<_IDSpecifier, RawValue> = RawValue & TagRecord<"GenericID2", [_IDSpecifier, RawValue]>; -export type GenericID2_JSON<_IDSpecifier_JSON, RawValue_JSON> = { - rawValue: RawValue_JSON; +export type GenericID2$JSON<_IDSpecifier$JSON, RawValue$JSON> = { + rawValue: RawValue$JSON; }; export function GenericID2_decode< _IDSpecifier, - _IDSpecifier_JSON, + _IDSpecifier$JSON, RawValue, - RawValue_JSON ->(json: GenericID2_JSON<_IDSpecifier_JSON, RawValue_JSON>, _IDSpecifier_decode: (json: _IDSpecifier_JSON) => _IDSpecifier, RawValue_decode: (json: RawValue_JSON) => RawValue): GenericID2<_IDSpecifier, RawValue> { + RawValue$JSON +>(json: GenericID2$JSON<_IDSpecifier$JSON, RawValue$JSON>, _IDSpecifier_decode: (json: _IDSpecifier$JSON) => _IDSpecifier, RawValue_decode: (json: RawValue$JSON) => RawValue): GenericID2<_IDSpecifier, RawValue> { return RawValue_decode(json.rawValue) as GenericID2<_IDSpecifier, RawValue>; } export function GenericID2_encode< _IDSpecifier, - _IDSpecifier_JSON, + _IDSpecifier$JSON, RawValue, - RawValue_JSON ->(entity: GenericID2<_IDSpecifier, RawValue>, _IDSpecifier_encode: (entity: _IDSpecifier) => _IDSpecifier_JSON, RawValue_encode: (entity: RawValue) => RawValue_JSON): GenericID2_JSON<_IDSpecifier_JSON, RawValue_JSON> { + RawValue$JSON +>(entity: GenericID2<_IDSpecifier, RawValue>, _IDSpecifier_encode: (entity: _IDSpecifier) => _IDSpecifier$JSON, RawValue_encode: (entity: RawValue) => RawValue$JSON): GenericID2$JSON<_IDSpecifier$JSON, RawValue$JSON> { return { rawValue: RawValue_encode(entity) }; @@ -38,7 +38,7 @@ export type MyValue = ({ none: {}; }) & TagRecord<"MyValue">; -export type MyValue_JSON = { +export type MyValue$JSON = { id: { _0: string; }; @@ -46,7 +46,7 @@ export type MyValue_JSON = { none: {}; }; -export function MyValue_decode(json: MyValue_JSON): MyValue { +export function MyValue_decode(json: MyValue$JSON): MyValue { if ("id" in json) { const j = json.id; const _0 = j._0; @@ -68,25 +68,25 @@ export function MyValue_decode(json: MyValue_JSON): MyValue { export type GenericID3 = MyValue & TagRecord<"GenericID3", [T]>; -export type GenericID3_JSON = { - rawValue: MyValue_JSON; +export type GenericID3$JSON = { + rawValue: MyValue$JSON; }; -export function GenericID3_decode(json: GenericID3_JSON, T_decode: (json: T_JSON) => T): GenericID3 { +export function GenericID3_decode(json: GenericID3$JSON, T_decode: (json: T$JSON) => T): GenericID3 { return MyValue_decode(json.rawValue) as GenericID3; } -export function GenericID3_encode(entity: GenericID3, T_encode: (entity: T) => T_JSON): GenericID3_JSON { +export function GenericID3_encode(entity: GenericID3, T_encode: (entity: T) => T$JSON): GenericID3$JSON { return { - rawValue: entity as MyValue_JSON + rawValue: entity as MyValue$JSON }; } -export type GenericID3_RawValue = MyValue; +export type GenericID3_RawValue = MyValue; -export type GenericID3_RawValue_JSON = MyValue_JSON; +export type GenericID3_RawValue$JSON = MyValue$JSON; -export function GenericID3_RawValue_decode(json: GenericID3_RawValue_JSON): GenericID3_RawValue { +export function GenericID3_RawValue_decode(json: GenericID3_RawValue$JSON, T_decode: (json: T$JSON) => T): GenericID3_RawValue { return MyValue_decode(json); } diff --git a/example/TSClient/src/Gen/APIDefinition/Entity/Student.gen.ts b/example/TSClient/src/Gen/APIDefinition/Entity/Student.gen.ts index 5225cd0..4a1f842 100644 --- a/example/TSClient/src/Gen/APIDefinition/Entity/Student.gen.ts +++ b/example/TSClient/src/Gen/APIDefinition/Entity/Student.gen.ts @@ -2,16 +2,16 @@ import { TagRecord, identity } from "../../common.gen.js"; import { GenericID, GenericID2, - GenericID2_JSON, + GenericID2$JSON, GenericID2_decode, GenericID2_encode, GenericID3, - GenericID3_JSON, + GenericID3$JSON, GenericID3_decode, GenericID3_encode, GenericID4, MyValue, - MyValue_JSON, + MyValue$JSON, MyValue_decode } from "./GenericID.gen.js"; @@ -27,12 +27,12 @@ export type Student2 = { name: string; } & TagRecord<"Student2">; -export type Student2_JSON = { - id: Student2_ID_JSON; +export type Student2$JSON = { + id: Student2_ID$JSON; name: string; }; -export function Student2_decode(json: Student2_JSON): Student2 { +export function Student2_decode(json: Student2$JSON): Student2 { const id = Student2_ID_decode(json.id); const name = json.name; return { @@ -41,7 +41,7 @@ export function Student2_decode(json: Student2_JSON): Student2 { }; } -export function Student2_encode(entity: Student2): Student2_JSON { +export function Student2_encode(entity: Student2): Student2$JSON { const id = Student2_ID_encode(entity.id); const name = entity.name; return { @@ -52,21 +52,21 @@ export function Student2_encode(entity: Student2): Student2_JSON { export type Student2_ID = GenericID2; -export type Student2_ID_JSON = GenericID2_JSON; +export type Student2_ID$JSON = GenericID2$JSON; -export function Student2_ID_decode(json: Student2_ID_JSON): Student2_ID { +export function Student2_ID_decode(json: Student2_ID$JSON): Student2_ID { return GenericID2_decode< Student2, - Student2_JSON, + Student2$JSON, string, string >(json, Student2_decode, identity); } -export function Student2_ID_encode(entity: Student2_ID): Student2_ID_JSON { +export function Student2_ID_encode(entity: Student2_ID): Student2_ID$JSON { return GenericID2_encode< Student2, - Student2_JSON, + Student2$JSON, string, string >(entity, Student2_encode, identity); @@ -77,12 +77,12 @@ export type Student3 = { name: string; } & TagRecord<"Student3">; -export type Student3_JSON = { - id: Student3_ID_JSON; +export type Student3$JSON = { + id: Student3_ID$JSON; name: string; }; -export function Student3_decode(json: Student3_JSON): Student3 { +export function Student3_decode(json: Student3$JSON): Student3 { const id = Student3_ID_decode(json.id); const name = json.name; return { @@ -91,7 +91,7 @@ export function Student3_decode(json: Student3_JSON): Student3 { }; } -export function Student3_encode(entity: Student3): Student3_JSON { +export function Student3_encode(entity: Student3): Student3$JSON { const id = Student3_ID_encode(entity.id); const name = entity.name; return { @@ -102,14 +102,14 @@ export function Student3_encode(entity: Student3): Student3_JSON { export type Student3_ID = GenericID3; -export type Student3_ID_JSON = GenericID3_JSON; +export type Student3_ID$JSON = GenericID3$JSON; -export function Student3_ID_decode(json: Student3_ID_JSON): Student3_ID { - return GenericID3_decode(json, Student3_decode); +export function Student3_ID_decode(json: Student3_ID$JSON): Student3_ID { + return GenericID3_decode(json, Student3_decode); } -export function Student3_ID_encode(entity: Student3_ID): Student3_ID_JSON { - return GenericID3_encode(entity, Student3_encode); +export function Student3_ID_encode(entity: Student3_ID): Student3_ID$JSON { + return GenericID3_encode(entity, Student3_encode); } export type Student4 = { @@ -117,12 +117,12 @@ export type Student4 = { name: string; } & TagRecord<"Student4">; -export type Student4_JSON = { - id: Student4_ID_JSON; +export type Student4$JSON = { + id: Student4_ID$JSON; name: string; }; -export function Student4_decode(json: Student4_JSON): Student4 { +export function Student4_decode(json: Student4$JSON): Student4 { const id = Student4_ID_decode(json.id); const name = json.name; return { @@ -131,7 +131,7 @@ export function Student4_decode(json: Student4_JSON): Student4 { }; } -export function Student4_encode(entity: Student4): Student4_JSON { +export function Student4_encode(entity: Student4): Student4$JSON { const id = Student4_ID_encode(entity.id); const name = entity.name; return { @@ -142,36 +142,36 @@ export function Student4_encode(entity: Student4): Student4_JSON { export type Student4_ID = GenericID2>; -export type Student4_ID_JSON = GenericID2_JSON>; +export type Student4_ID$JSON = GenericID2$JSON>; -export function Student4_ID_decode(json: Student4_ID_JSON): Student4_ID { +export function Student4_ID_decode(json: Student4_ID$JSON): Student4_ID { return GenericID2_decode< Student4, - Student4_JSON, + Student4$JSON, GenericID2, - GenericID2_JSON - >(json, Student4_decode, (json: GenericID2_JSON): GenericID2 => { + GenericID2$JSON + >(json, Student4_decode, (json: GenericID2$JSON): GenericID2 => { return GenericID2_decode< Student4, - Student4_JSON, + Student4$JSON, MyValue, - MyValue_JSON + MyValue$JSON >(json, Student4_decode, MyValue_decode); }); } -export function Student4_ID_encode(entity: Student4_ID): Student4_ID_JSON { +export function Student4_ID_encode(entity: Student4_ID): Student4_ID$JSON { return GenericID2_encode< Student4, - Student4_JSON, + Student4$JSON, GenericID2, - GenericID2_JSON - >(entity, Student4_encode, (entity: GenericID2): GenericID2_JSON => { + GenericID2$JSON + >(entity, Student4_encode, (entity: GenericID2): GenericID2$JSON => { return GenericID2_encode< Student4, - Student4_JSON, + Student4$JSON, MyValue, - MyValue_JSON + MyValue$JSON >(entity, Student4_encode, identity); }); } diff --git a/example/TSClient/src/Gen/APIDefinition/Entity/SubmitError.gen.ts b/example/TSClient/src/Gen/APIDefinition/Entity/SubmitError.gen.ts index 4d68bab..e68afb3 100644 --- a/example/TSClient/src/Gen/APIDefinition/Entity/SubmitError.gen.ts +++ b/example/TSClient/src/Gen/APIDefinition/Entity/SubmitError.gen.ts @@ -5,12 +5,12 @@ export type InputFieldError = { message: string; } & TagRecord<"InputFieldError", [E]>; -export type InputFieldError_JSON = { - name: E_JSON; +export type InputFieldError$JSON = { + name: E$JSON; message: string; }; -export function InputFieldError_decode(json: InputFieldError_JSON, E_decode: (json: E_JSON) => E): InputFieldError { +export function InputFieldError_decode(json: InputFieldError$JSON, E_decode: (json: E$JSON) => E): InputFieldError { const name = E_decode(json.name); const message = json.message; return { @@ -19,7 +19,7 @@ export function InputFieldError_decode(json: InputFieldError_JSON(entity: InputFieldError, E_encode: (entity: E) => E_JSON): InputFieldError_JSON { +export function InputFieldError_encode(entity: InputFieldError, E_encode: (entity: E) => E$JSON): InputFieldError$JSON { const name = E_encode(entity.name); const message = entity.message; return { @@ -32,22 +32,22 @@ export type SubmitError = { errors: InputFieldError[]; } & TagRecord<"SubmitError", [E]>; -export type SubmitError_JSON = { - errors: InputFieldError_JSON[]; +export type SubmitError$JSON = { + errors: InputFieldError$JSON[]; }; -export function SubmitError_decode(json: SubmitError_JSON, E_decode: (json: E_JSON) => E): SubmitError { - const errors = Array_decode, InputFieldError_JSON>(json.errors, (json: InputFieldError_JSON): InputFieldError => { - return InputFieldError_decode(json, E_decode); +export function SubmitError_decode(json: SubmitError$JSON, E_decode: (json: E$JSON) => E): SubmitError { + const errors = Array_decode, InputFieldError$JSON>(json.errors, (json: InputFieldError$JSON): InputFieldError => { + return InputFieldError_decode(json, E_decode); }); return { errors: errors }; } -export function SubmitError_encode(entity: SubmitError, E_encode: (entity: E) => E_JSON): SubmitError_JSON { - const errors = Array_encode, InputFieldError_JSON>(entity.errors, (entity: InputFieldError): InputFieldError_JSON => { - return InputFieldError_encode(entity, E_encode); +export function SubmitError_encode(entity: SubmitError, E_encode: (entity: E) => E$JSON): SubmitError$JSON { + const errors = Array_encode, InputFieldError$JSON>(entity.errors, (entity: InputFieldError): InputFieldError$JSON => { + return InputFieldError_encode(entity, E_encode); }); return { errors: errors diff --git a/example/TSClient/src/Gen/OtherDependency/CodableResult.gen.ts b/example/TSClient/src/Gen/OtherDependency/CodableResult.gen.ts index e08be56..342b669 100644 --- a/example/TSClient/src/Gen/OtherDependency/CodableResult.gen.ts +++ b/example/TSClient/src/Gen/OtherDependency/CodableResult.gen.ts @@ -12,22 +12,22 @@ export type CodableResult = ({ }; }) & TagRecord<"CodableResult", [T, E]>; -export type CodableResult_JSON = { +export type CodableResult$JSON = { success: { - _0: T_JSON; + _0: T$JSON; }; } | { failure: { - _0: E_JSON; + _0: E$JSON; }; }; export function CodableResult_decode< T, - T_JSON, + T$JSON, E, - E_JSON ->(json: CodableResult_JSON, T_decode: (json: T_JSON) => T, E_decode: (json: E_JSON) => E): CodableResult { + E$JSON +>(json: CodableResult$JSON, T_decode: (json: T$JSON) => T, E_decode: (json: E$JSON) => E): CodableResult { if ("success" in json) { const j = json.success; const _0 = T_decode(j._0); @@ -53,10 +53,10 @@ export function CodableResult_decode< export function CodableResult_encode< T, - T_JSON, + T$JSON, E, - E_JSON ->(entity: CodableResult, T_encode: (entity: T) => T_JSON, E_encode: (entity: E) => E_JSON): CodableResult_JSON { + E$JSON +>(entity: CodableResult, T_encode: (entity: T) => T$JSON, E_encode: (entity: E) => E$JSON): CodableResult$JSON { switch (entity.kind) { case "success": { diff --git a/example/TSClient/src/Gen/common.gen.ts b/example/TSClient/src/Gen/common.gen.ts index d07370c..8b7c3a9 100644 --- a/example/TSClient/src/Gen/common.gen.ts +++ b/example/TSClient/src/Gen/common.gen.ts @@ -2,37 +2,45 @@ export function identity(json: T): T { return json; } -export function OptionalField_decode(json: T_JSON | undefined, T_decode: (json: T_JSON) => T): T | undefined { +export function OptionalField_decode(json: T$JSON | undefined, T_decode: (json: T$JSON) => T): T | undefined { if (json === undefined) return undefined; return T_decode(json); } -export function OptionalField_encode(entity: T | undefined, T_encode: (entity: T) => T_JSON): T_JSON | undefined { +export function OptionalField_encode(entity: T | undefined, T_encode: (entity: T) => T$JSON): T$JSON | undefined { if (entity === undefined) return undefined; return T_encode(entity); } -export function Optional_decode(json: T_JSON | null, T_decode: (json: T_JSON) => T): T | null { +export function Optional_decode(json: T$JSON | null, T_decode: (json: T$JSON) => T): T | null { if (json === null) return null; return T_decode(json); } -export function Optional_encode(entity: T | null, T_encode: (entity: T) => T_JSON): T_JSON | null { +export function Optional_encode(entity: T | null, T_encode: (entity: T) => T$JSON): T$JSON | null { if (entity === null) return null; return T_encode(entity); } -export function Array_decode(json: T_JSON[], T_decode: (json: T_JSON) => T): T[] { +export function Array_decode(json: T$JSON[], T_decode: (json: T$JSON) => T): T[] { return json.map(T_decode); } -export function Array_encode(entity: T[], T_encode: (entity: T) => T_JSON): T_JSON[] { +export function Array_encode(entity: T[], T_encode: (entity: T) => T$JSON): T$JSON[] { return entity.map(T_encode); } -export function Dictionary_decode(json: { - [key: string]: T_JSON; -}, T_decode: (json: T_JSON) => T): Map { +export function Set_decode(json: T$JSON[], T_decode: (json: T$JSON) => T): Set { + return new Set(json.map(T_decode)); +} + +export function Set_encode(entity: Set, T_encode: (entity: T) => T$JSON): T$JSON[] { + return [... entity].map(T_encode); +} + +export function Dictionary_decode(json: { + [key: string]: T$JSON; +}, T_decode: (json: T$JSON) => T): Map { const entity = new Map(); for (const k in json) { if (json.hasOwnProperty(k)) { @@ -42,11 +50,11 @@ export function Dictionary_decode(json: { return entity; } -export function Dictionary_encode(entity: Map, T_encode: (entity: T) => T_JSON): { - [key: string]: T_JSON; +export function Dictionary_encode(entity: Map, T_encode: (entity: T) => T$JSON): { + [key: string]: T$JSON; } { const json: { - [key: string]: T_JSON; + [key: string]: T$JSON; } = {}; for (const k in entity.keys()) { json[k] = T_encode(entity.get(k) !!);