Skip to content

Commit

Permalink
Merge pull request #24 from GoNZooo/r.fix-nullable-typescript-fields
Browse files Browse the repository at this point in the history
fix(ts): fix nullable typescript fields
  • Loading branch information
GoNZooo authored Sep 7, 2022
2 parents 69ccfb7 + 06a5cb6 commit 625979a
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 43 deletions.
2 changes: 1 addition & 1 deletion package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: gotyno-hs
version: 1.2.15
version: 1.2.16
synopsis: A type definition compiler supporting multiple output languages.
description: Compiles type definitions into F#, TypeScript and Python, with validators, decoders and encoders.
license: BSD2
Expand Down
5 changes: 4 additions & 1 deletion src/CodeGeneration/TypeScript.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ outputModule module' =
if Text.null importsOutput then "" else importsOutput <> "\n\n",
declarationImportOutput,
if null declarationNames then "" else "\n\n",
definitionOutput
definitionOutput,
"\n"
]

modulePrelude :: Text
Expand Down Expand Up @@ -1088,6 +1089,8 @@ unionEnumConstructorTag unionName constructorName' =
mconcat [unionName, "Tag.", upperCaseFirst constructorName']

outputField :: StructField -> Text
outputField (StructField (FieldName name) (ComplexType (OptionalType fieldType))) =
mconcat [" ", name, "?: ", outputFieldType fieldType, ";\n"]
outputField (StructField (FieldName name) fieldType) =
mconcat [" ", name, ": ", outputFieldType fieldType, ";\n"]

Expand Down
2 changes: 1 addition & 1 deletion test/reference-output/arrayOfArraysOfNullableStrings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ export function isType(value: unknown): value is Type {

export function validateType(value: unknown): svt.ValidationResult<Type> {
return svt.validate<Type>(value, {field: svt.validateArray(svt.validateArray(svt.validateOptional(svt.validateString)))});
}
}
4 changes: 2 additions & 2 deletions test/reference-output/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export type Recruiter = {
type: "Recruiter";
Name: string;
emails: (string | null | undefined)[];
recruiter: Recruiter | null | undefined;
recruiter?: Recruiter;
created: bigint;
};

Expand Down Expand Up @@ -429,4 +429,4 @@ export function validateEmbeddedLogIn(value: unknown): svt.ValidationResult<Embe

export function validateSystemImploded(value: unknown): svt.ValidationResult<SystemImploded> {
return svt.validate<SystemImploded>(value, {type: EmbeddedEventTag.SystemImploded});
}
}
50 changes: 25 additions & 25 deletions test/reference-output/generics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ export function validateUsingOwnGenerics<T>(validateT: svt.Validator<T>): svt.Va

export type KnownForMovie = {
media_type: "movie";
poster_path: string | null | undefined;
poster_path?: string;
id: number;
title: string | null | undefined;
title?: string;
vote_average: number;
release_date: string | null | undefined;
release_date?: string;
overview: string;
};

Expand All @@ -52,12 +52,12 @@ export function validateKnownForMovie(value: unknown): svt.ValidationResult<Know

export type KnownForShow = {
media_type: "tv";
poster_path: string | null | undefined;
poster_path?: string;
id: number;
vote_average: number;
overview: string;
first_air_date: string | null | undefined;
name: string | null | undefined;
first_air_date?: string;
name?: string;
};

export function isKnownForShow(value: unknown): value is KnownForShow {
Expand All @@ -79,11 +79,11 @@ export function validateKnownFor(value: unknown): svt.ValidationResult<KnownFor>
}

export type KnownForMovieWithoutTypeTag = {
poster_path: string | null | undefined;
poster_path?: string;
id: number;
title: string | null | undefined;
title?: string;
vote_average: number;
release_date: string | null | undefined;
release_date?: string;
overview: string;
};

Expand All @@ -96,12 +96,12 @@ export function validateKnownForMovieWithoutTypeTag(value: unknown): svt.Validat
}

export type KnownForShowWithoutTypeTag = {
poster_path: string | null | undefined;
poster_path?: string;
id: number;
vote_average: number;
overview: string;
first_air_date: string | null | undefined;
name: string | null | undefined;
first_air_date?: string;
name?: string;
};

