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

Go: impossible to tell which fields are required in a struct #2536

Closed
1 of 5 tasks
eladb opened this issue Feb 4, 2021 · 1 comment · Fixed by #3486
Closed
1 of 5 tasks

Go: impossible to tell which fields are required in a struct #2536

eladb opened this issue Feb 4, 2021 · 1 comment · Fixed by #3486
Assignees
Labels
bug This issue is a bug. effort/medium Medium work item – a couple days of effort language/go Regarding GoLang bindings p2

Comments

@eladb
Copy link
Contributor

eladb commented Feb 4, 2021

🐛 Bug Report

Affected Languages

  • Golang
  • TypeScript or Javascript
  • Python
  • Java
  • .NET (C#, F#, ...)

General Information

  • JSII Version: 1.20.0
  • Platform:

What is the problem?

When generating Go structs from jsii data types, there is currently no indication (let alone compile-type check) to tell users which fields in the struct are required and which are optional:

export interface MyDataType {
  readonly thisIsRequired: string;

  /** @default 10 */
  readonly thisIsOptional?: number;
}

Go output:

// This is my data type.
// Struct proxy
type MyDataType struct {
	// This is a required textual field.
	RequiredField string `json:"requiredField"`
	// This is an optional numeric field.
	OptionalField float64 `json:"optionalField"`
}

What are our options in Go to address this? At the minimum, we should obviously generate some documentation that indicates which fields are required and what are the default values. Is there a way to get type-checking help?

Addendum:

This issue tracks adding doc comments indicating which fields are required vs optional to ensure that this is available during autocompletion. IE, when a user starts typing a property name, the documentation for that property will be displayed in their IDE telling them whether the field is optional or required.

There is an additional ticket to panic at runtime when required fields are nil #2672

@eladb eladb added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Feb 4, 2021
@MrArnoldPalmer MrArnoldPalmer added effort/medium Medium work item – a couple days of effort p2 and removed needs-triage This issue or PR still needs to be triaged. labels Feb 16, 2021
@MrArnoldPalmer MrArnoldPalmer removed their assignment Mar 30, 2021
@MrArnoldPalmer MrArnoldPalmer added the language/go Regarding GoLang bindings label Mar 30, 2021
RomainMuller added a commit that referenced this issue Apr 11, 2022
Adds a `required` or `optional` tag on struct fields to denote whether
they are required or optional (obviously). This is a cheap-ass way to
provide documentation visibility to users.

This isn't perfect as ideally, one would want compile-time enforcement
of these, however this isn't currently posisble with Go (we explored
several options, including via the use of generics, but it did not end
up with any particularly improved solution).

Fixes #2536
@RomainMuller RomainMuller self-assigned this Apr 11, 2022
RomainMuller added a commit that referenced this issue Apr 25, 2022
Adds a `field:"required"` or `field:"optional"` tag on struct fields to
denote whether they are required or optional (obviously). This is a
cheap-ass way to provide documentation visibility to users.

This isn't perfect as ideally, one would want compile-time enforcement
of these, however this isn't currently posisble with Go (we explored
several options, including via the use of generics, but it did not end
up with any particularly improved solution).

This does however include runtime enforcement with a dedicated error
message.

Fixes #2536
Fixes #2672
@github-actions
Copy link
Contributor

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. effort/medium Medium work item – a couple days of effort language/go Regarding GoLang bindings p2
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants