Skip to content

Commit 0cc9aea

Browse files
committed
Update README and enum tests for enum variant name override feature.
1 parent 3ccadea commit 0cc9aea

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

README.md

+30
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,36 @@ Make it OR condition
900900
// @Security OAuth2Application[write, admin] || APIKeyAuth
901901
```
902902
903+
### Generate enum types from enum constants
904+
905+
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.
906+
907+
```go
908+
type Difficulty string
909+
910+
const (
911+
Easy Difficulty = "easy" // You can add a comment to the enum variant.
912+
Medium Difficulty = "medium" // @name MediumDifficulty
913+
Hard Difficulty = "hard" // @name HardDifficulty You can have a name override and a comment.
914+
)
915+
916+
type Class int
917+
918+
const (
919+
First Class = iota // @name FirstClass
920+
Second // Name override and comment rules apply here just as above.
921+
Third // @name ThirdClass This one has a name override and a comment.
922+
)
923+
924+
// There is no need to add `enums:"..."` to the fields, it is automatically generated from the ordered consts.
925+
type Quiz struct {
926+
Difficulty Difficulty
927+
Class Class
928+
Questions []string
929+
Answers []string
930+
}
931+
```
932+
903933
904934
### Add a description for enum items
905935

enums_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ func TestParseGlobalEnums(t *testing.T) {
1717
p := New()
1818
err = p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth)
1919
assert.NoError(t, err)
20+
2021
b, err := json.MarshalIndent(p.swagger, "", " ")
2122
assert.NoError(t, err)
2223
assert.Equal(t, string(expected), string(b))
24+
2325
constsPath := "github.com/swaggo/swag/testdata/enums/consts"
2426
assert.Equal(t, 64, p.packages.packages[constsPath].ConstTable["uintSize"].Value)
2527
assert.Equal(t, int32(62), p.packages.packages[constsPath].ConstTable["maxBase"].Value)
@@ -30,4 +32,13 @@ func TestParseGlobalEnums(t *testing.T) {
3032
assert.Equal(t, "aa\nbb\u8888cc", p.packages.packages[constsPath].ConstTable["escapestr"].Value)
3133
assert.Equal(t, 1_000_000, p.packages.packages[constsPath].ConstTable["underscored"].Value)
3234
assert.Equal(t, 0b10001000, p.packages.packages[constsPath].ConstTable["binaryInteger"].Value)
35+
36+
typesPath := "github.com/swaggo/swag/testdata/enums/types"
37+
difficultyEnums := p.packages.packages[typesPath].TypeDefinitions["Difficulty"].Enums
38+
assert.Equal(t, "Easy", difficultyEnums[0].key)
39+
assert.Equal(t, "", difficultyEnums[0].Comment)
40+
assert.Equal(t, "Medium", difficultyEnums[1].key)
41+
assert.Equal(t, "This one also has a comment", difficultyEnums[1].Comment)
42+
assert.Equal(t, "DifficultyHard", difficultyEnums[2].key)
43+
assert.Equal(t, "This means really hard", difficultyEnums[2].Comment)
3344
}

testdata/enums/types/model.go

+8
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,11 @@ type PersonWithArrayEnum struct {
6363
Mask []Mask
6464
Type Type
6565
}
66+
67+
type Difficulty string
68+
69+
const (
70+
DifficultyEasy Difficulty = "easy" // @name Easy
71+
DifficultyMedium Difficulty = "medium" // @Name Medium This one also has a comment
72+
DifficultyHard Difficulty = "hard" // This means really hard
73+
)

0 commit comments

Comments
 (0)