-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #143 from freddrake/fd-data-type-examples
data type examples
- Loading branch information
Showing
19 changed files
with
858 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,24 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from __future__ import absolute_import, division, print_function | ||
|
||
import attr | ||
|
||
|
||
@attr.s | ||
class Example(object): | ||
""" | ||
Single example. | ||
Used starting with RAML 1.0. | ||
""" | ||
value = attr.ib(repr=False) | ||
name = attr.ib(default=None) | ||
description = attr.ib(default=None, repr=False) | ||
display_name = attr.ib(default=None, repr=False) | ||
|
||
# Not sure when validation of examples should get done; leave for now. | ||
strict = attr.ib(default=True, repr=False) | ||
|
||
# TODO: this will need to support annotations. |
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,64 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from __future__ import absolute_import, division, print_function | ||
|
||
from six import iteritems | ||
|
||
from ramlfications.errors import InvalidRAMLStructureError | ||
from ramlfications.models.base import BaseContent | ||
from ramlfications.models.examples import Example | ||
from ramlfications.utils.nodelist import NodeList | ||
|
||
|
||
def parse_examples(raml_version, data): | ||
# | ||
# This is the only place that knows when to supply examples and when | ||
# not, and that checks when both are supplied. | ||
# | ||
# This is also responsible for generating structured Example objects | ||
# when appropriate (RAML >= 1.0). | ||
# | ||
data = data or {} | ||
kw = {} | ||
example = data.get("example") | ||
|
||
if raml_version == "0.8": | ||
if "examples" in data: | ||
del data["examples"] | ||
# TODO: Emit a lint warning if there's an examples node and | ||
# we're processing RAML 0.8: authors may be expecting it to | ||
# be honored. | ||
kw["example"] = example | ||
|
||
if raml_version != "0.8": | ||
if "example" in data: | ||
kw["example"] = parse_example(None, example) | ||
|
||
kw["examples"] = None | ||
if "examples" in data: | ||
if "example" in data: | ||
# TODO: Emit a lint warning during validation. | ||
raise InvalidRAMLStructureError( | ||
"example and examples cannot co-exist") | ||
examples = data["examples"] | ||
# Must be a map: | ||
if not isinstance(examples, dict): | ||
# Need to decide what exception to make this. | ||
raise InvalidRAMLStructureError("examples must be a map node") | ||
kw["examples"] = NodeList([parse_example(nm, val) | ||
for nm, val in iteritems(examples)]) | ||
|
||
return kw | ||
|
||
|
||
def parse_example(name, node): | ||
data = dict(name=name, value=node) | ||
if name: | ||
data["display_name"] = name | ||
if isinstance(node, dict) and "value" in node: | ||
data["value"] = node["value"] | ||
data["description"] = BaseContent(node.get("description", "")) | ||
data["display_name"] = node.get("displayName", name) | ||
data["strict"] = node.get("strict", True) | ||
|
||
return Example(**data) |
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,77 @@ | ||
#%RAML 0.8 | ||
|
||
title: Example | ||
baseUri: https://example.test/{param}/ | ||
|
||
baseUriParameters: | ||
param: | ||
type: string | ||
description: account name | ||
example: splat | ||
|
||
mediaType: application/json | ||
protocols: [ HTTPS ] | ||
|
||
/with_example_and_examples: | ||
post: | ||
queryParameters: | ||
cached: | ||
type: boolean | ||
example: false | ||
description: Allow cached data? | ||
body: | ||
application/json: | ||
formParameters: | ||
key: | ||
type: string | ||
example: This is the example. | ||
|
||
# examples is ignored; may produce an lint warning later. | ||
examples: | ||
simple: "abc" | ||
fancy: "two words" | ||
excessive: | ||
displayName: "Serious Overkill" | ||
description: | | ||
There's way too much text here. | ||
value: "This is a long example." | ||
strict: false | ||
|
||
# This really isn't supported yet. | ||
multityped: | ||
- type: string | ||
description: Long explanation. | ||
example: Pile o text. | ||
- type: file | ||
description: File upload. | ||
|
||
/with_uri_param/{id}: | ||
uriParameters: | ||
id: | ||
example: s234gs9 | ||
|
||
/with_example_structured: | ||
post: | ||
body: | ||
application/json: | ||
formParameters: | ||
key: | ||
type: object | ||
example: | ||
value: This whole map is a value. | ||
|
||
/with_example_unstructured: | ||
post: | ||
body: | ||
application/json: | ||
formParameters: | ||
key: | ||
type: string | ||
example: This is a value. | ||
|
||
/with_header: | ||
displayName: Silly, silly example. | ||
headers: | ||
x-extra-fluff: | ||
type: boolean | ||
example: true |
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,18 @@ | ||
#%RAML 1.0 | ||
|
||
title: Example | ||
baseUri: https://example.test/{param}/ | ||
|
||
baseUriParameters: | ||
param: | ||
type: string | ||
description: account name | ||
example: splat | ||
examples: | ||
first: some text | ||
second: | ||
value: more text | ||
strict: false | ||
|
||
mediaType: application/json | ||
protocols: [ HTTPS ] |
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,16 @@ | ||
#%RAML 1.0 | ||
|
||
title: Example | ||
baseUri: https://example.test/{param}/ | ||
|
||
baseUriParameters: | ||
param: | ||
type: string | ||
description: account name | ||
examples: | ||
- some text | ||
- value: more text | ||
strict: false | ||
|
||
mediaType: application/json | ||
protocols: [ HTTPS ] |
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,13 @@ | ||
{"type": "object", | ||
"properties": { | ||
"name": { | ||
"type": "string" | ||
}, | ||
"age": { | ||
"description": "Age in years", | ||
"type": "integer", | ||
"minimum": 0 | ||
} | ||
}, | ||
"required": ["firstName", "lastName"] | ||
} |
Oops, something went wrong.