|
| 1 | +--- |
| 2 | +title: Enumerations |
| 3 | +weight: 12 |
| 4 | +description: | |
| 5 | + Publish a Go const block as a spec enum — any constant expression, the type |
| 6 | + and format taken from the declaration, and the same members inline on |
| 7 | + parameters and headers. |
| 8 | +--- |
| 9 | + |
| 10 | +An enum in Go is a named type plus a block of constants. `swagger:enum` turns |
| 11 | +that pair into an `enum` array on every schema, parameter and header the type |
| 12 | +reaches. This page covers what the scanner accepts on the value side, what |
| 13 | +decides the emitted `type` / `format`, and the two shapes that do not work. |
| 14 | + |
| 15 | +Every Go snippet below comes from the test-covered |
| 16 | +[`docs/examples/concepts/enums`](https://github.com/go-openapi/codescan/tree/master/docs/examples/concepts/enums) |
| 17 | +package, and every JSON pane is a golden file a test regenerates. |
| 18 | + |
| 19 | +## swagger:enum |
| 20 | + |
| 21 | +`swagger:enum <name>` collects the `const` values declared with that type. A |
| 22 | +bare `swagger:enum` on the type declaration works too — the name is inferred |
| 23 | +from the declaration it sits on. |
| 24 | + |
| 25 | +The enum type is emitted **because something points at it**: a model field, a |
| 26 | +parameter, a header. On its own it is unreachable, and unreachable types are not |
| 27 | +published. Add `swagger:model` to the enum type to make it a first-class |
| 28 | +definition (carrying the `enum` array) that fields `$ref` instead — the general |
| 29 | +`swagger:model ⇒ definition + $ref` rule. |
| 30 | + |
| 31 | +Each member's doc comment becomes a line of the `x-go-enum-desc` extension, and |
| 32 | +is appended to the property description. Set |
| 33 | +[`SkipEnumDescriptions`]({{% relref "/maintainers/options" %}}) to keep the |
| 34 | +mapping on the extension only. |
| 35 | + |
| 36 | +## Any constant expression, not just literals |
| 37 | + |
| 38 | +The values come from the Go **type-checker**, which has already evaluated the |
| 39 | +const block. So the members do not have to be written as literals — anything the |
| 40 | +compiler can fold is collected. |
| 41 | + |
| 42 | +`iota` is the case that matters most, because after the first line there is |
| 43 | +nothing left in the source to read: the following specs carry neither a type nor |
| 44 | +a value, and inherit both implicitly. |
| 45 | + |
| 46 | +{{< example go="concepts/enums/enums.go" goregion="iota" |
| 47 | + json="concepts/enums/testdata/iota.json" jsonlabel="#/definitions/Schedule" >}} |
| 48 | + |
| 49 | +Constant expressions and references to earlier members are collected the same |
| 50 | +way. |
| 51 | + |
| 52 | +{{< example go="concepts/enums/enums.go" goregion="expressions" |
| 53 | + json="concepts/enums/testdata/expressions.json" jsonlabel="#/definitions/Threshold" >}} |
| 54 | + |
| 55 | +The same goes for every other constant form Go offers: negative values, the |
| 56 | +non-decimal bases (`0x2a`, `0b101010`, `0o52`) and digit separators, values |
| 57 | +above `MaxInt64` in an unsigned enum, `true` / `false`, rune literals, and both |
| 58 | +the raw and the escaped string forms. |
| 59 | + |
| 60 | +Negative members are worth a pane of their own, since a signed constant is not a |
| 61 | +literal in the Go grammar — it is an expression wrapping one: |
| 62 | + |
| 63 | +{{< example go="concepts/enums/enums.go" goregion="signed" |
| 64 | + json="concepts/enums/testdata/signed.json" jsonlabel="#/definitions/Camera" >}} |
| 65 | + |
| 66 | +## The type comes from the declaration |
| 67 | + |
| 68 | +`type` and `format` are read from the Go type you declared, never from the |
| 69 | +members. `PanDirection` above is an `int8`, so the property is |
| 70 | +`{integer, int8}` — even though every member would fit in a smaller or larger |
| 71 | +box. |
| 72 | + |
| 73 | +This is also what makes the const block **safe to reorder**. `Zoom` is a |
| 74 | +`float32` whose first member is written `0`, an integer literal; the schema is a |
| 75 | +number enum regardless of which member comes first: |
| 76 | + |
| 77 | +{{< example go="concepts/enums/enums.go" goregion="width" |
| 78 | + json="concepts/enums/testdata/width.json" jsonlabel="#/definitions/Lens" >}} |
| 79 | + |
| 80 | +A type declared over another **named** type keeps what that type contributed. An |
| 81 | +enum written over a string format is still that format: |
| 82 | + |
| 83 | +{{< example go="concepts/enums/enums.go" goregion="strfmt" |
| 84 | + json="concepts/enums/testdata/strfmt.json" jsonlabel="#/definitions/Label" >}} |
| 85 | + |
| 86 | +## Parameters and headers |
| 87 | + |
| 88 | +OpenAPI 2.0 forbids a `$ref` on a non-body parameter or a response header, so |
| 89 | +there the members and the format are written **inline**. Nothing changes on the |
| 90 | +annotation side — the same enum type reaches `in: query`, `path`, `header` and |
| 91 | +`formData`, and the `items` of an array-typed one: |
| 92 | + |
| 93 | +{{< example go="concepts/enums/enums.go" goregion="params" |
| 94 | + json="concepts/enums/testdata/params.json" jsonlabel="GET /cameras/search — parameters" |
| 95 | + full="concepts/enums/testdata/full.json" >}} |
| 96 | + |
| 97 | +## Two shapes that do not work |
| 98 | + |
| 99 | +**A `rune` or `byte` enum emits integers.** It is collected like any other, and |
| 100 | +`'a'` reaches the spec as `97`: |
| 101 | + |
| 102 | +{{< example go="concepts/enums/enums.go" goregion="runes" |
| 103 | + json="concepts/enums/testdata/runes.json" jsonlabel="#/definitions/Glyph" >}} |
| 104 | + |
| 105 | +That is unlikely to be what you pictured, and it is the only faithful answer: a |
| 106 | +scalar `rune` is an `int32` on the wire as much as in Go, so `json.Marshal` |
| 107 | +writes `97`, and `encoding/json` **refuses** to unmarshal `"a"` back into the |
| 108 | +field. A string-typed schema would describe a payload your own server rejects. |
| 109 | +If you meant characters, declare the type over `string` |
| 110 | +(`type Letter string`, `LetterA Letter = "a"`) — that changes the wire, and the |
| 111 | +schema follows. |
| 112 | + |
| 113 | +**An alias to a basic type cannot host an enum.** |
| 114 | + |
| 115 | +```go |
| 116 | +type Unsigned = uint64 // an alias, not a new type |
| 117 | + |
| 118 | +// swagger:enum Unsigned // ← collects nothing |
| 119 | +const Zero Unsigned = 0 |
| 120 | +``` |
| 121 | + |
| 122 | +The Go type-checker erases the alias, so `Zero` is indistinguishable from any |
| 123 | +other `uint64` constant and there is no set of members to collect. Declare a |
| 124 | +real type instead (`type Unsigned uint64`). An alias to a *named* enum type is |
| 125 | +fine — the named type survives. |
| 126 | + |
| 127 | +## Where to go next |
| 128 | + |
| 129 | +- [Validations]({{% relref "/tutorials/validations" %}}) — the other constraints |
| 130 | + a property can carry, and the reduced surface parameters accept. |
| 131 | +- [Model definitions]({{% relref "/tutorials/model-definitions" %}}) — the rest |
| 132 | + of the per-type annotations. |
| 133 | +- [`swagger:enum` reference]({{% relref "/maintainers/annotations/swagger-enum" %}}) |
| 134 | + — the exhaustive rule. |
0 commit comments