export function isKnownForShowWithoutTypeTag(value: unknown): value is KnownForShowWithoutTypeTag {
Expand All @@ -121,22 +121,22 @@ export enum KnownForEmbeddedTag {

export type MovieStartingWithLowercase = {
media_type: KnownForEmbeddedTag.MovieStartingWithLowercase;
poster_path: string | null | undefined;
poster_path?: string;
id: number;
title: string | null | undefined;
title?: string;
vote_average: number;
release_date: string | null | undefined;
release_date?: string;
overview: string;
};

export type TvStartingWithLowercase = {
media_type: KnownForEmbeddedTag.TvStartingWithLowercase;
poster_path: string | null | undefined;
poster_path?: string;
id: number;
vote_average: number;
overview: string;
first_air_date: string | null | undefined;
name: string | null | undefined;
first_air_date?: string;
name?: string;
};

export function MovieStartingWithLowercase(data: KnownForMovieWithoutTypeTag): MovieStartingWithLowercase {
Expand Down Expand Up @@ -180,22 +180,22 @@ export enum KnownForEmbeddedWithUpperCaseTag {

export type Movie = {
media_type: KnownForEmbeddedWithUpperCaseTag.Movie;
poster_path: string | null | undefined;
poster_path?: string;
id: number;
title: string | null | undefined;
title?: string;
vote_average: number;
release_date: string | null | undefined;
release_date?: string;
overview: string;
};

export type Tv = {
media_type: KnownForEmbeddedWithUpperCaseTag.Tv;
poster_path: string | null | undefined;
poster_path?: string;
id: number;
vote_average: number;
overview: string;
first_air_date: string | null | undefined;
name: string | null | undefined;
first_air_date?: string;
name?: string;
};

export function Movie(data: KnownForMovieWithoutTypeTag): Movie {
Expand Down Expand Up @@ -228,4 +228,4 @@ export function validateMovie(value: unknown): svt.ValidationResult<Movie> {

export function validateTv(value: unknown): svt.ValidationResult<Tv> {
return svt.validate<Tv>(value, {media_type: KnownForEmbeddedWithUpperCaseTag.Tv, poster_path: svt.validateOptional(svt.validateString), id: svt.validateNumber, vote_average: svt.validateNumber, overview: svt.validateString, first_air_date: svt.validateOptional(svt.validateString), name: svt.validateOptional(svt.validateString)});
}
}
22 changes: 11 additions & 11 deletions test/reference-output/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export type UserData = {
following: number;
created_at: string;
updated_at: string;
location: string | null | undefined;
blog: string | null | undefined;
location?: string;
blog?: string;
};

export function isUserData(value: unknown): value is UserData {
Expand Down Expand Up @@ -51,9 +51,9 @@ export type OrganizationData = {
login: string;
id: number;
avatar_url: string;
members_url: string | null | undefined;
members_url?: string;
repos_url: string;
description: string | null | undefined;
description?: string;
};

export function isOrganizationData(value: unknown): value is OrganizationData {
Expand Down Expand Up @@ -88,9 +88,9 @@ export type Organization = {
login: string;
id: number;
avatar_url: string;
members_url: string | null | undefined;
members_url?: string;
repos_url: string;
description: string | null | undefined;
description?: string;
};

export function User(data: OwnerData): User {
Expand Down Expand Up @@ -133,11 +133,11 @@ export type Repository = {
fork: boolean;
created_at: string;
updated_at: string;
description: string | null | undefined;
description?: string;
owner: Owner;
url: string;
html_url: string;
language: string | null | undefined;
language?: string;
};

export function isRepository(value: unknown): value is Repository {
Expand Down Expand Up @@ -203,12 +203,12 @@ export type Issue = {
labels: Label[];
state: string;
locked: boolean;
assignee: UserData | null | undefined;
assignee?: UserData;
assignees: UserData[];
comments: number;
created_at: string;
updated_at: string;
closed_at: string | null | undefined;
closed_at?: string;
author_association: string;
body: string;
};
Expand Down Expand Up @@ -310,4 +310,4 @@ export function isRepositorySearchData(value: unknown): value is RepositorySearc

export function validateRepositorySearchData(value: unknown): svt.ValidationResult<RepositorySearchData> {
return svt.validate<RepositorySearchData>(value, {total_count: svt.validateNumber, incomplete_results: svt.validateBoolean, items: svt.validateArray(validateRepository)});
}
}
2 changes: 1 addition & 1 deletion test/reference-output/hasGeneric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,4 @@ export function validateGenericEvent<T>(validateT: svt.Validator<T>): svt.Valida
return function validateGenericEventT(value: unknown): svt.ValidationResult<GenericEvent<T>> {
return svt.validate<GenericEvent<T>>(value, {type: HasGenericEventTag.GenericEvent, data: external.validateOption(validateT)});
};
}
}
2 changes: 1 addition & 1 deletion test/reference-output/importExample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,4 @@ export function isAllConcrete(value: unknown): value is AllConcrete {

export function validateAllConcrete(value: unknown): svt.ValidationResult<AllConcrete> {
return svt.validate<AllConcrete>(value, {field: validateHoldsSomething(basic.validateEither(basic.validateMaybe(validateStructureUsingImport), validateUnionUsingImport))});
}
}

0 comments on commit 625979a

Please sign in to comment.