Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame^] | 1 | package restful |
| 2 | |
| 3 | // Copyright 2013 Ernest Micklei. All rights reserved. |
| 4 | // Use of this source code is governed by a license |
| 5 | // that can be found in the LICENSE file. |
| 6 | |
| 7 | const ( |
| 8 | // PathParameterKind = indicator of Request parameter type "path" |
| 9 | PathParameterKind = iota |
| 10 | |
| 11 | // QueryParameterKind = indicator of Request parameter type "query" |
| 12 | QueryParameterKind |
| 13 | |
| 14 | // BodyParameterKind = indicator of Request parameter type "body" |
| 15 | BodyParameterKind |
| 16 | |
| 17 | // HeaderParameterKind = indicator of Request parameter type "header" |
| 18 | HeaderParameterKind |
| 19 | |
| 20 | // FormParameterKind = indicator of Request parameter type "form" |
| 21 | FormParameterKind |
| 22 | |
| 23 | // CollectionFormatCSV comma separated values `foo,bar` |
| 24 | CollectionFormatCSV = CollectionFormat("csv") |
| 25 | |
| 26 | // CollectionFormatSSV space separated values `foo bar` |
| 27 | CollectionFormatSSV = CollectionFormat("ssv") |
| 28 | |
| 29 | // CollectionFormatTSV tab separated values `foo\tbar` |
| 30 | CollectionFormatTSV = CollectionFormat("tsv") |
| 31 | |
| 32 | // CollectionFormatPipes pipe separated values `foo|bar` |
| 33 | CollectionFormatPipes = CollectionFormat("pipes") |
| 34 | |
| 35 | // CollectionFormatMulti corresponds to multiple parameter instances instead of multiple values for a single |
| 36 | // instance `foo=bar&foo=baz`. This is valid only for QueryParameters and FormParameters |
| 37 | CollectionFormatMulti = CollectionFormat("multi") |
| 38 | ) |
| 39 | |
| 40 | type CollectionFormat string |
| 41 | |
| 42 | func (cf CollectionFormat) String() string { |
| 43 | return string(cf) |
| 44 | } |
| 45 | |
| 46 | // Parameter is for documententing the parameter used in a Http Request |
| 47 | // ParameterData kinds are Path,Query and Body |
| 48 | type Parameter struct { |
| 49 | data *ParameterData |
| 50 | } |
| 51 | |
| 52 | // ParameterData represents the state of a Parameter. |
| 53 | // It is made public to make it accessible to e.g. the Swagger package. |
| 54 | type ParameterData struct { |
| 55 | Name, Description, DataType, DataFormat string |
| 56 | Kind int |
| 57 | Required bool |
| 58 | AllowableValues map[string]string |
| 59 | AllowMultiple bool |
| 60 | DefaultValue string |
| 61 | CollectionFormat string |
| 62 | } |
| 63 | |
| 64 | // Data returns the state of the Parameter |
| 65 | func (p *Parameter) Data() ParameterData { |
| 66 | return *p.data |
| 67 | } |
| 68 | |
| 69 | // Kind returns the parameter type indicator (see const for valid values) |
| 70 | func (p *Parameter) Kind() int { |
| 71 | return p.data.Kind |
| 72 | } |
| 73 | |
| 74 | func (p *Parameter) bePath() *Parameter { |
| 75 | p.data.Kind = PathParameterKind |
| 76 | return p |
| 77 | } |
| 78 | func (p *Parameter) beQuery() *Parameter { |
| 79 | p.data.Kind = QueryParameterKind |
| 80 | return p |
| 81 | } |
| 82 | func (p *Parameter) beBody() *Parameter { |
| 83 | p.data.Kind = BodyParameterKind |
| 84 | return p |
| 85 | } |
| 86 | |
| 87 | func (p *Parameter) beHeader() *Parameter { |
| 88 | p.data.Kind = HeaderParameterKind |
| 89 | return p |
| 90 | } |
| 91 | |
| 92 | func (p *Parameter) beForm() *Parameter { |
| 93 | p.data.Kind = FormParameterKind |
| 94 | return p |
| 95 | } |
| 96 | |
| 97 | // Required sets the required field and returns the receiver |
| 98 | func (p *Parameter) Required(required bool) *Parameter { |
| 99 | p.data.Required = required |
| 100 | return p |
| 101 | } |
| 102 | |
| 103 | // AllowMultiple sets the allowMultiple field and returns the receiver |
| 104 | func (p *Parameter) AllowMultiple(multiple bool) *Parameter { |
| 105 | p.data.AllowMultiple = multiple |
| 106 | return p |
| 107 | } |
| 108 | |
| 109 | // AllowableValues sets the allowableValues field and returns the receiver |
| 110 | func (p *Parameter) AllowableValues(values map[string]string) *Parameter { |
| 111 | p.data.AllowableValues = values |
| 112 | return p |
| 113 | } |
| 114 | |
| 115 | // DataType sets the dataType field and returns the receiver |
| 116 | func (p *Parameter) DataType(typeName string) *Parameter { |
| 117 | p.data.DataType = typeName |
| 118 | return p |
| 119 | } |
| 120 | |
| 121 | // DataFormat sets the dataFormat field for Swagger UI |
| 122 | func (p *Parameter) DataFormat(formatName string) *Parameter { |
| 123 | p.data.DataFormat = formatName |
| 124 | return p |
| 125 | } |
| 126 | |
| 127 | // DefaultValue sets the default value field and returns the receiver |
| 128 | func (p *Parameter) DefaultValue(stringRepresentation string) *Parameter { |
| 129 | p.data.DefaultValue = stringRepresentation |
| 130 | return p |
| 131 | } |
| 132 | |
| 133 | // Description sets the description value field and returns the receiver |
| 134 | func (p *Parameter) Description(doc string) *Parameter { |
| 135 | p.data.Description = doc |
| 136 | return p |
| 137 | } |
| 138 | |
| 139 | // CollectionFormat sets the collection format for an array type |
| 140 | func (p *Parameter) CollectionFormat(format CollectionFormat) *Parameter { |
| 141 | p.data.CollectionFormat = format.String() |
| 142 | return p |
| 143 | } |