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

Structs are not properly embedded inside types/Composition is not handled properly #66

Closed
jtquach1 opened this issue Sep 4, 2024 · 1 comment

Comments

@jtquach1
Copy link

jtquach1 commented Sep 4, 2024

When a struct is embedded inside a type, the generated TypeScript definition for that type produces a key/value pair where the key and value are named after the struct, instead of inlining the fields of the struct inside the type.

Here's an example:

tygo.yaml:

  - path: 'github.com/Drelf2018/req'

Generated TS output:

// Code generated by tygo. DO NOT EDIT.

//////////
// source: client.go

export const Omitempty: string = "omitempty";
export const UserAgent: string = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.54";
export interface Client {
  Client: any /* http.Client */;
  BaseURL?: any /* url.URL */;
  Header: any /* http.Header */;
  /**
   * Client will use the value in Variables when the Field's Value starts with "$"
   */
  Variables: { [key: string]: any};
}

//////////
// source: encode.go

export type Marshaler = any;

//////////
// source: interfaces.go

export type Api = any;
export interface Get {
}
export interface Post {
}
export interface PostForm {
  Post: Post;
}
export interface PostJson {
  Post: Post;
}
export type Adder = any;
export type NamedReader = 
    any /* io.Reader */;
export type Unwrap = any;
export type BeforeRequest = any;

//////////
// source: task.go

export interface Field {
  /**
   * index in Api struct
   */
  Index: number /* int */[];
  /**
   * provided by json tag or parsed from the field name
   */
  Key: string;
  /**
   * default value used when the field value is zero
   */
  Value: string;
  /**
   * This field is ignored when it is zero, conflict with default value
   */
  Omit: boolean;
}
export interface Task {
  Body: Field[];
  Files: Field[];
  Query: Field[];
  Header: Field[];
}
export interface Any {
  Type: any /* unsafe.Pointer */;
  Value: any /* unsafe.Pointer */;
}

My knowledge of Go is beginner level, so apologies if I got something wrong. If I understand correctly, the type PostForm should be empty in terms of its generated fields (it is composed of Post).

@gzuidhof
Copy link
Owner

gzuidhof commented Sep 6, 2024

Hey, I think the default behavior is working as it should.

You can make use of the ,extends tag, see the docs here to get the behavior I think you may be expecting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants