From 03ea4e4b85ce2990a5a9b23e4b5396ae09993dff Mon Sep 17 00:00:00 2001 From: spinillos Date: Fri, 21 Jul 2023 12:34:15 +0200 Subject: [PATCH 1/8] Use _#schema to generate TS files --- encoding/typescript/gen.go | 44 ++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/encoding/typescript/gen.go b/encoding/typescript/gen.go index c6ed6eb..967b97d 100644 --- a/encoding/typescript/gen.go +++ b/encoding/typescript/gen.go @@ -71,34 +71,28 @@ func GenerateTypes(sch thema.Schema, cfg *TypeConfig) (*ast.File, error) { cfg.RootName = strings.Title(sch.Lineage().Name()) } - file := &ts.File{} - for _, path := range []cue.Selector{cue.Str("schema"), cue.Hid("_join", "github.com/grafana/thema")} { - schdef := sch.Underlying().LookupPath(cue.MakePath(path)) - // Cuetsy only accepts structs as root file, otherwise it fails. - // _join could be _ and we can skip it when it happens to avoid to break the whole generation process. - if schdef.IncompleteKind() != cue.StructKind { - continue + schdef := sch.Underlying().LookupPath(cue.MakePath(cue.Hid("_#schema", "github.com/grafana/thema"))) + tf, err := cuetsy.GenerateAST(schdef, *cfg.CuetsyConfig) + if err != nil { + return nil, fmt.Errorf("generating TS for child elements of schema failed: %w", err) + } + + file := &ts.File{ + Nodes: tf.Nodes, + } + + if !cfg.Group { + as := cuetsy.TypeInterface + if cfg.RootAsType { + as = cuetsy.TypeAlias } - tf, err := cuetsy.GenerateAST(schdef, *cfg.CuetsyConfig) + top, err := cuetsy.GenerateSingleAST(cfg.RootName, schdef, as) if err != nil { - return nil, fmt.Errorf("generating TS for child elements of schema failed: %w", err) + return nil, fmt.Errorf("generating TS for schema root failed: %w", err) } - - file.Nodes = append(file.Nodes, tf.Nodes...) - - if !cfg.Group { - as := cuetsy.TypeInterface - if cfg.RootAsType { - as = cuetsy.TypeAlias - } - top, err := cuetsy.GenerateSingleAST(cfg.RootName, schdef, as) - if err != nil { - return nil, fmt.Errorf("generating TS for schema root failed: %w", err) - } - file.Nodes = append(file.Nodes, top.T) - if top.D != nil { - file.Nodes = append(file.Nodes, top.D) - } + file.Nodes = append(file.Nodes, top.T) + if top.D != nil { + file.Nodes = append(file.Nodes, top.D) } } From 02dd3b9e75f78f932fdf49b9cecc8d1a145c0896 Mon Sep 17 00:00:00 2001 From: spinillos Date: Mon, 24 Jul 2023 11:09:00 +0200 Subject: [PATCH 2/8] Update cuetsy --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 9449550..a58e8ab 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,7 @@ require ( github.com/getkin/kin-openapi v0.115.0 github.com/golangci/lint-1 v0.0.0-20181222135242-d2cdd8c08219 github.com/google/go-cmp v0.5.8 - github.com/grafana/cuetsy v0.1.10 + github.com/grafana/cuetsy v0.1.11 github.com/labstack/echo/v4 v4.9.1 github.com/matryer/moq v0.2.7 github.com/spf13/cobra v1.4.0 diff --git a/go.sum b/go.sum index 39ae95f..fe6aca3 100644 --- a/go.sum +++ b/go.sum @@ -110,8 +110,8 @@ github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+ github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= -github.com/grafana/cuetsy v0.1.10 h1:+W9/7roI8LorL+D1RJhKGdhsTZ81adrK9dHS0r7qsXs= -github.com/grafana/cuetsy v0.1.10/go.mod h1:Ix97+CPD8ws9oSSxR3/Lf4ahU1I4Np83kjJmDVnLZvc= +github.com/grafana/cuetsy v0.1.11 h1:I3IwBhF+UaQxRM79HnImtrAn8REGdb5M3+C4QrYHoWk= +github.com/grafana/cuetsy v0.1.11/go.mod h1:Ix97+CPD8ws9oSSxR3/Lf4ahU1I4Np83kjJmDVnLZvc= github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= From 33693e0ac116e868ef9c3a130910058ff085bf35 Mon Sep 17 00:00:00 2001 From: spinillos Date: Mon, 24 Jul 2023 17:45:47 +0200 Subject: [PATCH 3/8] Add ts tests --- encoding/typescript/gen.go | 3 + encoding/typescript/ts_test.go | 62 +++++++++++++++++++ testdata/lineage/basic-multiversion.txtar | 56 +++++++++++++++++ testdata/lineage/embedexref.txtar | 6 ++ testdata/lineage/embedref.txtar | 6 ++ testdata/lineage/expand.txtar | 27 ++++++++ testdata/lineage/go-any.txtar | 11 ++++ testdata/lineage/join/embedref.txtar | 7 +++ testdata/lineage/join/exref.txtar | 11 ++++ testdata/lineage/join/nearoptional.txtar | 17 +++++ testdata/lineage/join/onenone.txtar | 5 ++ testdata/lineage/join/oneone.txtar | 6 ++ testdata/lineage/join/onestruct.txtar | 8 +++ testdata/lineage/join/repeat.txtar | 5 ++ testdata/lineage/maps.txtar | 21 +++++++ testdata/lineage/nearoptional.txtar | 17 +++++ testdata/lineage/noref.txtar | 5 ++ testdata/lineage/one-schema-versionless.txtar | 5 ++ testdata/lineage/optional.txtar | 16 +++++ testdata/lineage/refexstruct.txtar | 9 +++ testdata/lineage/refstruct.txtar | 17 +++++ testdata/lineage/scalars.txtar | 22 +++++++ testdata/lineage/trivial-two-comments.txtar | 18 ++++++ testdata/lineage/trivial-two.txtar | 9 +++ testdata/lineage/unifyref.txtar | 10 +++ testdata/lineage/union-null.txtar | 22 +++++++ testdata/lineage/union.txtar | 26 ++++++++ 27 files changed, 427 insertions(+) create mode 100644 encoding/typescript/ts_test.go diff --git a/encoding/typescript/gen.go b/encoding/typescript/gen.go index 967b97d..429191d 100644 --- a/encoding/typescript/gen.go +++ b/encoding/typescript/gen.go @@ -64,6 +64,9 @@ func GenerateTypes(sch thema.Schema, cfg *TypeConfig) (*ast.File, error) { } if cfg.CuetsyConfig == nil { cfg.CuetsyConfig = &cuetsy.Config{ + ImportMapper: func(_ string) (string, error) { + return "", nil + }, Export: true, } } diff --git a/encoding/typescript/ts_test.go b/encoding/typescript/ts_test.go new file mode 100644 index 0000000..b7acab3 --- /dev/null +++ b/encoding/typescript/ts_test.go @@ -0,0 +1,62 @@ +package typescript + +import ( + "github.com/stretchr/testify/require" + "testing" + + "cuelang.org/go/cue/cuecontext" + "github.com/grafana/thema" + "github.com/grafana/thema/internal/txtartest/bindlin" + "github.com/grafana/thema/internal/txtartest/vanilla" +) + +func TestGenerate(t *testing.T) { + test := vanilla.TxTarTest{ + Root: "../../testdata/lineage", + Name: "encoding/typescript/TestGenerate", + ThemaFS: thema.CueJointFS, + Skip: map[string]string{ + "lineage/refexscalar": "bounds constraints are not supported as they lack a direct typescript equivalent", + "lineage/refscalar": "bounds constraints are not supported as they lack a direct typescript equivalent", + }, + } + + ctx := cuecontext.New() + rt := thema.NewRuntime(ctx) + + table := []struct { + name string + cfg *TypeConfig + }{ + { + name: "nilcfg", + cfg: nil, + }, + {}, + } + + for _, tb := range table { + t.Run(tb.name, func(t *testing.T) { + testcpy := test + testcpy.Name += "/" + tb.name + testcpy.Run(t, func(tc *vanilla.Test) { + if testing.Short() && tc.HasTag("slow") { + t.Skip("case is tagged #slow, skipping for -short") + } + lin, err := bindlin.BindTxtarLineage(tc, rt) + if err != nil { + tc.Fatal(err) + } + + for sch := lin.First(); sch != nil; sch = sch.Successor() { + f, err := GenerateTypes(sch, tb.cfg) + if err != nil { + tc.Fatal(err) + } + _, err = tc.Write([]byte(f.String())) //nolint:gosec,errcheck + require.NoError(t, err) + } + }) + }) + } +} diff --git a/testdata/lineage/basic-multiversion.txtar b/testdata/lineage/basic-multiversion.txtar index 40ae6bc..63e36a1 100644 --- a/testdata/lineage/basic-multiversion.txtar +++ b/testdata/lineage/basic-multiversion.txtar @@ -1840,3 +1840,59 @@ type Basicmultiversion struct { // BasicmultiversionWithDefault defines model for Basicmultiversion.WithDefault. type BasicmultiversionWithDefault string + +-- out/encoding/typescript/TestGenerate/nilcfg -- +export interface Basic-Multiversion { + init: string; +} +export interface Basic-Multiversion { + init: string; + optional?: number; +} +export interface Basic-Multiversion { + init: string; + optional?: number; + withDefault?: ('foo' | 'bar'); +} + +export const defaultBasic-Multiversion: Partial = { + withDefault: 'foo', +}; +export interface Basic-Multiversion { + init: string; + optional?: number; + withDefault?: ('foo' | 'bar' | 'baz'); +} + +export const defaultBasic-Multiversion: Partial = { + withDefault: 'foo', +}; +export interface Basic-Multiversion { + optional?: number; + renamed: string; + withDefault: ('foo' | 'bar' | 'baz'); +} + +export const defaultBasic-Multiversion: Partial = { + withDefault: 'bar', +}; +export interface Basic-Multiversion { + optional?: number; + renamed: string; + withDefault: ('foo' | 'bar' | 'baz' | 'bing'); +} + +export const defaultBasic-Multiversion: Partial = { + withDefault: 'bar', +}; +export interface Basic-Multiversion { + optional?: number; + toObj: { + init: string; + }; + withDefault: ('foo' | 'bar' | 'baz' | 'bing'); +} + +export const defaultBasic-Multiversion: Partial = { + withDefault: 'bar', +}; diff --git a/testdata/lineage/embedexref.txtar b/testdata/lineage/embedexref.txtar index 259e36d..738da13 100644 --- a/testdata/lineage/embedexref.txtar +++ b/testdata/lineage/embedexref.txtar @@ -189,3 +189,9 @@ type EmbedexrefRefField2 int Schema count: 1 Schema versions: 0.0 Lenses count: 0 + +-- out/encoding/typescript/TestGenerate/nilcfg -- +export interface Embedexref { + refField1: string; + refField2: 42; +} diff --git a/testdata/lineage/embedref.txtar b/testdata/lineage/embedref.txtar index 042a30e..e3a3615 100644 --- a/testdata/lineage/embedref.txtar +++ b/testdata/lineage/embedref.txtar @@ -314,3 +314,9 @@ type Embedref struct { // EmbedrefRefField2 defines model for Embedref.RefField2. type EmbedrefRefField2 int + +-- out/encoding/typescript/TestGenerate/nilcfg -- +export interface Embedref { + refField1: string; + refField2: 42; +} diff --git a/testdata/lineage/expand.txtar b/testdata/lineage/expand.txtar index c28ff4a..6e28076 100644 --- a/testdata/lineage/expand.txtar +++ b/testdata/lineage/expand.txtar @@ -687,3 +687,30 @@ type Expand struct { // ExpandWithDefault defines model for Expand.WithDefault. type ExpandWithDefault string + +-- out/encoding/typescript/TestGenerate/nilcfg -- +export interface Expand { + init: string; +} +export interface Expand { + init: string; + optional?: number; +} +export interface Expand { + init: string; + optional?: number; + withDefault?: ('foo' | 'bar'); +} + +export const defaultExpand: Partial = { + withDefault: 'foo', +}; +export interface Expand { + init: string; + optional?: number; + withDefault?: ('foo' | 'bar' | 'baz'); +} + +export const defaultExpand: Partial = { + withDefault: 'foo', +}; diff --git a/testdata/lineage/go-any.txtar b/testdata/lineage/go-any.txtar index 9f2d891..9fe520a 100644 --- a/testdata/lineage/go-any.txtar +++ b/testdata/lineage/go-any.txtar @@ -314,3 +314,14 @@ type Goany struct { } `json:"structVal"` Value any `json:"value"` } + +-- out/encoding/typescript/TestGenerate/nilcfg -- +export interface Go-Any { + emptyMap: Record; + optional?: (string | boolean); + structVal: { + inner: (string | number); + innerOptional?: unknown; + }; + value: (string | boolean); +} diff --git a/testdata/lineage/join/embedref.txtar b/testdata/lineage/join/embedref.txtar index dc2a4ab..65eccd4 100644 --- a/testdata/lineage/join/embedref.txtar +++ b/testdata/lineage/join/embedref.txtar @@ -218,3 +218,10 @@ type Embedref struct { // EmbedrefRefField2 defines model for Embedref.RefField2. type EmbedrefRefField2 int + +-- out/encoding/typescript/TestGenerate/nilcfg -- +export interface Embedref { + foo: string; + refField1: string; + refField2: 42; +} diff --git a/testdata/lineage/join/exref.txtar b/testdata/lineage/join/exref.txtar index c9f4986..d7b2e8a 100644 --- a/testdata/lineage/join/exref.txtar +++ b/testdata/lineage/join/exref.txtar @@ -268,3 +268,14 @@ type Exref struct { Ref ExRef `json:"ref"` Refdef ExRefDef `json:"refdef"` } + +-- out/encoding/typescript/TestGenerate/nilcfg -- +export interface Exref { + foo: string; + ref: { + normalField: string; + }; + refdef: { + defField: string; + }; +} diff --git a/testdata/lineage/join/nearoptional.txtar b/testdata/lineage/join/nearoptional.txtar index 905031a..0bae85b 100644 --- a/testdata/lineage/join/nearoptional.txtar +++ b/testdata/lineage/join/nearoptional.txtar @@ -276,3 +276,20 @@ type Nearoptional struct { } `json:"astruct,omitempty"` Notoptional int32 `json:"notoptional"` } + +-- out/encoding/typescript/TestGenerate/nilcfg -- +export interface Nearoptional { + abool?: boolean; + abytes?: string; + alist?: Array; + anint?: number; + astring?: string; + astruct?: { + nested: string; + }; + notoptional: number; +} + +export const defaultNearoptional: Partial = { + alist: [], +}; diff --git a/testdata/lineage/join/onenone.txtar b/testdata/lineage/join/onenone.txtar index d4c60e9..ec377ca 100644 --- a/testdata/lineage/join/onenone.txtar +++ b/testdata/lineage/join/onenone.txtar @@ -122,3 +122,8 @@ package onenone // Foo defines model for foo. type Foo = string + +-- out/encoding/typescript/TestGenerate/nilcfg -- +export interface Onenone { + foo: string; +} diff --git a/testdata/lineage/join/oneone.txtar b/testdata/lineage/join/oneone.txtar index eae87de..5e775f9 100644 --- a/testdata/lineage/join/oneone.txtar +++ b/testdata/lineage/join/oneone.txtar @@ -149,3 +149,9 @@ type Oneone struct { Bar string `json:"bar"` Foo string `json:"foo"` } + +-- out/encoding/typescript/TestGenerate/nilcfg -- +export interface Oneone { + bar: string; + foo: string; +} diff --git a/testdata/lineage/join/onestruct.txtar b/testdata/lineage/join/onestruct.txtar index 88c53a1..2976055 100644 --- a/testdata/lineage/join/onestruct.txtar +++ b/testdata/lineage/join/onestruct.txtar @@ -185,3 +185,11 @@ type AField struct { // Foo defines model for foo. type Foo = string + +-- out/encoding/typescript/TestGenerate/nilcfg -- +export interface Onestruct { + aField: { + defLitField: string; + }; + foo: string; +} diff --git a/testdata/lineage/join/repeat.txtar b/testdata/lineage/join/repeat.txtar index c3d39a8..b217ddc 100644 --- a/testdata/lineage/join/repeat.txtar +++ b/testdata/lineage/join/repeat.txtar @@ -123,3 +123,8 @@ package repeat type Repeat struct { Foo string `json:"foo"` } + +-- out/encoding/typescript/TestGenerate/nilcfg -- +export interface Repeat { + foo: string; +} diff --git a/testdata/lineage/maps.txtar b/testdata/lineage/maps.txtar index 84ca6b4..239bb0a 100644 --- a/testdata/lineage/maps.txtar +++ b/testdata/lineage/maps.txtar @@ -605,3 +605,24 @@ type ValPrimitive map[string]bool type ValStruct map[string]struct { Foo string `json:"foo"` } + +-- out/encoding/typescript/TestGenerate/nilcfg -- +export interface Maps { + aComplexMap?: { + foo: string; + }; + optValList?: Record>; + optValPrimitive?: Record; + optValStruct?: Record; + refValue: Record; + someField: Record; + valList: Record>; + valPrimitive: Record; + valStruct: Record; +} diff --git a/testdata/lineage/nearoptional.txtar b/testdata/lineage/nearoptional.txtar index db8944e..4c3be2b 100644 --- a/testdata/lineage/nearoptional.txtar +++ b/testdata/lineage/nearoptional.txtar @@ -294,3 +294,20 @@ type Nearoptional struct { } `json:"astruct,omitempty"` Notoptional int32 `json:"notoptional"` } + +-- out/encoding/typescript/TestGenerate/nilcfg -- +export interface Nearoptional { + abool?: boolean; + abytes?: string; + alist?: Array; + anint?: number; + astring?: string; + astruct?: { + nested: string; + }; + notoptional: number; +} + +export const defaultNearoptional: Partial = { + alist: [], +}; diff --git a/testdata/lineage/noref.txtar b/testdata/lineage/noref.txtar index 2f82a4f..a64050b 100644 --- a/testdata/lineage/noref.txtar +++ b/testdata/lineage/noref.txtar @@ -224,3 +224,8 @@ type Baz struct { // SomeField defines model for someField. type SomeField = string + +-- out/encoding/typescript/TestGenerate/nilcfg -- +export interface Noref { + someField: string; +} diff --git a/testdata/lineage/one-schema-versionless.txtar b/testdata/lineage/one-schema-versionless.txtar index 508bcf1..380ab65 100644 --- a/testdata/lineage/one-schema-versionless.txtar +++ b/testdata/lineage/one-schema-versionless.txtar @@ -119,3 +119,8 @@ package oneschemaversionless // Firstfield defines model for firstfield. type Firstfield = string + +-- out/encoding/typescript/TestGenerate/nilcfg -- +export interface One-Schema-Versionless { + firstfield: string; +} diff --git a/testdata/lineage/optional.txtar b/testdata/lineage/optional.txtar index ab48abc..4b4587b 100644 --- a/testdata/lineage/optional.txtar +++ b/testdata/lineage/optional.txtar @@ -18,3 +18,19 @@ schemas: [{ } }] lenses: [] + +-- out/encoding/typescript/TestGenerate/nilcfg -- +export interface Optional { + abool?: boolean; + abytes?: string; + alist?: Array; + anint?: number; + astring?: string; + astruct?: { + nested: string; + }; +} + +export const defaultOptional: Partial = { + alist: [], +}; diff --git a/testdata/lineage/refexstruct.txtar b/testdata/lineage/refexstruct.txtar index 39317d4..ba0df00 100644 --- a/testdata/lineage/refexstruct.txtar +++ b/testdata/lineage/refexstruct.txtar @@ -212,3 +212,12 @@ type Baz struct { type Refexstruct struct { ABaz Baz `json:"aBaz"` } + +-- out/encoding/typescript/TestGenerate/nilcfg -- +export interface Refexstruct { + aBaz: { + run: string; + tell: string; + dat: number; + }; +} diff --git a/testdata/lineage/refstruct.txtar b/testdata/lineage/refstruct.txtar index e106ffa..be9bce0 100644 --- a/testdata/lineage/refstruct.txtar +++ b/testdata/lineage/refstruct.txtar @@ -446,3 +446,20 @@ type Refstruct struct { ABaz Baz `json:"aBaz"` Disj any `json:"disj"` } + +-- out/encoding/typescript/TestGenerate/nilcfg -- +export interface Refstruct { + aBaz: { + run: string; + tell?: string; + dat: number; + }; + disj: ({ + run: string; + tell?: string; + dat: number; + } | { + one: string; + two: string; + }); +} diff --git a/testdata/lineage/scalars.txtar b/testdata/lineage/scalars.txtar index dae118a..538696a 100644 --- a/testdata/lineage/scalars.txtar +++ b/testdata/lineage/scalars.txtar @@ -730,3 +730,25 @@ type SomeUInt8 = int // StringWithLength defines model for stringWithLength. type StringWithLength = string + +-- out/encoding/typescript/TestGenerate/nilcfg -- +export interface Scalar-Fields { + intWithBounds: number; + nullableIntWithDefault: (number | null); + nullableIntWithNoDefault: (number | null); + someFloat32: number; + someFloat64: number; + someInt16: number; + someInt32: number; + someInt64: number; + someInt8: number; + someUInt16: number; + someUInt32: number; + someUInt64: number; + someUInt8: number; + stringWithLength: string; +} + +export const defaultScalar-Fields: Partial = { + nullableIntWithDefault: 10, +}; diff --git a/testdata/lineage/trivial-two-comments.txtar b/testdata/lineage/trivial-two-comments.txtar index a40aaf3..52c1b7d 100644 --- a/testdata/lineage/trivial-two-comments.txtar +++ b/testdata/lineage/trivial-two-comments.txtar @@ -280,3 +280,21 @@ type Trivialtwocomments struct { // Secondfield but clearly this one is a great idea Secondfield int32 `json:"secondfield,omitempty"` } + +-- out/encoding/typescript/TestGenerate/nilcfg -- +export interface Trivial-Two-Comments { + /** + * TODO some thing to be done + */ + firstfield: string; +} +export interface Trivial-Two-Comments { + /** + * TODO some thing to be done + */ + firstfield: string; + /** + * but clearly this one is a great idea + */ + secondfield?: number; +} diff --git a/testdata/lineage/trivial-two.txtar b/testdata/lineage/trivial-two.txtar index 4d5389e..6b7ef85 100644 --- a/testdata/lineage/trivial-two.txtar +++ b/testdata/lineage/trivial-two.txtar @@ -269,3 +269,12 @@ type Trivialtwo struct { Firstfield string `json:"firstfield"` Secondfield int32 `json:"secondfield,omitempty"` } + +-- out/encoding/typescript/TestGenerate/nilcfg -- +export interface Trivial-Two { + firstfield: string; +} +export interface Trivial-Two { + firstfield: string; + secondfield?: number; +} diff --git a/testdata/lineage/unifyref.txtar b/testdata/lineage/unifyref.txtar index a5809f2..ca9dede 100644 --- a/testdata/lineage/unifyref.txtar +++ b/testdata/lineage/unifyref.txtar @@ -349,3 +349,13 @@ type Unifyref struct { Schema count: 1 Schema versions: 0.0 Lenses count: 0 + +-- out/encoding/typescript/TestGenerate/nilcfg -- +export interface Unifyref { + afoo: { + extfield: string; + optf?: { + another: string; + }; + }; +} diff --git a/testdata/lineage/union-null.txtar b/testdata/lineage/union-null.txtar index 0aaccd3..098fc4f 100644 --- a/testdata/lineage/union-null.txtar +++ b/testdata/lineage/union-null.txtar @@ -471,3 +471,25 @@ type Unionnull struct { WithNull *string `json:"withNull"` } `json:"kindString"` } + +-- out/encoding/typescript/TestGenerate/nilcfg -- +export interface Union-Null { + kindFloat: { + simpleFloat64: number; + simpleFloat32: number; + withNull64: (number | null); + withNull32: (number | null); + }; + kindInt: { + simpleInt: number; + simpleInt32: number; + simpleInt64: number; + withNull: (number | null); + withNull64: (number | null); + withNull32: (number | null); + }; + kindString: { + simpleString: string; + withNull: (string | null); + }; +} diff --git a/testdata/lineage/union.txtar b/testdata/lineage/union.txtar index 9e52ef9..84ba908 100644 --- a/testdata/lineage/union.txtar +++ b/testdata/lineage/union.txtar @@ -594,3 +594,29 @@ lenses: [] Schema count: 1 Schema versions: 0.0 Lenses count: 0 + +-- out/encoding/typescript/TestGenerate/nilcfg -- +export interface Union { + doubleList: Array | '#UnionDef')>>; + emptyStructs: Array>>; + listUnion: Array<(Record | '#UnionDef')>; + mapChained: Record | '#UnionDef')>>; + mapDoubleList: Record | '#UnionDef')>>>; + mapList: Record | '#UnionDef')>>; + mapListChained: Record | '#UnionDef')>>>>; + mapTripleList: Record | '#UnionDef')>>>>; + mapUnion: Record | '#UnionDef')>; + nestedStruct: { + structUnion: (Record | '#UnionDef'); + mapUnion: Record | '#UnionDef')>; + listUnion: Array<(Record | '#UnionDef')>; + }; + optionalUnion?: (Record | '#UnionDef'); + theUnion: (Record | '#UnionDef'); +} + +export const defaultUnion: Partial = { + doubleList: [], + emptyStructs: [], + listUnion: [], +}; From abb9f0af04d8f67703985873b84af5bf36905a40 Mon Sep 17 00:00:00 2001 From: spinillos Date: Tue, 25 Jul 2023 11:51:00 +0200 Subject: [PATCH 4/8] Use default import mapper --- encoding/typescript/gen.go | 6 ++---- encoding/typescript/ts_test.go | 1 - 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/encoding/typescript/gen.go b/encoding/typescript/gen.go index 429191d..4b5a3d9 100644 --- a/encoding/typescript/gen.go +++ b/encoding/typescript/gen.go @@ -64,10 +64,8 @@ func GenerateTypes(sch thema.Schema, cfg *TypeConfig) (*ast.File, error) { } if cfg.CuetsyConfig == nil { cfg.CuetsyConfig = &cuetsy.Config{ - ImportMapper: func(_ string) (string, error) { - return "", nil - }, - Export: true, + ImportMapper: cuetsy.IgnoreImportMapper, + Export: true, } } if cfg.RootName == "" { diff --git a/encoding/typescript/ts_test.go b/encoding/typescript/ts_test.go index b7acab3..3350782 100644 --- a/encoding/typescript/ts_test.go +++ b/encoding/typescript/ts_test.go @@ -32,7 +32,6 @@ func TestGenerate(t *testing.T) { name: "nilcfg", cfg: nil, }, - {}, } for _, tb := range table { From 7e36dc2ce9b611941a534b4e77a9e7e2f87907d2 Mon Sep 17 00:00:00 2001 From: spinillos Date: Tue, 25 Jul 2023 12:37:34 +0200 Subject: [PATCH 5/8] Fix test lint --- testdata/lineage/basic-multiversion.txtar | 1 - testdata/lineage/embedexref.txtar | 1 + testdata/lineage/embedref.txtar | 1 - testdata/lineage/expand.txtar | 1 - testdata/lineage/go-any.txtar | 1 - testdata/lineage/join/embedref.txtar | 1 - testdata/lineage/join/exref.txtar | 1 - testdata/lineage/join/nearoptional.txtar | 1 - testdata/lineage/join/onenone.txtar | 1 - testdata/lineage/join/oneone.txtar | 1 - testdata/lineage/join/onestruct.txtar | 1 - testdata/lineage/join/repeat.txtar | 1 - testdata/lineage/maps.txtar | 1 - testdata/lineage/nearoptional.txtar | 1 - testdata/lineage/noref.txtar | 1 - testdata/lineage/one-schema-versionless.txtar | 1 - testdata/lineage/optional.txtar | 1 - testdata/lineage/refexstruct.txtar | 1 - testdata/lineage/refstruct.txtar | 1 - testdata/lineage/scalars.txtar | 1 - testdata/lineage/trivial-two-comments.txtar | 1 - testdata/lineage/trivial-two.txtar | 1 - testdata/lineage/union-null.txtar | 1 - 23 files changed, 1 insertion(+), 22 deletions(-) diff --git a/testdata/lineage/basic-multiversion.txtar b/testdata/lineage/basic-multiversion.txtar index 63e36a1..1075126 100644 --- a/testdata/lineage/basic-multiversion.txtar +++ b/testdata/lineage/basic-multiversion.txtar @@ -1840,7 +1840,6 @@ type Basicmultiversion struct { // BasicmultiversionWithDefault defines model for Basicmultiversion.WithDefault. type BasicmultiversionWithDefault string - -- out/encoding/typescript/TestGenerate/nilcfg -- export interface Basic-Multiversion { init: string; diff --git a/testdata/lineage/embedexref.txtar b/testdata/lineage/embedexref.txtar index 738da13..a7bc620 100644 --- a/testdata/lineage/embedexref.txtar +++ b/testdata/lineage/embedexref.txtar @@ -190,6 +190,7 @@ Schema count: 1 Schema versions: 0.0 Lenses count: 0 + -- out/encoding/typescript/TestGenerate/nilcfg -- export interface Embedexref { refField1: string; diff --git a/testdata/lineage/embedref.txtar b/testdata/lineage/embedref.txtar index e3a3615..883439b 100644 --- a/testdata/lineage/embedref.txtar +++ b/testdata/lineage/embedref.txtar @@ -314,7 +314,6 @@ type Embedref struct { // EmbedrefRefField2 defines model for Embedref.RefField2. type EmbedrefRefField2 int - -- out/encoding/typescript/TestGenerate/nilcfg -- export interface Embedref { refField1: string; diff --git a/testdata/lineage/expand.txtar b/testdata/lineage/expand.txtar index 6e28076..c7083c5 100644 --- a/testdata/lineage/expand.txtar +++ b/testdata/lineage/expand.txtar @@ -687,7 +687,6 @@ type Expand struct { // ExpandWithDefault defines model for Expand.WithDefault. type ExpandWithDefault string - -- out/encoding/typescript/TestGenerate/nilcfg -- export interface Expand { init: string; diff --git a/testdata/lineage/go-any.txtar b/testdata/lineage/go-any.txtar index 9fe520a..2599f38 100644 --- a/testdata/lineage/go-any.txtar +++ b/testdata/lineage/go-any.txtar @@ -314,7 +314,6 @@ type Goany struct { } `json:"structVal"` Value any `json:"value"` } - -- out/encoding/typescript/TestGenerate/nilcfg -- export interface Go-Any { emptyMap: Record; diff --git a/testdata/lineage/join/embedref.txtar b/testdata/lineage/join/embedref.txtar index 65eccd4..49b6c4b 100644 --- a/testdata/lineage/join/embedref.txtar +++ b/testdata/lineage/join/embedref.txtar @@ -218,7 +218,6 @@ type Embedref struct { // EmbedrefRefField2 defines model for Embedref.RefField2. type EmbedrefRefField2 int - -- out/encoding/typescript/TestGenerate/nilcfg -- export interface Embedref { foo: string; diff --git a/testdata/lineage/join/exref.txtar b/testdata/lineage/join/exref.txtar index d7b2e8a..f47913b 100644 --- a/testdata/lineage/join/exref.txtar +++ b/testdata/lineage/join/exref.txtar @@ -268,7 +268,6 @@ type Exref struct { Ref ExRef `json:"ref"` Refdef ExRefDef `json:"refdef"` } - -- out/encoding/typescript/TestGenerate/nilcfg -- export interface Exref { foo: string; diff --git a/testdata/lineage/join/nearoptional.txtar b/testdata/lineage/join/nearoptional.txtar index 0bae85b..089c1b0 100644 --- a/testdata/lineage/join/nearoptional.txtar +++ b/testdata/lineage/join/nearoptional.txtar @@ -276,7 +276,6 @@ type Nearoptional struct { } `json:"astruct,omitempty"` Notoptional int32 `json:"notoptional"` } - -- out/encoding/typescript/TestGenerate/nilcfg -- export interface Nearoptional { abool?: boolean; diff --git a/testdata/lineage/join/onenone.txtar b/testdata/lineage/join/onenone.txtar index ec377ca..9ff35cd 100644 --- a/testdata/lineage/join/onenone.txtar +++ b/testdata/lineage/join/onenone.txtar @@ -122,7 +122,6 @@ package onenone // Foo defines model for foo. type Foo = string - -- out/encoding/typescript/TestGenerate/nilcfg -- export interface Onenone { foo: string; diff --git a/testdata/lineage/join/oneone.txtar b/testdata/lineage/join/oneone.txtar index 5e775f9..478beef 100644 --- a/testdata/lineage/join/oneone.txtar +++ b/testdata/lineage/join/oneone.txtar @@ -149,7 +149,6 @@ type Oneone struct { Bar string `json:"bar"` Foo string `json:"foo"` } - -- out/encoding/typescript/TestGenerate/nilcfg -- export interface Oneone { bar: string; diff --git a/testdata/lineage/join/onestruct.txtar b/testdata/lineage/join/onestruct.txtar index 2976055..e5665cf 100644 --- a/testdata/lineage/join/onestruct.txtar +++ b/testdata/lineage/join/onestruct.txtar @@ -185,7 +185,6 @@ type AField struct { // Foo defines model for foo. type Foo = string - -- out/encoding/typescript/TestGenerate/nilcfg -- export interface Onestruct { aField: { diff --git a/testdata/lineage/join/repeat.txtar b/testdata/lineage/join/repeat.txtar index b217ddc..2317d9f 100644 --- a/testdata/lineage/join/repeat.txtar +++ b/testdata/lineage/join/repeat.txtar @@ -123,7 +123,6 @@ package repeat type Repeat struct { Foo string `json:"foo"` } - -- out/encoding/typescript/TestGenerate/nilcfg -- export interface Repeat { foo: string; diff --git a/testdata/lineage/maps.txtar b/testdata/lineage/maps.txtar index 239bb0a..3e6f195 100644 --- a/testdata/lineage/maps.txtar +++ b/testdata/lineage/maps.txtar @@ -605,7 +605,6 @@ type ValPrimitive map[string]bool type ValStruct map[string]struct { Foo string `json:"foo"` } - -- out/encoding/typescript/TestGenerate/nilcfg -- export interface Maps { aComplexMap?: { diff --git a/testdata/lineage/nearoptional.txtar b/testdata/lineage/nearoptional.txtar index 4c3be2b..2a51697 100644 --- a/testdata/lineage/nearoptional.txtar +++ b/testdata/lineage/nearoptional.txtar @@ -294,7 +294,6 @@ type Nearoptional struct { } `json:"astruct,omitempty"` Notoptional int32 `json:"notoptional"` } - -- out/encoding/typescript/TestGenerate/nilcfg -- export interface Nearoptional { abool?: boolean; diff --git a/testdata/lineage/noref.txtar b/testdata/lineage/noref.txtar index a64050b..b27c58b 100644 --- a/testdata/lineage/noref.txtar +++ b/testdata/lineage/noref.txtar @@ -224,7 +224,6 @@ type Baz struct { // SomeField defines model for someField. type SomeField = string - -- out/encoding/typescript/TestGenerate/nilcfg -- export interface Noref { someField: string; diff --git a/testdata/lineage/one-schema-versionless.txtar b/testdata/lineage/one-schema-versionless.txtar index 380ab65..de7f865 100644 --- a/testdata/lineage/one-schema-versionless.txtar +++ b/testdata/lineage/one-schema-versionless.txtar @@ -119,7 +119,6 @@ package oneschemaversionless // Firstfield defines model for firstfield. type Firstfield = string - -- out/encoding/typescript/TestGenerate/nilcfg -- export interface One-Schema-Versionless { firstfield: string; diff --git a/testdata/lineage/optional.txtar b/testdata/lineage/optional.txtar index 4b4587b..d8af180 100644 --- a/testdata/lineage/optional.txtar +++ b/testdata/lineage/optional.txtar @@ -18,7 +18,6 @@ schemas: [{ } }] lenses: [] - -- out/encoding/typescript/TestGenerate/nilcfg -- export interface Optional { abool?: boolean; diff --git a/testdata/lineage/refexstruct.txtar b/testdata/lineage/refexstruct.txtar index ba0df00..8f22f8c 100644 --- a/testdata/lineage/refexstruct.txtar +++ b/testdata/lineage/refexstruct.txtar @@ -212,7 +212,6 @@ type Baz struct { type Refexstruct struct { ABaz Baz `json:"aBaz"` } - -- out/encoding/typescript/TestGenerate/nilcfg -- export interface Refexstruct { aBaz: { diff --git a/testdata/lineage/refstruct.txtar b/testdata/lineage/refstruct.txtar index be9bce0..3f15e68 100644 --- a/testdata/lineage/refstruct.txtar +++ b/testdata/lineage/refstruct.txtar @@ -446,7 +446,6 @@ type Refstruct struct { ABaz Baz `json:"aBaz"` Disj any `json:"disj"` } - -- out/encoding/typescript/TestGenerate/nilcfg -- export interface Refstruct { aBaz: { diff --git a/testdata/lineage/scalars.txtar b/testdata/lineage/scalars.txtar index 538696a..2d779d9 100644 --- a/testdata/lineage/scalars.txtar +++ b/testdata/lineage/scalars.txtar @@ -730,7 +730,6 @@ type SomeUInt8 = int // StringWithLength defines model for stringWithLength. type StringWithLength = string - -- out/encoding/typescript/TestGenerate/nilcfg -- export interface Scalar-Fields { intWithBounds: number; diff --git a/testdata/lineage/trivial-two-comments.txtar b/testdata/lineage/trivial-two-comments.txtar index 52c1b7d..cab6824 100644 --- a/testdata/lineage/trivial-two-comments.txtar +++ b/testdata/lineage/trivial-two-comments.txtar @@ -280,7 +280,6 @@ type Trivialtwocomments struct { // Secondfield but clearly this one is a great idea Secondfield int32 `json:"secondfield,omitempty"` } - -- out/encoding/typescript/TestGenerate/nilcfg -- export interface Trivial-Two-Comments { /** diff --git a/testdata/lineage/trivial-two.txtar b/testdata/lineage/trivial-two.txtar index 6b7ef85..ca35708 100644 --- a/testdata/lineage/trivial-two.txtar +++ b/testdata/lineage/trivial-two.txtar @@ -269,7 +269,6 @@ type Trivialtwo struct { Firstfield string `json:"firstfield"` Secondfield int32 `json:"secondfield,omitempty"` } - -- out/encoding/typescript/TestGenerate/nilcfg -- export interface Trivial-Two { firstfield: string; diff --git a/testdata/lineage/union-null.txtar b/testdata/lineage/union-null.txtar index 098fc4f..dd5a811 100644 --- a/testdata/lineage/union-null.txtar +++ b/testdata/lineage/union-null.txtar @@ -471,7 +471,6 @@ type Unionnull struct { WithNull *string `json:"withNull"` } `json:"kindString"` } - -- out/encoding/typescript/TestGenerate/nilcfg -- export interface Union-Null { kindFloat: { From 5d7fda1510eea918ac37c22abc9a8279c0546200 Mon Sep 17 00:00:00 2001 From: spinillos Date: Tue, 25 Jul 2023 12:42:56 +0200 Subject: [PATCH 6/8] Fix missing tests --- testdata/lineage/embedexref.txtar | 1 - testdata/lineage/unifyref.txtar | 1 - 2 files changed, 2 deletions(-) diff --git a/testdata/lineage/embedexref.txtar b/testdata/lineage/embedexref.txtar index a7bc620..738da13 100644 --- a/testdata/lineage/embedexref.txtar +++ b/testdata/lineage/embedexref.txtar @@ -190,7 +190,6 @@ Schema count: 1 Schema versions: 0.0 Lenses count: 0 - -- out/encoding/typescript/TestGenerate/nilcfg -- export interface Embedexref { refField1: string; diff --git a/testdata/lineage/unifyref.txtar b/testdata/lineage/unifyref.txtar index ca9dede..9a2eae4 100644 --- a/testdata/lineage/unifyref.txtar +++ b/testdata/lineage/unifyref.txtar @@ -349,7 +349,6 @@ type Unifyref struct { Schema count: 1 Schema versions: 0.0 Lenses count: 0 - -- out/encoding/typescript/TestGenerate/nilcfg -- export interface Unifyref { afoo: { From 2842dbf8e50babd6cdbba433273dc3848d62e9eb Mon Sep 17 00:00:00 2001 From: spinillos Date: Tue, 25 Jul 2023 13:06:33 +0200 Subject: [PATCH 7/8] Another one... --- testdata/lineage/embedexref.txtar | 1 - 1 file changed, 1 deletion(-) diff --git a/testdata/lineage/embedexref.txtar b/testdata/lineage/embedexref.txtar index 738da13..b055eea 100644 --- a/testdata/lineage/embedexref.txtar +++ b/testdata/lineage/embedexref.txtar @@ -189,7 +189,6 @@ type EmbedexrefRefField2 int Schema count: 1 Schema versions: 0.0 Lenses count: 0 - -- out/encoding/typescript/TestGenerate/nilcfg -- export interface Embedexref { refField1: string; From a4034a0a0b2a3028ae98296c7099ea405908748b Mon Sep 17 00:00:00 2001 From: spinillos Date: Wed, 26 Jul 2023 11:18:22 +0200 Subject: [PATCH 8/8] Remove blank line --- testdata/lineage/union.txtar | 1 - 1 file changed, 1 deletion(-) diff --git a/testdata/lineage/union.txtar b/testdata/lineage/union.txtar index 84ba908..ef4c097 100644 --- a/testdata/lineage/union.txtar +++ b/testdata/lineage/union.txtar @@ -594,7 +594,6 @@ lenses: [] Schema count: 1 Schema versions: 0.0 Lenses count: 0 - -- out/encoding/typescript/TestGenerate/nilcfg -- export interface Union { doubleList: Array | '#UnionDef')>>;