1package 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
7const (
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
40type CollectionFormat string
41
42func (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
48type 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.
54type 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
65func (p *Parameter) Data() ParameterData {
66	return *p.data
67}
68
69// Kind returns the parameter type indicator (see const for valid values)
70func (p *Parameter) Kind() int {
71	return p.data.Kind
72}
73
74func (p *Parameter) bePath() *Parameter {
75	p.data.Kind = PathParameterKind
76	return p
77}
78func (p *Parameter) beQuery() *Parameter {
79	p.data.Kind = QueryParameterKind
80	return p
81}
82func (p *Parameter) beBody() *Parameter {
83	p.data.Kind = BodyParameterKind
84	return p
85}
86
87func (p *Parameter) beHeader() *Parameter {
88	p.data.Kind = HeaderParameterKind
89	return p
90}
91
92func (p *Parameter) beForm() *Parameter {
93	p.data.Kind = FormParameterKind
94	return p
95}
96
97// Required sets the required field and returns the receiver
98func (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
104func (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
110func (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
116func (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
122func (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
128func (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
134func (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
140func (p *Parameter) CollectionFormat(format CollectionFormat) *Parameter {
141	p.data.CollectionFormat = format.String()
142	return p
143}
144