Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: format code with Prettier #2442

Closed
wants to merge 1 commit into from
Closed
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
23 changes: 12 additions & 11 deletions FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
- [I'd like to customize the output for my particular application.](#id-like-to-customize-the-output-for-my-particular-application)
- [How can I control the property order in JSON Schema?](#how-can-i-control-the-property-order-in-json-schema)
- [quicktype is awesome, I'd like to support it!](#quicktype-is-awesome-id-like-to-support-it)
- [How is this different from other JSON converters?](#how-is-quicktype-different-from-other-json-converters)
- [How is this different from other JSON converters?](#how-is-quicktype-different-from-other-json-converters)

## What is this?

Expand All @@ -29,16 +29,16 @@ You paste JSON on the left, and code appears on the right. [This video](https://

`quicktype` is superior to other JSON converters in many important ways:

- **Type inference**: quicktype infers optionals, dates, UUIDs, enums, integers, and unions. It also infers maps (versus objects) using a Markov chain.
- **Careful naming**: quicktype’s naming system creates nice, legal, unique names for types and properties, handling reserved words and tricky corner cases (e.g. `{ “”: “this is legal”, “null”: “so is this”, "1;DROP TABLE users”: “and this" }`).
- **Heterogeneous data**: JSON data is often heterogenous. quicktype infers this, and creates union types in languages that support them, or synthetic union types in languages that don’t (e.g. try quicktyping `[0, “zero”]` as Swift and Go).
- **Type unification**. This works across multiple samples, so you can quicktype a directory of API data, for example, and unify types across all responses (e.g. you’ll get just one `Customer` type, even if customer data occurs in many samples). You can also provide multiple samples for the same type for better coverage.
- **Marshalling code**: In addition to types, quicktype generates functions for marshalling your types to and from JSON.
- Supports dynamic languages: quicktype can add dynamic typechecks for JavaScript, TypeScript, Flow, Python, and Ruby.
- **Convenient CLI**: Run `quicktype https://blockchain.info/latestblock -o LatestBlock.ts` to quicktype a Bitcoin API in TypeScript.
- **Client-side**: [The web version of quicktype](https://app.quicktype.io/) runs on the client, so servers never see your data (most JSON converters send your JSON to their server)
- **Typed input**: Feed quicktype TypeScript or JSON Schema instead of JSON for better control over generated types.
- **Code quality**: quicktype emits clean code
- **Type inference**: quicktype infers optionals, dates, UUIDs, enums, integers, and unions. It also infers maps (versus objects) using a Markov chain.
- **Careful naming**: quicktype’s naming system creates nice, legal, unique names for types and properties, handling reserved words and tricky corner cases (e.g. `{ “”: “this is legal”, “null”: “so is this”, "1;DROP TABLE users”: “and this" }`).
- **Heterogeneous data**: JSON data is often heterogenous. quicktype infers this, and creates union types in languages that support them, or synthetic union types in languages that don’t (e.g. try quicktyping `[0, “zero”]` as Swift and Go).
- **Type unification**. This works across multiple samples, so you can quicktype a directory of API data, for example, and unify types across all responses (e.g. you’ll get just one `Customer` type, even if customer data occurs in many samples). You can also provide multiple samples for the same type for better coverage.
- **Marshalling code**: In addition to types, quicktype generates functions for marshalling your types to and from JSON.
- Supports dynamic languages: quicktype can add dynamic typechecks for JavaScript, TypeScript, Flow, Python, and Ruby.
- **Convenient CLI**: Run `quicktype https://blockchain.info/latestblock -o LatestBlock.ts` to quicktype a Bitcoin API in TypeScript.
- **Client-side**: [The web version of quicktype](https://app.quicktype.io/) runs on the client, so servers never see your data (most JSON converters send your JSON to their server)
- **Typed input**: Feed quicktype TypeScript or JSON Schema instead of JSON for better control over generated types.
- **Code quality**: quicktype emits clean code

## How do I use this with my code?

Expand Down Expand Up @@ -115,6 +115,7 @@ We have [a blog post](https://blog.quicktype.io/customizing-quicktype/) on that
There is a custom schema field `quicktypePropertyOrder` which can be used to specify the order of properties for quicktype.

For example:

```json
...
"Location": {
Expand Down
12 changes: 6 additions & 6 deletions PACKAGES.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
# The packages

* `quicktype-core`: This is the engine of quicktype. It takes JSON, JSON Schema, and GraphQL input and produces output in a variety of programming languages. We're trying to keep this lean in terms of download size so that it can be embedded, such as in [quicktype-playground](https://github.com/quicktype/quicktype-playground).
- `quicktype-core`: This is the engine of quicktype. It takes JSON, JSON Schema, and GraphQL input and produces output in a variety of programming languages. We're trying to keep this lean in terms of download size so that it can be embedded, such as in [quicktype-playground](https://github.com/quicktype/quicktype-playground).

* `quicktype-typescript-input`: This is a bit of code that allows TypeScript code to be fed as input to `quicktype-core`, by transforming it to JSON Schema with [typescript-json-schema](https://github.com/YousefED/typescript-json-schema). It depends on `quicktype-core`.
- `quicktype-typescript-input`: This is a bit of code that allows TypeScript code to be fed as input to `quicktype-core`, by transforming it to JSON Schema with [typescript-json-schema](https://github.com/YousefED/typescript-json-schema). It depends on `quicktype-core`.

* `quicktype-graphql-input`: This is the GraphQL input module. It's split off into a separate package because it's not used in the web UI and `quicktype-playgrounds`, and it uses the moderately sized `graphql` dependency.
- `quicktype-graphql-input`: This is the GraphQL input module. It's split off into a separate package because it's not used in the web UI and `quicktype-playgrounds`, and it uses the moderately sized `graphql` dependency.

* `quicktype`: This is the command line interface for quicktype. It's a monolithic package that doesn't depend on the other packages, but contains all their code directly. This is mainly for ease of development. Packages that want to use quicktype's CLI interface, such as [json-to-azure-node-schema](https://github.com/json-helpers/json-to-azure-node-schema) will have to use this package.
- `quicktype`: This is the command line interface for quicktype. It's a monolithic package that doesn't depend on the other packages, but contains all their code directly. This is mainly for ease of development. Packages that want to use quicktype's CLI interface, such as [json-to-azure-node-schema](https://github.com/json-helpers/json-to-azure-node-schema) will have to use this package.

# Module resolution

`quicktype-typescript-input` and `quicktype-graphql-input` have to work both as their own packages, depending on the `quicktype-core` package, as well as part of `quicktype`, referring to the files in the local `src/quicktype-core` directory.

In addition, since those two input packages depend on `quicktype-core`, we would have to first build `quicktype-core`, publish it, and then build the input packages, depending on the just published `quicktype-core`. This is bad for development, since we couldn't do modifications to all packages without publishing, if we want to test independent of the `quicktype` package. The same goes for CI. Therefore, the two have to build as packages depending on the local `build/quicktype-core` package, but have to be published depending on the proper `quicktype-core` NPM package. We solve this the following way:

* All packages, including `quicktype-typescript-input` and `quicktype-graphql-input`, import files with local paths, such as `"../quicktype-core"`. This seems the only way to make VSCode's TypeScript integration, as well as `ts-node` happy. Unfortunately, since local paths can's use `tsc`'s path mapping, we have to rewrite those paths _before_ compiling, which is done in `build/quicktype-*-input/build.ts`: it copies all the sources, rewrites them, compiles, and then deletes the copied sources again.
- All packages, including `quicktype-typescript-input` and `quicktype-graphql-input`, import files with local paths, such as `"../quicktype-core"`. This seems the only way to make VSCode's TypeScript integration, as well as `ts-node` happy. Unfortunately, since local paths can's use `tsc`'s path mapping, we have to rewrite those paths _before_ compiling, which is done in `build/quicktype-*-input/build.ts`: it copies all the sources, rewrites them, compiles, and then deletes the copied sources again.

* Depending on whether we build the input packages, or publish them, their `package.json`s will have to refer to either the local `quicktype-core` package, or the NPM one. This is also done by the build script, which replaces the dependency with the right one for the job.
- Depending on whether we build the input packages, or publish them, their `package.json`s will have to refer to either the local `quicktype-core` package, or the NPM one. This is also done by the build script, which replaces the dependency with the right one for the job.

## Issues

Expand Down
10 changes: 5 additions & 5 deletions doc/Transformers.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ quicktype has facilities for transforming JSON strings into other data types, pr

There are two different sorts of transformed string types:

- Those that transform strings into other JSON-representable types, such as stringified integers.
- Those that transform strings into other JSON-representable types, such as stringified integers.

- Those that transform into types that are not directly representable in JSON (other than as strings), such as date/time types.
- Those that transform into types that are not directly representable in JSON (other than as strings), such as date/time types.

Several pieces need to be implemented to add a new transformed string type:

Expand All @@ -22,8 +22,8 @@ Several pieces need to be implemented to add a new transformed string type:

## Stuff we need to improve

- Automatic generation of tests. We should have a test generator that produces test files with all string types in all reasonable combinations.
- Automatic generation of tests. We should have a test generator that produces test files with all string types in all reasonable combinations.

- One CLI option for all string types. No CLI option work should be necessary to implement a new string type.
- One CLI option for all string types. No CLI option work should be necessary to implement a new string type.

- The AJV thing should be automated, too. We have almost all the validation code necessary in `StringTypes.ts` anyway.
- The AJV thing should be automated, too. We have almost all the validation code necessary in `StringTypes.ts` anyway.
5 changes: 4 additions & 1 deletion packages/quicktype-core/src/DeclarationIR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ export interface Declaration {
export class DeclarationIR {
readonly declarations: ReadonlyArray<Declaration>;

constructor(declarations: Iterable<Declaration>, readonly forwardedTypes: Set<Type>) {
constructor(
declarations: Iterable<Declaration>,
readonly forwardedTypes: Set<Type>
) {
this.declarations = Array.from(declarations);
}
}
Expand Down
17 changes: 14 additions & 3 deletions packages/quicktype-core/src/Naming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ export type NameStyle = (rawName: string) => string;
export class Namer {
private readonly _prefixes: ReadonlySet<string>;

constructor(readonly name: string, readonly nameStyle: NameStyle, prefixes: string[]) {
constructor(
readonly name: string,
readonly nameStyle: NameStyle,
prefixes: string[]
) {
this._prefixes = new Set(prefixes);
}

Expand Down Expand Up @@ -177,7 +181,10 @@ export abstract class Name {
private readonly _associates = new Set<AssociatedName>();

// If a Named is fixed, the namingFunction is undefined.
constructor(private readonly _namingFunction: Namer | undefined, readonly order: number) {}
constructor(
private readonly _namingFunction: Namer | undefined,
readonly order: number
) {}

addAssociate(associate: AssociatedName): void {
this._associates.add(associate);
Expand Down Expand Up @@ -255,7 +262,11 @@ export class SimpleName extends Name {
}

export class AssociatedName extends Name {
constructor(private readonly _sponsor: Name, order: number, readonly getName: (sponsorName: string) => string) {
constructor(
private readonly _sponsor: Name,
order: number,
readonly getName: (sponsorName: string) => string
) {
super(undefined, order);
}

Expand Down
5 changes: 4 additions & 1 deletion packages/quicktype-core/src/Renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,10 @@ export abstract class Renderer {

private _emitContext: EmitContext;

constructor(protected readonly targetLanguage: TargetLanguage, renderContext: RenderContext) {
constructor(
protected readonly targetLanguage: TargetLanguage,
renderContext: RenderContext
) {
this.typeGraph = renderContext.typeGraph;
this.leadingComments = renderContext.leadingComments;

Expand Down
2 changes: 1 addition & 1 deletion packages/quicktype-core/src/Source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export function sourcelikeToSource(sl: Sourcelike): Source {
kind: "sequence",
sequence: arrayIntercalate(
newline(),
lines.map((l: string) => ({ kind: "text", text: l } as Source))
lines.map((l: string) => ({ kind: "text", text: l }) as Source)
)
};
}
Expand Down
6 changes: 5 additions & 1 deletion packages/quicktype-core/src/TargetLanguage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import { DateTimeRecognizer, DefaultDateTimeRecognizer } from "./DateTime";
export type MultiFileRenderResult = ReadonlyMap<string, SerializedRenderResult>;

export abstract class TargetLanguage {
constructor(readonly displayName: string, readonly names: string[], readonly extension: string) {}
constructor(
readonly displayName: string,
readonly names: string[],
readonly extension: string
) {}

protected abstract getOptions(): Option<any>[];

Expand Down
47 changes: 40 additions & 7 deletions packages/quicktype-core/src/Transformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ function getNumberOfNodes(xfer: Transformer | undefined): number {
}

export abstract class Transformer {
constructor(readonly kind: string, protected readonly graph: TypeGraph, readonly sourceTypeRef: TypeRef) {}
constructor(
readonly kind: string,
protected readonly graph: TypeGraph,
readonly sourceTypeRef: TypeRef
) {}

get sourceType(): Type {
return derefTypeRef(this.sourceTypeRef, this.graph);
Expand Down Expand Up @@ -73,7 +77,12 @@ export abstract class Transformer {
}

export abstract class ProducerTransformer extends Transformer {
constructor(kind: string, graph: TypeGraph, sourceTypeRef: TypeRef, readonly consumer: Transformer | undefined) {
constructor(
kind: string,
graph: TypeGraph,
sourceTypeRef: TypeRef,
readonly consumer: Transformer | undefined
) {
super(kind, graph, sourceTypeRef);
}

Expand Down Expand Up @@ -105,7 +114,12 @@ export abstract class ProducerTransformer extends Transformer {
}

export abstract class MatchTransformer extends Transformer {
constructor(kind: string, graph: TypeGraph, sourceTypeRef: TypeRef, readonly transformer: Transformer) {
constructor(
kind: string,
graph: TypeGraph,
sourceTypeRef: TypeRef,
readonly transformer: Transformer
) {
super(kind, graph, sourceTypeRef);
}

Expand Down Expand Up @@ -342,7 +356,11 @@ export class ArrayEncodingTransformer extends Transformer {
}

export class ChoiceTransformer extends Transformer {
constructor(graph: TypeGraph, sourceTypeRef: TypeRef, public readonly transformers: ReadonlyArray<Transformer>) {
constructor(
graph: TypeGraph,
sourceTypeRef: TypeRef,
public readonly transformers: ReadonlyArray<Transformer>
) {
super("choice", graph, sourceTypeRef);
assert(transformers.length > 0, "Choice must have at least one transformer");
}
Expand Down Expand Up @@ -603,7 +621,12 @@ export class DecodingChoiceTransformer extends Transformer {
}

export class UnionMemberMatchTransformer extends MatchTransformer {
constructor(graph: TypeGraph, sourceTypeRef: TypeRef, transformer: Transformer, readonly memberTypeRef: TypeRef) {
constructor(
graph: TypeGraph,
sourceTypeRef: TypeRef,
transformer: Transformer,
readonly memberTypeRef: TypeRef
) {
super("union-member-match", graph, sourceTypeRef, transformer);
}

Expand Down Expand Up @@ -660,7 +683,12 @@ export class UnionMemberMatchTransformer extends MatchTransformer {
* This matches strings and enum cases.
*/
export class StringMatchTransformer extends MatchTransformer {
constructor(graph: TypeGraph, sourceTypeRef: TypeRef, transformer: Transformer, readonly stringCase: string) {
constructor(
graph: TypeGraph,
sourceTypeRef: TypeRef,
transformer: Transformer,
readonly stringCase: string
) {
super("string-match", graph, sourceTypeRef, transformer);
}

Expand Down Expand Up @@ -744,7 +772,12 @@ export class UnionInstantiationTransformer extends Transformer {
* Produces a string or an enum case.
*/
export class StringProducerTransformer extends ProducerTransformer {
constructor(graph: TypeGraph, sourceTypeRef: TypeRef, consumer: Transformer | undefined, readonly result: string) {
constructor(
graph: TypeGraph,
sourceTypeRef: TypeRef,
consumer: Transformer | undefined,
readonly result: string
) {
super("string-producer", graph, sourceTypeRef, consumer);
}

Expand Down
Loading
Loading