glideapps / glideapps/quicktype

Hard to use optional Enum fields in Go

Open
#1,512 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
13.9k
Forks
1.2k
Avg merge
8h 53m
Merged PRs (30d)
369

Description

example: (Sarif JSON Schema -> Go) https://app.quicktype.io?share=fg425OVcEDDUTMsa0BpG
Go playground: https://play.golang.org/p/eiVmYTRSKY8

```go
package main

// Specifies the failure level for the report.
//
// A value specifying the severity level of the notification.
//
// A value specifying the severity level of the result.
type Level string

const (
Error Level = "error"
None Level = "none"
Note Level = "note"
Warning Level = "warning"
)

// A result produced by an analysis tool.
type Result struct {
// ...
Level *Level `json:"level,omitempty"` // A value specifying the severity level of the result.
// ...
}

func main() {
println(Result{Level: &Error}) // ./prog.go:25:24: cannot take the address of Error
}
```

quicktype defines enum type/values with const values, which is good. However, if a struct has the enum field as optional (not-required), the required type will be pointer to the enum type (example: `*Level`).
Since we cannot get the reference to the const value in Go, we cannot use defined `Level` value such as `Error` to fill the struct (example: `Result`).

Are there any workaround for this? Thanks in advance!

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.