Skip to content

Commit

Permalink
Update README and enum tests for enum variant name override feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
drewsilcock committed Mar 28, 2024
1 parent 3ccadea commit 0cc9aea
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,36 @@ Make it OR condition
// @Security OAuth2Application[write, admin] || APIKeyAuth
```
### Generate enum types from enum constants
You can generate enums from ordered constants. Each enum variant can have a comment, an override name, or both. This works with both iota-defined and manually defined constants.
```go
type Difficulty string
const (
Easy Difficulty = "easy" // You can add a comment to the enum variant.
Medium Difficulty = "medium" // @name MediumDifficulty
Hard Difficulty = "hard" // @name HardDifficulty You can have a name override and a comment.
)
type Class int
const (
First Class = iota // @name FirstClass
Second // Name override and comment rules apply here just as above.
Third // @name ThirdClass This one has a name override and a comment.
)
// There is no need to add `enums:"..."` to the fields, it is automatically generated from the ordered consts.
type Quiz struct {
Difficulty Difficulty
Class Class
Questions []string
Answers []string
}
```
### Add a description for enum items
Expand Down
11 changes: 11 additions & 0 deletions enums_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ func TestParseGlobalEnums(t *testing.T) {
p := New()
err = p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth)
assert.NoError(t, err)

b, err := json.MarshalIndent(p.swagger, "", " ")
assert.NoError(t, err)
assert.Equal(t, string(expected), string(b))

constsPath := "github.com/swaggo/swag/testdata/enums/consts"
assert.Equal(t, 64, p.packages.packages[constsPath].ConstTable["uintSize"].Value)
assert.Equal(t, int32(62), p.packages.packages[constsPath].ConstTable["maxBase"].Value)
Expand All @@ -30,4 +32,13 @@ func TestParseGlobalEnums(t *testing.T) {
assert.Equal(t, "aa\nbb\u8888cc", p.packages.packages[constsPath].ConstTable["escapestr"].Value)
assert.Equal(t, 1_000_000, p.packages.packages[constsPath].ConstTable["underscored"].Value)
assert.Equal(t, 0b10001000, p.packages.packages[constsPath].ConstTable["binaryInteger"].Value)

typesPath := "github.com/swaggo/swag/testdata/enums/types"
difficultyEnums := p.packages.packages[typesPath].TypeDefinitions["Difficulty"].Enums
assert.Equal(t, "Easy", difficultyEnums[0].key)
assert.Equal(t, "", difficultyEnums[0].Comment)
assert.Equal(t, "Medium", difficultyEnums[1].key)
assert.Equal(t, "This one also has a comment", difficultyEnums[1].Comment)
assert.Equal(t, "DifficultyHard", difficultyEnums[2].key)
assert.Equal(t, "This means really hard", difficultyEnums[2].Comment)
}
8 changes: 8 additions & 0 deletions testdata/enums/types/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,11 @@ type PersonWithArrayEnum struct {
Mask []Mask
Type Type
}

type Difficulty string

const (
DifficultyEasy Difficulty = "easy" // @name Easy
DifficultyMedium Difficulty = "medium" // @Name Medium This one also has a comment
DifficultyHard Difficulty = "hard" // This means really hard
)

0 comments on commit 0cc9aea

Please sign in to comment.