From 6113dbacbcc7764c7909c733b8cb835028c8e523 Mon Sep 17 00:00:00 2001 From: merlinfuchs Date: Wed, 19 Feb 2025 11:06:30 +0100 Subject: [PATCH 1/3] Add support for new struct tag option "omitzero" --- tygo/write.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tygo/write.go b/tygo/write.go index 3caf4f2..a002eef 100644 --- a/tygo/write.go +++ b/tygo/write.go @@ -320,7 +320,7 @@ func (g *PackageGenerator) writeStructFields(s *strings.Builder, fields []*ast.F continue } - optional = jsonTag.HasOption("omitempty") + optional = jsonTag.HasOption("omitempty") || jsonTag.HasOption("omitzero") } yamlTag, err := tags.Get("yaml") if err == nil { From 77bf91fcf8f58eed05dd17e6aa5523b090bff43d Mon Sep 17 00:00:00 2001 From: merlinfuchs Date: Wed, 19 Feb 2025 11:22:42 +0100 Subject: [PATCH 2/3] add examples --- README.md | 21 ++++++++++----------- examples/simple/index.ts | 1 + examples/simple/simple.go | 13 +++++++++---- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 8af9711..1c9b542 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ type UserEntry struct { MaybeFieldWithStar *string `json:"address"` Nickname string `json:"nickname,omitempty"` Role UserRole `json:"role"` + CreatedAt time.Time `json:"created_at,omitzero"` Complex ComplexType `json:"complex"` unexported bool // Unexported fields are omitted @@ -85,6 +86,7 @@ export interface UserEntry { address?: string; nickname?: string; role: UserRole; + createdAt?: string /* RFC3339 */; complex: ComplexType; } export interface ListUsersResponse { @@ -200,10 +202,11 @@ You could use the `frontmatter` field in the config to inject `export type Genre **`tygo:emit` directive** -Another way to generate types that cannot be directly represented in Go is to use a `//tygo:emit` directive to +Another way to generate types that cannot be directly represented in Go is to use a `//tygo:emit` directive to directly emit literal TS code. -The directive can be used in two ways. A `tygo:emit` directive on a struct will emit the remainder of the directive +The directive can be used in two ways. A `tygo:emit` directive on a struct will emit the remainder of the directive text before the struct. + ```golang // Golang input @@ -215,7 +218,7 @@ type Book struct { ``` ```typescript -export type Genre = "novel" | "crime" | "fantasy" +export type Genre = "novel" | "crime" | "fantasy"; export interface Book { title: string; @@ -224,11 +227,12 @@ export interface Book { ``` A `//tygo:emit` directive on a string var will emit the contents of the var, useful for multi-line content. + ```golang //tygo:emit var _ = `export type StructAsTuple=[ - a:number, - b:number, + a:number, + b:number, c:string, ] ` @@ -238,16 +242,11 @@ type CustomMarshalled struct { ``` ```typescript -export type StructAsTuple=[ - a:number, - b:number, - c:string, -] +export type StructAsTuple = [a: number, b: number, c: string]; export interface CustomMarshalled { content: StructAsTuple[]; } - ``` Generating types this way is particularly useful for tuple types, because a comma cannot be used in the `tstype` tag. diff --git a/examples/simple/index.ts b/examples/simple/index.ts index 0e291aa..248620c 100755 --- a/examples/simple/index.ts +++ b/examples/simple/index.ts @@ -27,6 +27,7 @@ export interface UserEntry { address?: string; nickname?: string; role: UserRole; + createdAt?: string /* RFC3339 */; complex: ComplexType; } export interface ListUsersResponse { diff --git a/examples/simple/simple.go b/examples/simple/simple.go index 03de5b0..1158ab8 100644 --- a/examples/simple/simple.go +++ b/examples/simple/simple.go @@ -1,6 +1,10 @@ package simple -import "github.com/google/uuid" +import ( + "time" + + "github.com/google/uuid" +) // Comments are kept :) type ComplexType map[string]map[uint16]*uint32 @@ -24,9 +28,10 @@ type UserEntry struct { Bar uuid.UUID `json:"bar"` } `json:"prefs"` - MaybeFieldWithStar *string `json:"address"` - Nickname string `json:"nickname,omitempty"` - Role UserRole `json:"role"` + MaybeFieldWithStar *string `json:"address"` + Nickname string `json:"nickname,omitempty"` + Role UserRole `json:"role"` + CreatedAt time.Time `json:"created_at,omitzero"` Complex ComplexType `json:"complex"` unexported bool // Unexported fields won't be in the output From 3e5e5f220c289458fdc99931446501b5c0b62c25 Mon Sep 17 00:00:00 2001 From: merlinfuchs Date: Wed, 19 Feb 2025 11:24:16 +0100 Subject: [PATCH 3/3] minor --- README.md | 21 ++++++++++++--------- examples/simple/index.ts | 2 +- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 1c9b542..ad8e875 100644 --- a/README.md +++ b/README.md @@ -86,7 +86,7 @@ export interface UserEntry { address?: string; nickname?: string; role: UserRole; - createdAt?: string /* RFC3339 */; + created_at?: string /* RFC3339 */; complex: ComplexType; } export interface ListUsersResponse { @@ -202,11 +202,10 @@ You could use the `frontmatter` field in the config to inject `export type Genre **`tygo:emit` directive** -Another way to generate types that cannot be directly represented in Go is to use a `//tygo:emit` directive to +Another way to generate types that cannot be directly represented in Go is to use a `//tygo:emit` directive to directly emit literal TS code. -The directive can be used in two ways. A `tygo:emit` directive on a struct will emit the remainder of the directive +The directive can be used in two ways. A `tygo:emit` directive on a struct will emit the remainder of the directive text before the struct. - ```golang // Golang input @@ -218,7 +217,7 @@ type Book struct { ``` ```typescript -export type Genre = "novel" | "crime" | "fantasy"; +export type Genre = "novel" | "crime" | "fantasy" export interface Book { title: string; @@ -227,12 +226,11 @@ export interface Book { ``` A `//tygo:emit` directive on a string var will emit the contents of the var, useful for multi-line content. - ```golang //tygo:emit var _ = `export type StructAsTuple=[ - a:number, - b:number, + a:number, + b:number, c:string, ] ` @@ -242,11 +240,16 @@ type CustomMarshalled struct { ``` ```typescript -export type StructAsTuple = [a: number, b: number, c: string]; +export type StructAsTuple=[ + a:number, + b:number, + c:string, +] export interface CustomMarshalled { content: StructAsTuple[]; } + ``` Generating types this way is particularly useful for tuple types, because a comma cannot be used in the `tstype` tag. diff --git a/examples/simple/index.ts b/examples/simple/index.ts index 248620c..90e57a6 100755 --- a/examples/simple/index.ts +++ b/examples/simple/index.ts @@ -27,7 +27,7 @@ export interface UserEntry { address?: string; nickname?: string; role: UserRole; - createdAt?: string /* RFC3339 */; + created_at?: string /* RFC3339 */; complex: ComplexType; } export interface ListUsersResponse {