-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
1,081 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Swagger 2.0 | ||
|
||
Loose implementation of [Swagger 2.0 specification](https://swagger.io/specification/v2/) for the needs of specifically polyform. This does not aim to be a full fledged implementation, so be cautious before considering using anything here for personal purposes. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package swagger | ||
|
||
const Vector2DefinitionName = "Vector2" | ||
const Vector3DefinitionName = "Vector3" | ||
const Vector4DefinitionName = "Vector4" | ||
const AABBDefinitionName = "AABB" | ||
|
||
var vectorComponent = Property{ | ||
Type: NumberPropertyType, | ||
Format: DoublePropertyFormat, | ||
Example: "1.0", | ||
} | ||
|
||
var Vector2Definition = Definition{ | ||
Type: "object", | ||
Properties: map[string]Property{ | ||
"x": vectorComponent, | ||
"y": vectorComponent, | ||
}, | ||
} | ||
|
||
var Vector3Definition = Definition{ | ||
Type: "object", | ||
Properties: map[string]Property{ | ||
"x": vectorComponent, | ||
"y": vectorComponent, | ||
"z": vectorComponent, | ||
}, | ||
} | ||
|
||
var Vector4Definition = Definition{ | ||
Type: "object", | ||
Properties: map[string]Property{ | ||
"x": vectorComponent, | ||
"y": vectorComponent, | ||
"z": vectorComponent, | ||
"w": vectorComponent, | ||
}, | ||
} | ||
|
||
var AABBDefinition = Definition{ | ||
Type: "object", | ||
Properties: map[string]Property{ | ||
"min": { | ||
Ref: DefinitionRefPath(Vector3DefinitionName), | ||
}, | ||
"max": { | ||
Ref: DefinitionRefPath(Vector3DefinitionName), | ||
}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package swagger | ||
|
||
type ParameterLocation string | ||
|
||
const ( | ||
PathParameterLocation ParameterLocation = "path" | ||
QueryParameterLocation ParameterLocation = "query" | ||
HeaderParameterLocation ParameterLocation = "header" | ||
BodyParameterLocation ParameterLocation = "body" | ||
FormParameterLocation ParameterLocation = "formData" | ||
) | ||
|
||
type Parameter struct { | ||
// Required. The location of the parameter. Possible values are "query", | ||
// "header", "path", "formData" or "body". | ||
In ParameterLocation `json:"in"` | ||
|
||
// Required. The name of the parameter. Parameter names are case sensitive. | ||
// * If in is "path", the name field MUST correspond to the associated | ||
// path segment from the path field in the Paths Object. See Path | ||
// Templating for further information. | ||
// * For all other cases, the name corresponds to the parameter name used | ||
// based on the in property | ||
Name string `json:"name,omitempty"` | ||
|
||
// A brief description of the parameter. This could contain examples of | ||
// use. GFM syntax can be used for rich text representation. | ||
Description string `json:"description,omitempty"` | ||
|
||
// Determines whether this parameter is mandatory. If the parameter is in | ||
// "path", this property is required and its value MUST be true. Otherwise, | ||
// the property MAY be included and its default value is false. | ||
Required bool `json:"required,omitempty"` | ||
|
||
// If in is "body": Required. The schema defining the type used for the | ||
// body parameter. | ||
Schema any `json:"schema,omitempty"` | ||
|
||
// If in is any value other than "body" | ||
// Required. The type of the parameter. Since the parameter is not located | ||
// at the request body, it is limited to simple types (that is, not an | ||
// object). The value MUST be one of "string", "number", "integer", | ||
// "boolean", "array" or "file". If type is "file", the consumes MUST be | ||
// either "multipart/form-data", " application/x-www-form-urlencoded" or | ||
// both and the parameter MUST be in "formData". | ||
Type string `json:"type,omitempty"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package swagger | ||
|
||
type PropertyType string | ||
|
||
const ( | ||
ObjectPropertyType PropertyType = "object" | ||
StringPropertyType PropertyType = "string" | ||
IntegerPropertyType PropertyType = "integer" | ||
NumberPropertyType PropertyType = "number" | ||
BooleanPropertyType PropertyType = "boolean" | ||
ArrayPropertyType PropertyType = "array" | ||
) | ||
|
||
type PropertyFormat string | ||
|
||
const ( | ||
// Integer ================================================================ | ||
|
||
Int32PropertyFormat PropertyFormat = "int32" | ||
Int64PropertyFormat PropertyFormat = "int64" | ||
|
||
// Number ================================================================= | ||
|
||
FloatPropertyFormat PropertyFormat = "float" | ||
DoublePropertyFormat PropertyFormat = "double" | ||
|
||
// String ================================================================= | ||
|
||
// Base64-encoded characters, for example, U3dhZ2dlciByb2Nrcw== | ||
BytePropertyFormat PropertyFormat = "byte" | ||
|
||
// Binary data, used to describe files | ||
BinaryPropertyFormat PropertyFormat = "binary" | ||
|
||
// Full-date notation as defined by RFC 3339, section 5.6, for example, | ||
// 2017-07-21 | ||
DatePropertyFormat PropertyFormat = "date" | ||
|
||
// The date-time notation as defined by RFC 3339, section 5.6, for example, | ||
// 2017-07-21T17:32:28Z | ||
DateTimePropertyFormat PropertyFormat = "date-time" | ||
|
||
// A hint to UIs to mask the input | ||
PasswordPropertyFormat PropertyFormat = "password" | ||
) | ||
|
||
type Property struct { | ||
Type PropertyType `json:"type,omitempty"` | ||
|
||
// An optional format modifier serves as a hint at the contents and format | ||
// of the string | ||
Format PropertyFormat `json:"format,omitempty"` | ||
|
||
Example any `json:"example,omitempty"` | ||
Ref any `json:"$ref,omitempty"` | ||
Description any `json:"description,omitempty"` | ||
Items any `json:"items,omitempty"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package swagger | ||
|
||
import "errors" | ||
|
||
func DefinitionRefPath(r string) string { | ||
if r == "" { | ||
panic(errors.New("can not build definition reference from an empty string")) | ||
} | ||
return "#/definitions/" + r | ||
} | ||
|
||
type SchemaObject struct { | ||
Ref string `json:"$ref,omitempty"` | ||
} | ||
|
||
type RequestMethod string | ||
|
||
const ( | ||
GetRequestMethod RequestMethod = "get" | ||
PostRequestMethod RequestMethod = "post" | ||
) | ||
|
||
type Info struct { | ||
Title string `json:"title"` | ||
Description string `json:"description,omitempty"` | ||
Version string `json:"version,omitempty"` | ||
} | ||
|
||
type Response struct { | ||
Description string `json:"description,omitempty"` | ||
// This is a lookup into the responses section, not definitions | ||
Ref string `json:"$ref,omitempty"` | ||
Schema any `json:"schema,omitempty"` | ||
} | ||
|
||
type RequestDefinition struct { | ||
Summary string `json:"summary"` | ||
Description string `json:"description"` | ||
Produces []string `json:"produces"` | ||
Consumes []string `json:"consumes"` | ||
Responses map[int]Response `json:"responses"` | ||
Parameters []Parameter `json:"parameters"` | ||
} | ||
|
||
type Path map[RequestMethod]RequestDefinition | ||
|
||
type Definition struct { | ||
// Type is probably always "object" | ||
Type string `json:"type"` | ||
Properties map[string]Property `json:"properties"` | ||
Required []string `json:"required,omitempty"` | ||
Example any `json:"example,omitempty"` | ||
} | ||
|
||
type Spec struct { | ||
Version string `json:"swagger"` // Must be 2.0 | ||
Info *Info `json:"info,omitempty"` | ||
Paths map[string]Path `json:"paths"` | ||
Definitions map[string]Definition `json:"definitions,omitempty"` | ||
Responses map[string]Response `json:"responses,omitempty"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package swagger_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/EliCDavis/polyform/formats/swagger" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDefinitionRef(t *testing.T) { | ||
tests := map[string]struct { | ||
input string | ||
want string | ||
}{ | ||
"simple": { | ||
input: "simple", | ||
want: "#/definitions/simple", | ||
}, | ||
} | ||
|
||
for name, tc := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
assert.Equal(t, tc.want, swagger.DefinitionRefPath(tc.input)) | ||
}) | ||
} | ||
} | ||
|
||
func TestDefinitionRef_PanicOnEmpty(t *testing.T) { | ||
assert.PanicsWithError(t, "can not build definition reference from an empty string", func() { | ||
swagger.DefinitionRefPath("") | ||
}) | ||
} |
Oops, something went wrong.