From 9bbc93e01fcd4a7b3debe50c9971473fc013cc2e Mon Sep 17 00:00:00 2001 From: omochimetaru Date: Tue, 6 Dec 2022 17:22:49 +0900 Subject: [PATCH] fix encode only type bug --- .../TypeConverter/DefaultTypeConverter.swift | 3 + .../Generate/GenerateCustomTypeTests.swift | 68 ++++++++++++++++++- 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/Sources/CodableToTypeScript/TypeConverter/DefaultTypeConverter.swift b/Sources/CodableToTypeScript/TypeConverter/DefaultTypeConverter.swift index 2e6a76b..7c6f00f 100644 --- a/Sources/CodableToTypeScript/TypeConverter/DefaultTypeConverter.swift +++ b/Sources/CodableToTypeScript/TypeConverter/DefaultTypeConverter.swift @@ -43,6 +43,9 @@ public struct DefaultTypeConverter { if try converter.hasDecode() { return true } + if try converter.hasEncode() { + return true + } return false } diff --git a/Tests/CodableToTypeScriptTests/Generate/GenerateCustomTypeTests.swift b/Tests/CodableToTypeScriptTests/Generate/GenerateCustomTypeTests.swift index ae0ca6c..982237d 100644 --- a/Tests/CodableToTypeScriptTests/Generate/GenerateCustomTypeTests.swift +++ b/Tests/CodableToTypeScriptTests/Generate/GenerateCustomTypeTests.swift @@ -28,7 +28,40 @@ export type S = { ) } - func testCustomDecode() throws { + func testCustomDecodeSimple() throws { + var typeMap = TypeMap.default + typeMap.table["Date"] = .init(name: "Date", decode: "Date_decode") + + try assertGenerate( + source: """ +struct S { + var a: Date +} +""", + typeMap: typeMap, + expecteds: [""" +export type S = { + a: Date; +}; +""", """ +export type S_JSON = { + a: Date_JSON; +}; +""", """ +export function S_decode(json: S_JSON): S { + return { + a: Date_decode(json.a) + }; +} +""" + ], + unexpecteds: [""" +export function S_encode +"""] + ) + } + + func testCustomDecodeComplex() throws { var typeMap = TypeMap.default typeMap.table["Date"] = .init(name: "Date", decode: "Date_decode") @@ -68,6 +101,39 @@ export function S_decode(json: S_JSON): S { ) } + func testCustomEncode() throws { + var typeMap = TypeMap.default + typeMap.table["Date"] = .init(name: "Date", encode: "Date_encode") + + try assertGenerate( + source: """ +struct S { + var a: Date +} +""", + typeMap: typeMap, + expecteds: [""" +export type S = { + a: Date; +}; +""", """ +export type S_JSON = { + a: Date_JSON; +}; +""", """ +export function S_encode(entity: S): S_JSON { + return { + a: Date_encode(entity.a) + }; +} +""" + ], + unexpecteds: [""" +export function S_decode +"""] + ) + } + func testCustomCoding() throws { var typeMap = TypeMap.default typeMap.table["Date"] = .init(