├── CHANGELOG.md ├── Godeps ├── Godeps.json ├── Readme └── _workspace │ ├── .gitignore │ └── src │ └── github.com │ └── buddhamagnet │ ├── raml │ ├── .gitignore │ ├── CONTRIBUTORS │ ├── LICENSE │ ├── README.md │ ├── errors.go │ ├── parser.go │ ├── samples │ │ ├── bad_raml.raml │ │ ├── congo │ │ │ └── api.raml │ │ ├── example.raml │ │ ├── github │ │ │ └── github-api-v3.raml │ │ ├── notes │ │ │ └── api.raml │ │ ├── other_example.raml │ │ ├── raml-tutorial-200 │ │ │ ├── README.md │ │ │ ├── heybulldog.mp3 │ │ │ ├── jukebox-api.raml │ │ │ ├── jukebox-include-album-new.sample │ │ │ ├── jukebox-include-album-retrieve.sample │ │ │ ├── jukebox-include-album-songs.sample │ │ │ ├── jukebox-include-album.schema │ │ │ ├── jukebox-include-albums.sample │ │ │ ├── jukebox-include-artist-albums.sample │ │ │ ├── jukebox-include-artist-new.sample │ │ │ ├── jukebox-include-artist-retrieve.sample │ │ │ ├── jukebox-include-artist.schema │ │ │ ├── jukebox-include-artists.sample │ │ │ ├── jukebox-include-song-new.sample │ │ │ ├── jukebox-include-song-retrieve.sample │ │ │ ├── jukebox-include-song.schema │ │ │ └── jukebox-include-songs.sample │ │ ├── sample_documentation.yaml │ │ └── simple_example.raml │ ├── types.go │ └── validator.go │ └── yaml │ ├── CONTRIBUTORS │ ├── LICENSE │ ├── LICENSE.libyaml │ ├── README.md │ ├── apic.go │ ├── decode.go │ ├── decode_test.go │ ├── emitterc.go │ ├── encode.go │ ├── encode_test.go │ ├── parserc.go │ ├── readerc.go │ ├── resolve.go │ ├── scannerc.go │ ├── sorter.go │ ├── suite_test.go │ ├── writerc.go │ ├── yaml.go │ ├── yamlh.go │ └── yamlprivateh.go ├── LICENCE ├── README.md ├── fixtures └── valid.raml ├── ramlapi.go ├── ramlapi_test.go └── ramlgen ├── main.go ├── ramlgen_test.go └── templates.go /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ### 1.1.0 2 | 3 | * Add support for URI parameters. 4 | 5 | ### 1.0.0 6 | 7 | * Change API to pass a struct to the consuming code. 8 | * Add support for multiple query parameters. 9 | * Add support for multiple word display names. 10 | 11 | ### 0.3.1 12 | 13 | Bring ramlgen in line with upstream ramlapi API changes 14 | 15 | ### 0.3.0 16 | 17 | Revert partial query parameter support 18 | 19 | ### 0.2.0 20 | 21 | Add query parameter support 22 | 23 | ### 0.1.0 24 | 25 | Initial release -------------------------------------------------------------------------------- /Godeps/Godeps.json: -------------------------------------------------------------------------------- 1 | { 2 | "ImportPath": "github.com/EconomistDigitalSolutions/ramlapi", 3 | "GoVersion": "go1.4", 4 | "Deps": [ 5 | { 6 | "ImportPath": "github.com/buddhamagnet/raml", 7 | "Rev": "396f76ddca3fd297d722114be47112bf2ce8d509" 8 | }, 9 | { 10 | "ImportPath": "github.com/buddhamagnet/yaml", 11 | "Rev": "e401b2b026855a4fa8e7014d553ae5bdfccb21cd" 12 | } 13 | ] 14 | } 15 | -------------------------------------------------------------------------------- /Godeps/Readme: -------------------------------------------------------------------------------- 1 | This directory tree is generated automatically by godep. 2 | 3 | Please do not edit. 4 | 5 | See https://github.com/tools/godep for more information. 6 | -------------------------------------------------------------------------------- /Godeps/_workspace/.gitignore: -------------------------------------------------------------------------------- 1 | /pkg 2 | /bin 3 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/raml/.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | *.test 24 | *.prof 25 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/raml/CONTRIBUTORS: -------------------------------------------------------------------------------- 1 | go-raml Contributor List (ordered by date): 2 | 3 | Alon Diamant [https://github.com/advance512] 4 | Dvir Volk [https://github.com/dvirsky] 5 | 6 | If anyone is missing, please message me. 7 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/raml/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2014 DoAT. All rights reserved. 2 | 3 | Redistribution and use in source and binary forms, with or without modification, 4 | are permitted provided that the following conditions are met: 5 | 6 | 1. Redistributions of source code must retain the above copyright notice, 7 | this list of conditions and the following disclaimer. 8 | 9 | 2. Redistributions in binary form must reproduce the above copyright notice, 10 | this list of conditions and the following disclaimer in the documentation and/or 11 | other materials provided with the distribution. 12 | 13 | THIS SOFTWARE IS PROVIDED “AS IS” WITHOUT ANY WARRANTIES WHATSOEVER. 14 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 15 | THE IMPLIED WARRANTIES OF NON INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A 16 | PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL DoAT OR CONTRIBUTORS 17 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 20 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 21 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 22 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 | 24 | The views and conclusions contained in the software and documentation are those of 25 | the authors and should not be interpreted as representing official policies, 26 | either expressed or implied, of DoAT. 27 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/raml/README.md: -------------------------------------------------------------------------------- 1 | raml 2 | ==== 3 | 4 | An implementation of a RAML parser for Go. Compliant with RAML 0.8. 5 | 6 | Introduction 7 | ============ 8 | 9 | RAML is a YAML-based language that describes RESTful APIs. Together with the 10 | YAML specification, this specification provides all the information necessary 11 | to describe RESTful APIs; to create API client-code and API server-code 12 | generators; and to create API user documentation from RAML API definitions. 13 | 14 | The **_raml_** package enables Go programs to parse RAML files and valid RAML API 15 | definitions. It was originally developed within [EverythingMe](https://www.everything.me). 16 | 17 | Status 18 | ------ 19 | 20 | The **_raml_** package is currently unstable and does not offer any kind of API 21 | stability guarantees. 22 | 23 | Installation 24 | ============ 25 | 26 | The yaml package may be installed by running: 27 | 28 | $ go get gopkg.in/raml.v0 29 | 30 | Opening that same URL in a browser will present a nice introductory page 31 | containing links to the documentation, source code, and all versions available 32 | for the given package: 33 | 34 | https://gopkg.in/raml.v0 35 | 36 | The actual implementation of the package is in GitHub: 37 | 38 | https://github.com/go-raml/raml 39 | 40 | Contributing to development 41 | --------------------------- 42 | 43 | Typical installation process for developing purposes: 44 | 45 | $ git clone git@github.com:go-raml/raml.git 46 | $ cd raml 47 | $ go build 48 | $ go install 49 | $ go test 50 | 51 | Usage 52 | ===== 53 | 54 | Usage is very simple: 55 | 56 | package main 57 | 58 | import ( 59 | "fmt" 60 | raml "gopkg.in/raml.v0" 61 | "github.com/kr/pretty" 62 | ) 63 | 64 | func main() { 65 | 66 | fileName := "./samples/congo/api.raml" 67 | 68 | if apiDefinition, err := raml.ParseFile(fileName); err != nil { 69 | fmt.Printf("Failed parsing RAML file %s:\n %s", fileName, err.Error()) 70 | } else { 71 | fmt.Printf("Successfully parsed RAML file %s!\n\n", fileName) 72 | pretty.Printf(apiDefinition) 73 | } 74 | } 75 | 76 | Getting help 77 | ============ 78 | 79 | * Look up the [RAML 0.8](http://raml.org/spec.html) spec if in doubt 80 | * Contact Alon, the maintainer, directly: diamant.alon@gmail.com 81 | 82 | Roadmap 83 | ======= 84 | 85 | TBD. 86 | 87 | Reporting Bugs and Contributing Code 88 | ==================================== 89 | 90 | * Want to report a bug or request a feature? Please open [an issue](https://github.com/go-raml/raml/issues/new). 91 | * Want to contribute to **_raml_**? Fork the project and make a pull request. Cool cool cool. 92 | 93 | ## License 94 | 95 | See [LICENSE](https://github.com/go-raml/raml/blob/v0/LICENSE) file. 96 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/raml/errors.go: -------------------------------------------------------------------------------- 1 | // Copyright 2014 DoAT. All rights reserved. 2 | // 3 | // Redistribution and use in source and binary forms, with or without modification, 4 | // are permitted provided that the following conditions are met: 5 | // 6 | // 1. Redistributions of source code must retain the above copyright notice, 7 | // this list of conditions and the following disclaimer. 8 | // 9 | // 2. Redistributions in binary form must reproduce the above copyright notice, 10 | // this list of conditions and the following disclaimer in the documentation and/or 11 | // other materials provided with the distribution. 12 | // 13 | // THIS SOFTWARE IS PROVIDED “AS IS” WITHOUT ANY WARRANTIES WHATSOEVER. 14 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 15 | // THE IMPLIED WARRANTIES OF NON INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A 16 | // PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL DoAT OR CONTRIBUTORS 17 | // BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 | // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 20 | // // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 21 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 22 | // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 | // 24 | // The views and conclusions contained in the software and documentation are those of 25 | // the authors and should not be interpreted as representing official policies, 26 | // either expressed or implied, of DoAT. 27 | 28 | package raml 29 | 30 | // This file contains all code related to YAML and RAML errors. 31 | 32 | import ( 33 | "fmt" 34 | "strings" 35 | 36 | yaml "github.com/buddhamagnet/yaml" 37 | ) 38 | 39 | // A RamlError is returned by the ParseFile function when RAML or YAML problems 40 | // are encountered when parsing the RAML document. 41 | type RamlError struct { 42 | Errors []string 43 | } 44 | 45 | func (e *RamlError) Error() string { 46 | return fmt.Sprintf("Error parsing RAML:\n %s\n", 47 | strings.Join(e.Errors, "\n ")) 48 | } 49 | 50 | // Populate the RAML error value with converted YAML error strings (with 51 | // additional context) 52 | func populateRAMLError(ramlError *RamlError, 53 | yamlErrors *yaml.TypeError) { 54 | 55 | // Go over the errors 56 | for _, currErr := range yamlErrors.Errors { 57 | 58 | // Create the RAML errors 59 | ramlError.Errors = 60 | append(ramlError.Errors, convertYAMLError(currErr)) 61 | } 62 | } 63 | 64 | // Convert a YAML error string into RAML error string, with more context 65 | func convertYAMLError(yamlError string) string { 66 | 67 | if strings.Contains(yamlError, "cannot unmarshal") { 68 | 69 | yamlErrorParts := strings.Split(yamlError, " ") 70 | 71 | if len(yamlErrorParts) >= 7 { 72 | 73 | fmt.Println(yamlError) 74 | 75 | var ok bool 76 | var source string 77 | var target string 78 | var targetName string 79 | line := yamlErrorParts[1] 80 | line = line[:len(line)-1] 81 | 82 | // TODO: support more complex types: 83 | // map[string]raml.NamedParameter --> 84 | // detect map, format to: 85 | // "mapping of %s to %s", ramlTypeNames["string"], ramlTypeNames["raml.NamedParameter"] 86 | // if "string" is not found, use the key, i.e. "string" in this case. 87 | // so the output would be: 88 | // mapping of string to named parameter 89 | 90 | // TODO: instead of having string in the key of some mappings, 91 | // perhaps use a type alias: 92 | // type Name string 93 | // map[Name]NamedParameter 94 | // would output: 95 | // mapping of name string to named parameter 96 | 97 | if source, ok = yamlTypeToName[yamlErrorParts[4]]; !ok { 98 | source = yamlErrorParts[4] 99 | } 100 | fmt.Println("source: ", source) 101 | 102 | if source == "string" { 103 | source = fmt.Sprintf("string (got %s)", yamlErrorParts[5]) 104 | target = yamlErrorParts[7] 105 | } else { 106 | target = yamlErrorParts[6] 107 | 108 | } 109 | if targetName, ok = ramlTypeNames[target]; !ok { 110 | targetName = target 111 | } 112 | 113 | target, _ = ramlTypes[target] 114 | 115 | return fmt.Sprintf("line %s: %s cannot be of "+ 116 | "type %s, must be %s", line, targetName, source, target) 117 | 118 | } 119 | } 120 | 121 | // Otherwise 122 | return fmt.Sprintf("YAML error, %s", yamlError) 123 | } 124 | 125 | var yamlTypeToName map[string]string = map[string]string{ 126 | "!!seq": "sequence", 127 | "!!map": "mapping", 128 | "!!int": "integer", 129 | "!!str": "string", 130 | "!!null": "null", 131 | "!!bool": "boolean", 132 | "!!float": "float", 133 | "!!timestamp": "timestamp", 134 | "!!binary": "binary", 135 | "!!merge": "merge", 136 | } 137 | 138 | var ramlTypeNames map[string]string = map[string]string{ 139 | "string": "string value", 140 | "int": "numeric value", 141 | "raml.NamedParameter": "named parameter", 142 | "raml.HTTPCode": "HTTP code", 143 | "raml.HTTPHeader": "HTTP header", 144 | "raml.Header": "header", 145 | "raml.Documentation": "documentation", 146 | "raml.Body": "body", 147 | "raml.Response": "response", 148 | "raml.DefinitionParameters": "definition parameters", 149 | "raml.DefinitionChoice": "definition choice", 150 | "raml.Trait": "trait", 151 | "raml.ResourceTypeMethod": "resource type method", 152 | "raml.ResourceType": "resource type", 153 | "raml.SecuritySchemeMethod": "security scheme method", 154 | "raml.SecurityScheme": "security scheme", 155 | "raml.Method": "method", 156 | "raml.Resource": "resource", 157 | "raml.APIDefinition": "API definition", 158 | } 159 | 160 | var ramlTypes map[string]string = map[string]string{ 161 | "string": "string", 162 | "int": "integer", 163 | "raml.NamedParameter": "mapping", 164 | "raml.HTTPCode": "integer", 165 | "raml.HTTPHeader": "string", 166 | "raml.Header": "mapping", 167 | "raml.Documentation": "mapping", 168 | "raml.Body": "mapping", 169 | "raml.Response": "mapping", 170 | "raml.DefinitionParameters": "mapping", 171 | "raml.DefinitionChoice": "string or mapping", 172 | "raml.Trait": "mapping", 173 | "raml.ResourceTypeMethod": "mapping", 174 | "raml.ResourceType": "mapping", 175 | "raml.SecuritySchemeMethod": "mapping", 176 | "raml.SecurityScheme": "mapping", 177 | "raml.Method": "mapping", 178 | "raml.Resource": "mapping", 179 | "raml.APIDefinition": "mapping", 180 | } 181 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/raml/parser.go: -------------------------------------------------------------------------------- 1 | // Copyright 2014 DoAT. All rights reserved. 2 | // 3 | // Redistribution and use in source and binary forms, with or without modification, 4 | // are permitted provided that the following conditions are met: 5 | // 6 | // 1. Redistributions of source code must retain the above copyright notice, 7 | // this list of conditions and the following disclaimer. 8 | // 9 | // 2. Redistributions in binary form must reproduce the above copyright notice, 10 | // this list of conditions and the following disclaimer in the documentation and/or 11 | // other materials provided with the distribution. 12 | // 13 | // THIS SOFTWARE IS PROVIDED “AS IS” WITHOUT ANY WARRANTIES WHATSOEVER. 14 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 15 | // THE IMPLIED WARRANTIES OF NON INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A 16 | // PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL DoAT OR CONTRIBUTORS 17 | // BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 | // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 20 | // // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 21 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 22 | // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 | // 24 | // The views and conclusions contained in the software and documentation are those of 25 | // the authors and should not be interpreted as representing official policies, 26 | // either expressed or implied, of DoAT. 27 | 28 | package raml 29 | 30 | // This file contains all of the RAML parser related code. 31 | 32 | import ( 33 | "bufio" 34 | "bytes" 35 | "errors" 36 | "fmt" 37 | "io" 38 | "io/ioutil" 39 | "path/filepath" 40 | "strings" 41 | 42 | yaml "github.com/buddhamagnet/yaml" 43 | ) 44 | 45 | // Parse a RAML file. Returns a raml.APIDefinition value or an error if 46 | // everything is something went wrong. 47 | // This is the main entry point to the RAML parser. 48 | func ParseFile(filePath string) (*APIDefinition, error) { 49 | 50 | // Get the working directory 51 | workingDirectory, fileName := filepath.Split(filePath) 52 | 53 | // Read original file contents into a byte array 54 | mainFileBytes, err := readFileContents(workingDirectory, fileName) 55 | 56 | if err != nil { 57 | return nil, err 58 | } 59 | 60 | // Get the contents of the main file 61 | mainFileBuffer := bytes.NewBuffer(mainFileBytes) 62 | 63 | // Verify the YAML version 64 | var ramlVersion string 65 | if firstLine, err := mainFileBuffer.ReadString('\n'); err != nil { 66 | return nil, fmt.Errorf("Problem reading RAML file (Error: %s)", err.Error()) 67 | } else { 68 | 69 | // We read some data... 70 | if len(firstLine) >= 10 { 71 | ramlVersion = firstLine[:10] 72 | } 73 | 74 | // TODO: Make this smart. We probably won't support multiple RAML 75 | // versions in the same package - we'll have different branches 76 | // for different versions. This one is hard-coded to 0.8. 77 | // Still, would be good to think about this. 78 | if ramlVersion != "#%RAML 0.8" { 79 | return nil, errors.New("Input file is not a RAML 0.8 file. Make " + 80 | "sure the file starts with #%RAML 0.8") 81 | } 82 | } 83 | 84 | // Pre-process the original file, following !include directive 85 | preprocessedContentsBytes, err := 86 | preProcess(mainFileBuffer, workingDirectory) 87 | 88 | if err != nil { 89 | return nil, 90 | fmt.Errorf("Error preprocessing RAML file (Error: %s)", err.Error()) 91 | } 92 | 93 | //pretty.Println(string(preprocessedContentsBytes)) 94 | 95 | // Unmarshal into an APIDefinition value 96 | apiDefinition := new(APIDefinition) 97 | apiDefinition.RAMLVersion = ramlVersion 98 | 99 | // Go! 100 | err = yaml.Unmarshal(preprocessedContentsBytes, apiDefinition) 101 | 102 | // Any errors? 103 | if err != nil { 104 | 105 | // Create a RAML error value 106 | ramlError := new(RamlError) 107 | 108 | // Copy the YAML errors into it.. 109 | if yamlErrors, ok := err.(*yaml.TypeError); ok { 110 | populateRAMLError(ramlError, yamlErrors) 111 | } else { 112 | // Or just any other error, though this shouldn't happen. 113 | ramlError.Errors = append(ramlError.Errors, err.Error()) 114 | } 115 | 116 | return nil, ramlError 117 | } 118 | 119 | postProcess(apiDefinition) 120 | 121 | // Good. 122 | return apiDefinition, nil 123 | } 124 | 125 | func postProcess(d *APIDefinition) { 126 | for _, r := range d.Resources { 127 | addMethodNames(&r) 128 | } 129 | } 130 | 131 | func addMethodNames(r *Resource) { 132 | if r.Get != nil { 133 | r.Get.Name = "GET" 134 | } 135 | if r.Post != nil { 136 | r.Post.Name = "POST" 137 | } 138 | if r.Put != nil { 139 | r.Put.Name = "PUT" 140 | } 141 | if r.Patch != nil { 142 | r.Patch.Name = "PATCH" 143 | } 144 | if r.Head != nil { 145 | r.Head.Name = "HEAD" 146 | } 147 | if r.Delete != nil { 148 | r.Delete.Name = "DELETE" 149 | } 150 | 151 | for _, n := range r.Nested { 152 | addMethodNames(n) 153 | } 154 | } 155 | 156 | // Reads the contents of a file, returns a bytes buffer 157 | func readFileContents(workingDirectory string, fileName string) ([]byte, error) { 158 | 159 | filePath := filepath.Join(workingDirectory, fileName) 160 | 161 | if fileName == "" { 162 | return nil, fmt.Errorf("File name cannot be nil: %s", filePath) 163 | } 164 | 165 | // Read the file 166 | fileContentsArray, err := ioutil.ReadFile(filePath) 167 | if err != nil { 168 | return nil, 169 | fmt.Errorf("Could not read file %s (Error: %s)", 170 | filePath, err.Error()) 171 | } 172 | 173 | return fileContentsArray, nil 174 | } 175 | 176 | // preProcess acts as a preprocessor for a RAML document in YAML format, 177 | // including files referenced via !include. It returns a pre-processed document. 178 | func preProcess(originalContents io.Reader, workingDirectory string) ([]byte, error) { 179 | 180 | // NOTE: Since YAML doesn't support !include directives, and since go-yaml 181 | // does NOT play nice with !include tags, this has to be done like this. 182 | // I am considering modifying go-yaml to add custom handlers for specific 183 | // tags, to add support for !include, but for now - this method is 184 | // GoodEnough(TM) and since it will only happen once, I am not prematurely 185 | // optimizing it. 186 | 187 | var preprocessedContents bytes.Buffer 188 | 189 | // Go over each line, looking for !include tags 190 | scanner := bufio.NewScanner(originalContents) 191 | var line string 192 | 193 | // Scan the file until we reach EOF or error out 194 | for scanner.Scan() { 195 | line = scanner.Text() 196 | 197 | // Did we find an !include directive to handle? 198 | if idx := strings.Index(line, "!include"); idx != -1 { 199 | 200 | // TODO: Do this better 201 | includeLength := len("!include ") 202 | 203 | includedFile := line[idx+includeLength:] 204 | 205 | preprocessedContents.Write([]byte(line[:idx])) 206 | 207 | // Get the included file contents 208 | includedContents, err := 209 | readFileContents(workingDirectory, includedFile) 210 | 211 | if err != nil { 212 | return nil, 213 | fmt.Errorf("Error including file %s:\n %s", 214 | includedFile, err.Error()) 215 | } 216 | 217 | // TODO: Check that you only insert .yaml, .raml, .txt and .md files 218 | // In case of .raml or .yaml, remove the comments 219 | // In case of other files, Base64 them first. 220 | 221 | // TODO: Better, step by step checks .. though prolly it'll panic 222 | // Write text files in the same indentation as the first line 223 | internalScanner := 224 | bufio.NewScanner(bytes.NewBuffer(includedContents)) 225 | 226 | // Indent by this much 227 | firstLine := true 228 | indentationString := "" 229 | 230 | // Go over each line, write it 231 | for internalScanner.Scan() { 232 | internalLine := internalScanner.Text() 233 | 234 | preprocessedContents.WriteString(indentationString) 235 | if firstLine { 236 | indentationString = strings.Repeat(" ", idx) 237 | firstLine = false 238 | } 239 | 240 | preprocessedContents.WriteString(internalLine) 241 | preprocessedContents.WriteByte('\n') 242 | } 243 | 244 | } else { 245 | 246 | // No, just a simple line.. write it 247 | preprocessedContents.WriteString(line) 248 | preprocessedContents.WriteByte('\n') 249 | } 250 | } 251 | 252 | // Any errors encountered? 253 | if err := scanner.Err(); err != nil { 254 | return nil, fmt.Errorf("Error reading YAML file: %s", err.Error()) 255 | } 256 | 257 | // Return the preprocessed contents 258 | return preprocessedContents.Bytes(), nil 259 | } 260 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/raml/samples/bad_raml.raml: -------------------------------------------------------------------------------- 1 | #%RAML 0.8 2 | title: Notes Example API 3 | /jobs: 4 | displayName: Jobs 5 | post: 6 | description: Create a job 7 | /projects: 8 | displayName: Projects 9 | post: 10 | description: Create a project 11 | /resources: 12 | displayName: Resources 13 | type: 14 | searchableCollection: 15 | queryParamName: title 16 | fallbackParamName: digest_all_fields 17 | get: 18 | description: Get a resource 19 | is: 20 | - secured: 21 | tokenName: access_token 22 | paged: 23 | maxPages: 10 24 | post: 25 | description: Post a resource 26 | /{resourceId}: 27 | get: 28 | description: Get a resource ID 29 | put: 30 | description: Put resource ID 31 | delete: 32 | description: Delete a resource ID 33 | /yet_another: 34 | get: 35 | description: Yet another 36 | uriParameters: 37 | communityDomain: 38 | displayName: Community Domain 39 | type: string 40 | communityPath: 41 | displayName: Community Path 42 | type: string 43 | pattern: ^[a-zA-Z0-9][-a-zA-Z0-9]*$ 44 | minLength: "hello" 45 | baseUri: /lala 46 | protocols: [HTTP, HTTPS] 47 | traits: 48 | - secured: 49 | queryParameters: 50 | <>: 51 | description: A valid <> is required 52 | paged: 53 | queryParameters: 54 | numPages: 55 | description: The number of pages to return, not to exceed <> 56 | resourceTypes: 57 | - rttest: 58 | description: test 59 | - one: 60 | description: hi 61 | - auditableResource: 62 | post?: 63 | body: 64 | application/x-www-form-urlencoded: 65 | formParameters: 66 | createAuthority: 67 | description: | 68 | If the resource has a post method defined, expect a createAuthority 69 | property in its body 70 | delete?: 71 | body: 72 | multipart/form-data: 73 | formParameters: 74 | deleteAuthority: 75 | description: | 76 | If the resource has a delete method defined, expect a deleteAuthority 77 | property in its body 78 | - searchableCollection: 79 | get: 80 | queryParameters: 81 | <>: 82 | description: Return <> that have their <> matching the given value 83 | <>: 84 | description: If no values match the value given for <>, use <> instead 85 | securitySchemes: 86 | - oauth_2_0: 87 | description: | 88 | Dropbox supports OAuth 2.0 for authenticating all API requests. 89 | type: OAuth 2.0 90 | describedBy: 91 | headers: 92 | Authorization: 93 | description: | 94 | Used to send a valid OAuth 2 access token. Do not use 95 | with the "access_token" query string parameter. 96 | type: string 97 | queryParameters: 98 | access_token: 99 | description: | 100 | Used to send a valid OAuth 2 access token. Do not use together with 101 | the "Authorization" header 102 | type: string 103 | responses: 104 | 401: 105 | description: | 106 | Bad or expired token. This can happen if the user or Dropbox 107 | revoked or expired an access token. To fix, you should re- 108 | authenticate the user. 109 | 403: 110 | description: | 111 | Bad OAuth request (wrong consumer key, bad nonce, expired 112 | timestamp...). Unfortunately, re-authenticating the user won't help here. 113 | settings: 114 | authorizationUri: https://www.dropbox.com/1/oauth2/authorize 115 | accessTokenUri: https://api.dropbox.com/1/oauth2/token 116 | authorizationGrants: [ code, token ] 117 | - oauth_1_0: 118 | description: | 119 | OAuth 1.0 continues to be supported for all API requests, but OAuth 2.0 is now preferred. 120 | type: OAuth 1.0 121 | settings: 122 | requestTokenUri: https://api.dropbox.com/1/oauth/request_token 123 | authorizationUri: https://www.dropbox.com/1/oauth/authorize 124 | tokenCredentialsUri: https://api.dropbox.com/1/oauth/access_token 125 | - customHeader: 126 | description: | 127 | A custom 128 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/raml/samples/congo/api.raml: -------------------------------------------------------------------------------- 1 | #%RAML 0.8 2 | title: Congo API For Drone Deliveries 3 | mediaType: application/json 4 | /deliveries: 5 | get: 6 | description: Get a list of deliveries 7 | queryParameters: 8 | sinceDate: 9 | type: date 10 | example: Mon, 14 Jul 2014 07:00:00 GMT 11 | throughDate: 12 | type: date 13 | example: Mon, 18 Jul 2014 07:00:00 GMT 14 | responses: 15 | 200: 16 | body: 17 | example: | 18 | [ 19 | { 20 | "id": "4", 21 | "at": "Tue, 08 Jul 2014 13:00:00 GMT", 22 | "toAddressId": "gi6w4fgi", 23 | "orderItemId": "6782798", 24 | "status": "completed", 25 | "droneId": "f" 26 | }, 27 | { 28 | "id": "137", 29 | "at": "Sun, 20 Jul 2014 19:36:00 GMT", 30 | "toAddressId": "6tg23d6i", 31 | "orderItemId": "7626827", 32 | "status": "scheduled", 33 | "droneId": "a" 34 | }, 35 | { 36 | "id": "8", 37 | "at": "Sun, 20 Jul 2014 21:20:00 GMT", 38 | "toAddressId": "gi6w4fgi", 39 | "orderItemId": "9877292", 40 | "status": "scheduled", 41 | "droneId": "f" 42 | } 43 | ] 44 | post: 45 | description: Create/request a new delivery 46 | body: 47 | example: | 48 | { 49 | "at": "Sun, 20 Jul 2014 21:20:00 GMT", 50 | "toAddressId": "273hh79", 51 | "orderItemId": "736786" 52 | } 53 | responses: 54 | 201: 55 | headers: 56 | Location: 57 | body: 58 | example: | 59 | { 60 | "id": "987", 61 | "at": "Sun, 20 Jul 2014 21:20:00 GMT", 62 | "toAddressId": "273hh79", 63 | "orderItemId": "736786", 64 | "status": "scheduled", 65 | "droneId": "f" 66 | } 67 | /{deliveryId}: 68 | get: 69 | description: Get information on a specific delivery 70 | responses: 71 | 200: 72 | body: 73 | example: | 74 | { 75 | "id": "8", 76 | "at": "Sun, 20 Jul 2014 21:20:00 GMT", 77 | "toAddressId": "gi6w4fgi", 78 | "orderItemId": "736786", 79 | "status": "scheduled", 80 | "droneId": "f" 81 | } 82 | patch: 83 | description: Update the information on a specific delivery 84 | body: 85 | example: | 86 | { 87 | "at": "Mon, 21 Jul 2014 00:00:00 GMT" 88 | } 89 | responses: 90 | 200: 91 | body: 92 | example: | 93 | { 94 | "id": "8", 95 | "at": "Mon, 21 Jul 2014 00:00:00 GMT", 96 | "toAddressId": "gi6w4fgi", 97 | "orderItemId": "736786", 98 | "status": "scheduled", 99 | "droneId": "f" 100 | } 101 | delete: 102 | description: Cancel a specific delivery 103 | responses: 104 | 204: 105 | /drones: 106 | get: 107 | description: Get a list of drones 108 | queryParameters: 109 | atLatitude: 110 | description: Latitude in decimal degrees 111 | type: number 112 | example: 37.8 113 | atLongitude: 114 | description: Longitude in decimal degrees 115 | type: number 116 | example: -122.3 117 | atAltitude: 118 | description: Altitude in meters above the [ellipsoid](http://www.w3.org/TR/geolocation-API/#ref-wgs) 119 | type: number 120 | example: 0 121 | atRange: 122 | description: Maximum distance from location, in meters 123 | type: number 124 | example: 50 125 | default: 100 126 | responses: 127 | 200: 128 | body: 129 | example: | 130 | [ 131 | { 132 | "id": "a", 133 | "latitude": 37.787862, 134 | "longitude": -122.404694, 135 | "altitude": 28.3, 136 | "name": "High Flyer" 137 | }, 138 | { 139 | "id": "f", 140 | "latitude": 37.852519, 141 | "longitude": -122.237390, 142 | "altitude": 56.9, 143 | "name": "Camelot" 144 | } 145 | ] 146 | post: 147 | description: Add a new drone to the fleet 148 | body: 149 | example: | 150 | { 151 | "name": "Lancelot" 152 | } 153 | /{droneId}: 154 | get: 155 | description: Get information on a specific drone 156 | responses: 157 | 200: 158 | body: 159 | example: | 160 | { 161 | "id": "f", 162 | "latitude": 37.852519, 163 | "longitude": -122.237390, 164 | "altitude": 56.9, 165 | "name": "Camelot" 166 | } 167 | patch: 168 | description: Update the information on a specific drone 169 | body: 170 | example: | 171 | { 172 | "name": "Arthur" 173 | } 174 | responses: 175 | 200: 176 | body: 177 | example: | 178 | { 179 | "id": "f", 180 | "latitude": 37.852519, 181 | "longitude": -122.237390, 182 | "altitude": 56.9, 183 | "name": "Arthur" 184 | } 185 | delete: 186 | description: Remove a drone from the fleet 187 | responses: 188 | 204: 189 | /deliveries: 190 | get: 191 | description: The deliveries scheduled for the current drone 192 | responses: 193 | 200: 194 | body: 195 | example: | 196 | [ 197 | { 198 | "id": "8", 199 | "at": "Sun, 20 Jul 2014 21:20:00 GMT", 200 | "toAddressId": "gi6w4fgi", 201 | "orderItemId": "736786", 202 | "status": "scheduled", 203 | "droneId": "f" 204 | } 205 | ] 206 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/raml/samples/example.raml: -------------------------------------------------------------------------------- 1 | #%RAML 0.8 2 | --- 3 | title: Example API 4 | baseUri: http://example.com 5 | securitySchemes: 6 | - basic: 7 | type: Basic Authentication 8 | traits: 9 | - secured: 10 | description: Some requests require authentication 11 | - unsecured: 12 | description: This is not secured 13 | - catpictures: 14 | description: requires cat headers 15 | - anotherTrait: {} 16 | - andAnotherTrait: {} 17 | - andYetAnotherTrait: {} 18 | - aFinalTrait: {} 19 | - someParameterizedTrait: 20 | description: <> 21 | resourceTypes: 22 | - collection: 23 | description: bunk 24 | documentation: 25 | - title: Getting Started 26 | content: | 27 | # Header 28 | Content 29 | ## Subheader 30 | **Bolded content** 31 | /resource: 32 | displayName: First One 33 | is: 34 | [ secured ] 35 | options: 36 | connect: 37 | trace: 38 | patch: 39 | delete: 40 | put: 41 | get: 42 | description: get the first one 43 | headers: 44 | x-custom: 45 | /{resourceId}: 46 | description: This is a resource description *with* some _markdown_ embedded in it 47 | uriParameters: 48 | resourceId: 49 | required: true 50 | description: Which resoure would you like to view 51 | get: 52 | description: | 53 | Instagram’s API uses the [OAuth 2.0 protocol](http://tools.ietf.org/html/draft-ietf-oauth-v2-12) for simple, but effective authentication and authorization. OAuth 2.0 is much easier to use than previous schemes; developers can start using the Instagram API almost immediately. The one thing to keep in mind is that all requests to the API must be made over SSL (https:// not http://) 54 | 55 | ## Do you need to authenticate? 56 | 57 | For the most part, Instagram’s API only requires the use of a _client_id). A client_id simply associates your server, script, or program with a specific application. However, some requests require authentication - specifically requests made on behalf of a user. Authenticated requests require an _access_token_. These tokens are unique to a user and should be stored securely. Access tokens may expire at any time in the future. 58 | 59 | Note that in many situations, you may not need to authenticate users at all. For instance, you may request popular photos without authenticating (i.e. you do not need to provide an access_token; just use your client ID with your request). We only require authentication in cases where your application is making requests on behalf of a user (commenting, liking, browsing a user’s feed, etc.). 60 | 61 | ## Receiving an access_token 62 | queryParameters: 63 | filter: 64 | description: What to filter 65 | type: string 66 | post: 67 | body: 68 | application/json: 69 | application/x-www-form-urlencoded: 70 | formParameters: 71 | name: 72 | description: The name of the resource to create 73 | type: string 74 | example: Comment 75 | description: 76 | description: A description of the resource to create 77 | type: string 78 | example: User-generated content pertinent to the associated blog post 79 | multipart/form-data: 80 | formParameters: 81 | name: 82 | description: The name of the resource to create 83 | type: string 84 | example: Comment 85 | description: 86 | description: A description of the resource to create 87 | type: string 88 | example: User-generated content pertinent to the associated blog post 89 | 90 | /another/resource: 91 | displayName: Cats 92 | type: collection 93 | is: [secured, catpictures] 94 | connect: !!null 95 | head: !!null 96 | get: 97 | queryParameters: 98 | chunk: 99 | displayName: page 100 | description: Which page to display 101 | type: integer 102 | example: 1 103 | minimum: 1 104 | maximum: 100 105 | required: true 106 | order: 107 | description: The sort order of resources 108 | type: string 109 | enum: ["oldest", "newest"] 110 | example: oldest 111 | minLength: 5 112 | maxLength: 7 113 | default: newest 114 | query: 115 | description: A query parameter 116 | repeat: true 117 | 118 | /resource-with-headers: 119 | displayName: Resource With headers 120 | get: 121 | headers: 122 | x-custom-header: 123 | displayName: Custom header 124 | description: This header is used to send data that... 125 | type: string 126 | pattern: ^\w+$ 127 | x-p-{*}: 128 | displayName: Parameterized header 129 | 130 | /secured-resource: 131 | displayName: SO SECURE 132 | get: 133 | securedBy: [basic] 134 | /resource-with-method-level-traits: 135 | displayName: First One 136 | is: [secured] 137 | get: 138 | is: [unsecured, catpictures, anotherTrait, andAnotherTrait, andYetAnotherTrait, aFinalTrait, someParameterizedTrait: { someParameterName: someParameterValue }, otherParameterizedTrait : {asd: 123}] 139 | description: get the first one 140 | 141 | /resource-with-form-and-multipart-form-parameters: 142 | get: 143 | queryParameters: 144 | some_query_param: 145 | displayName: Some Query Param 146 | description: Your value for some thing. 147 | type: string 148 | required: true 149 | example: "my value" 150 | body: 151 | application/json: 152 | example: | 153 | { 154 | "api_key": "c4f820f0420a013ea143230c290fbf99", 155 | ... 156 | } 157 | application/x-www-form-urlencoded: 158 | formParameters: 159 | api_key: 160 | displayName: API Key 161 | description: Your license key for the application. Please contact developer@nzpost.co.nz for a license key 162 | type: string 163 | required: true 164 | example: "c4f820f0420a013ea143230c290fbf99" 165 | multipart/form-data: 166 | formParameters: 167 | api_key: 168 | displayName: API Key 169 | description: Your license key for the application. Please contact developer@nzpost.co.nz for a license key 170 | type: string 171 | required: true 172 | example: "c4f820f0420a013ea143230c290fbf99" 173 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/raml/samples/notes/api.raml: -------------------------------------------------------------------------------- 1 | #%RAML 0.8 2 | title: Notes Example API 3 | version: v2 4 | mediaType: application/json 5 | documentation: 6 | - title: Overview 7 | content: This is an example of a simple API for a "notes" service 8 | /notes: 9 | description: A collection of notes 10 | get: 11 | description: List all notes, optionally filtered by a query string 12 | queryParameters: 13 | q: 14 | description: An optional search query to filter the results 15 | example: shopping 16 | responses: 17 | 200: 18 | body: 19 | text/json: 20 | example: | 21 | [ { "id": 1, "title": "Buy some milk", "status": "done" }, 22 | { "id": 2, "title": "Return sweater", "status": "overdue", "dueInDays": -2 }, 23 | { "id": 3, "title": "Renew license", "status": "not done", "dueInDays": 1 }, 24 | { "id": 4, "title": "Join gym", "status": "not done", "dueInDays": 3 } ] 25 | post: 26 | description: Create a new note in the collection 27 | body: 28 | example: | 29 | { "title": "Return sweater", "dueInDays": -2 } 30 | headers: 31 | X-Tracking-Example: 32 | description: You can specify request headers like this 33 | enum: [ accounting, payroll, finance ] 34 | required: false # require it by changing this to true 35 | example: accounting 36 | responses: 37 | 201: 38 | headers: 39 | X-Powered-By: 40 | description: You can describe response headers like this 41 | example: RAML 42 | body: 43 | text/json: 44 | example: | 45 | { 46 | "id": 2, 47 | "title": "Return sweater", 48 | "status": "overdue", 49 | "dueInDays": -2 50 | } 51 | /{id}: 52 | description: A specific note, identified by its id 53 | uriParameters: 54 | id: 55 | description: The `id` of the specific note 56 | type: number 57 | example: 2 58 | get: 59 | description: Retrieve the specified note 60 | responses: 61 | 200: 62 | text/json: 63 | body: 64 | example: | 65 | { 66 | "id": 2, 67 | "title": "Return sweater", 68 | "status": "overdue", 69 | "dueInDays": -2 70 | } 71 | patch: 72 | description: Update the specified note (allowing partial information) 73 | body: 74 | text/json: 75 | example: | 76 | { 77 | "title": "Exchange sweater", 78 | "dueInDays: 3 79 | } 80 | responses: 81 | 200: 82 | text/json: 83 | body: 84 | example: | 85 | { 86 | "id": 2, 87 | "title": "Exchange sweater", 88 | "status": "not done", 89 | "dueInDays": 3 90 | } 91 | delete: 92 | description: Remove the specified note 93 | responses: 94 | 204: 95 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/raml/samples/other_example.raml: -------------------------------------------------------------------------------- 1 | #%RAML 0.8 2 | title: Notes Example API 3 | mediaType: application/json 4 | documentation: 5 | - title: Overview 6 | content: This is an example of a simple API for a "notes" service 7 | - title: Explanation In Depth 8 | content: Enjoy this 9 | baseUri: /lala 10 | protocols: [HTTP, HTTPS] 11 | schemas: 12 | - schema1: "jsonStuff" 13 | schema2: "" 14 | - schema3: "another value" 15 | uriParameters: 16 | communityDomain: 17 | displayName: Community Domain 18 | type: string 19 | communityPath: 20 | displayName: Community Path 21 | type: string 22 | pattern: ^[a-zA-Z0-9][-a-zA-Z0-9]*$ 23 | minLength: 1 24 | baseUriParameters: 25 | test: 26 | displayName: TEST PARAM 27 | description: a crazy description 28 | type: paramType 29 | pattern: regexpCRAZYNESS 30 | minLength: 10 31 | maxLength: 20 32 | minimum: 0.3 33 | maximum: 0.5 34 | repeat: true 35 | required: false 36 | default: "default string" 37 | example: 50 38 | enum: [ example, 4, 0.3 ] 39 | apiDomain: 40 | description: | 41 | The sub-domain at which the API is accessible. Most API calls are sent to https://api.dropbox.com 42 | enum: [ "api" ] 43 | traits: 44 | - anothertrait: 45 | usage: any way you want it 46 | description: why 47 | yetanothertrait: 48 | usage: don't use it 49 | description: honestly, don't 50 | - testtrait: 51 | usage: use this trait so happilly 52 | description: Traits RULE 53 | queryParameters: 54 | test: 55 | displayName: TEST PARAM 56 | description: a crazy description 57 | type: paramType 58 | pattern: regexpCRAZYNESS 59 | minLength: 10 60 | maxLength: 20 61 | minimum: 0.3 62 | maximum: 0.5 63 | repeat: true 64 | required: false 65 | default: "default string" 66 | example: 50 67 | enum: [ example, 4, 0.3 ] 68 | apiDomain: 69 | description: | 70 | The sub-domain at which the API is accessible. Most API calls are sent to https://api.dropbox.com 71 | enum: [ "api" ] 72 | body: 73 | application/json: 74 | schema: responseExampleJSON 75 | description: SDFSDF 76 | example: DFGD 77 | headers: 78 | header5: 79 | displayName: HEADER ONE 80 | header9: 81 | displayName: HEADER tWOOOOTWOTW 82 | application/xml: 83 | schema: responseExampleXML 84 | description: SDFSDF 85 | example: DFGD 86 | formParameters: 87 | formParam234: 88 | displayName: SO WORKS 89 | headers: 90 | header10: 91 | displayName: HEADER ONE 92 | header20: 93 | displayName: HEADER tWOOOOTWOTW 94 | headers: 95 | header1: 96 | displayName: HEADER ONE 97 | header2: 98 | displayName: HEADER tWOOOOTWOTW 99 | responses: 100 | 200: 101 | description: response200 102 | headers: 103 | header1: 104 | displayName: HEADER ONE 105 | header2: 106 | displayName: HEADER tWOOOOTWOTW 107 | body: 108 | application/json: 109 | schema: responseExampleJSON 110 | description: SDFSDF 111 | example: DFGD 112 | headers: 113 | header5: 114 | displayName: HEADER ONE 115 | header9: 116 | displayName: HEADER tWOOOOTWOTW 117 | application/xml: 118 | schema: responseExampleXML 119 | description: SDFSDF 120 | example: DFGD 121 | formParameters: 122 | formParam234: 123 | displayName: SO WORKS 124 | headers: 125 | header10: 126 | displayName: HEADER ONE 127 | header20: 128 | displayName: HEADER tWOOOOTWOTW 129 | 404: 130 | description: response404 131 | headers: 132 | header1: 133 | displayName: HEADER ONE 134 | header2: 135 | displayName: HEADER tWOOOOTWOTW 136 | body: 137 | application/json: 138 | schema: responseExampleJSON 139 | description: SDFSDF 140 | example: DFGD 141 | headers: 142 | header5: 143 | displayName: HEADER ONE 144 | header9: 145 | displayName: HEADER tWOOOOTWOTW 146 | application/xml: 147 | schema: responseExampleXML 148 | description: SDFSDF 149 | example: DFGD 150 | formParameters: 151 | formParam234: 152 | displayName: SO WORKS 153 | headers: 154 | header10: 155 | displayName: HEADER ONE 156 | header20: 157 | displayName: HEADER tWOOOOTWOTW 158 | resourceTypes: 159 | - rttest: 160 | description: test 161 | - one: 162 | description: hi 163 | two: 164 | description: bye 165 | get: 166 | description: test 167 | - collection: 168 | description: collectme 169 | get: 170 | responses: 171 | 200: 172 | body: 173 | application/json: 174 | schema: 175 | <> # e.g. users 176 | post: 177 | responses: 178 | 200: 179 | body: 180 | schema: 181 | <> # e.g. user 182 | image/jpeg: 183 | example: <> # e.g. user 184 | - member: 185 | description: memme 186 | get: 187 | responses: 188 | 200: 189 | body: 190 | schema: 191 | <> # e.g. user 192 | /jobs: 193 | displayName: Jobs 194 | post: 195 | description: Create a job 196 | body: 197 | text/xml: 198 | schema: 199 | schema1 # using a name from the root schemas 200 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/raml/samples/raml-tutorial-200/README.md: -------------------------------------------------------------------------------- 1 | raml-tutorial-200 2 | ================= 3 | 4 | Step by step 200 raml tutorial code 5 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/raml/samples/raml-tutorial-200/heybulldog.mp3: -------------------------------------------------------------------------------- 1 | I am not an MP3. 2 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/raml/samples/raml-tutorial-200/jukebox-api.raml: -------------------------------------------------------------------------------- 1 | #%RAML 0.8 2 | --- 3 | title: Jukebox API 4 | baseUri: http://jukebox.api.com 5 | version: v1 6 | 7 | schemas: 8 | - song: !include jukebox-include-song.schema 9 | - artist: !include jukebox-include-artist.schema 10 | - album: !include jukebox-include-album.schema 11 | 12 | 13 | resourceTypes: 14 | - readOnlyCollection: 15 | description: Collection of available <> in Jukebox. 16 | get: 17 | description: Get a list of <>. 18 | responses: 19 | 200: 20 | body: 21 | application/json: 22 | example: | 23 | <> 24 | - collection: 25 | description: Collection of available <> in Jukebox. 26 | get: 27 | description: Get a list of <>. 28 | responses: 29 | 200: 30 | body: 31 | application/json: 32 | example: | 33 | <> 34 | post: 35 | description: | 36 | Add a new <> to Jukebox. 37 | queryParameters: 38 | access_token: 39 | description: "The access token provided by the authentication application" 40 | example: AABBCCDD 41 | required: true 42 | type: string 43 | body: 44 | application/json: 45 | schema: <> 46 | example: | 47 | <> 48 | responses: 49 | 200: 50 | body: 51 | application/json: 52 | example: | 53 | { "message": "The <> has been properly entered" } 54 | - collection-item: 55 | description: Entity representing a <> 56 | get: 57 | description: | 58 | Get the <> 59 | with <>Id = 60 | {<>Id} 61 | responses: 62 | 200: 63 | body: 64 | application/json: 65 | example: | 66 | <> 67 | 404: 68 | body: 69 | application/json: 70 | example: | 71 | {"message": "<> not found" } 72 | traits: 73 | - searchable: 74 | queryParameters: 75 | query: 76 | description: | 77 | JSON array [{"field1","value1","operator1"},{"field2","value2","operator2"},...,{"fieldN","valueN","operatorN"}] <> 78 | example: | 79 | <> 80 | - orderable: 81 | queryParameters: 82 | orderBy: 83 | description: | 84 | Order by field: <> 85 | type: string 86 | required: false 87 | order: 88 | description: Order 89 | enum: [desc, asc] 90 | default: desc 91 | required: false 92 | - pageable: 93 | queryParameters: 94 | offset: 95 | description: Skip over a number of elements by specifying an offset value for the query 96 | type: integer 97 | required: false 98 | example: 20 99 | default: 0 100 | limit: 101 | description: Limit the number of elements on the response 102 | type: integer 103 | required: false 104 | example: 80 105 | default: 10 106 | 107 | /songs: 108 | type: 109 | collection: 110 | exampleCollection: !include jukebox-include-songs.sample 111 | exampleItem: !include jukebox-include-song-new.sample 112 | get: 113 | is: [ 114 | searchable: {description: "with valid searchable fields: songTitle", example: "[\"songTitle\", \"Get L\", \"like\"]"}, 115 | orderable: {fieldsList: "songTitle"}, 116 | pageable 117 | ] 118 | /{songId}: 119 | type: 120 | collection-item: 121 | exampleItem: !include jukebox-include-song-retrieve.sample 122 | /file-content: 123 | description: The file to be reproduced by the client 124 | get: 125 | description: Get the file content 126 | responses: 127 | 200: 128 | body: 129 | binary/octet-stream: 130 | example: 131 | !include heybulldog.mp3 132 | post: 133 | description: | 134 | Enters the file content for an existing song entity. 135 | 136 | The song needs to be created for the `/songs/{songId}/file-content` to exist. 137 | You can use this second resource to get and post the file to reproduce. 138 | 139 | Use the "binary/octet-stream" content type to specify the content from any consumer (excepting web-browsers). 140 | Use the "multipart-form/data" content type to upload a file which content will become the file-content 141 | body: 142 | binary/octet-stream: 143 | multipart/form-data: 144 | formParameters: 145 | file: 146 | description: The file to be uploaded 147 | required: true 148 | type: file 149 | /artists: 150 | type: 151 | collection: 152 | exampleCollection: !include jukebox-include-artists.sample 153 | exampleItem: !include jukebox-include-artist-new.sample 154 | get: 155 | is: [ 156 | searchable: {description: "with valid searchable fields: countryCode", example: "[\"countryCode\", \"FRA\", \"equals\"]"}, 157 | orderable: {fieldsList: "artistName, nationality"}, 158 | pageable 159 | ] 160 | /{artistId}: 161 | type: 162 | collection-item: 163 | exampleItem: !include jukebox-include-artist-retrieve.sample 164 | /albums: 165 | type: 166 | readOnlyCollection: 167 | exampleCollection: !include jukebox-include-artist-albums.sample 168 | description: Collection of albulms belonging to the artist 169 | get: 170 | description: Get a specific artist's albums list 171 | is: [orderable: {fieldsList: "albumName"}, pageable] 172 | /albums: 173 | type: 174 | collection: 175 | exampleCollection: !include jukebox-include-albums.sample 176 | exampleItem: !include jukebox-include-album-new.sample 177 | get: 178 | is: [ 179 | searchable: {description: "with valid searchable fields: genreCode", example: "[\"genreCode\", \"ELE\", \"equals\"]"}, 180 | orderable: {fieldsList: "albumName, genre"}, 181 | pageable 182 | ] 183 | /{albumId}: 184 | type: 185 | collection-item: 186 | exampleItem: !include jukebox-include-album-retrieve.sample 187 | /songs: 188 | type: 189 | readOnlyCollection: 190 | exampleCollection: | 191 | !include jukebox-include-album-songs.sample 192 | get: 193 | is: [orderable: {fieldsList: "songTitle"}] 194 | description: Get the list of songs for the album with `albumId = {albumId}` 195 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/raml/samples/raml-tutorial-200/jukebox-include-album-new.sample: -------------------------------------------------------------------------------- 1 | '{ 2 | "albumId": "183100e3-0e2b-4404-a716-66104d440550", 3 | "albumName": "Random Access Memories", 4 | "year": "2013", 5 | "imageURL": "http://upload.wikimedia.org/wikipedia/en/a/a7/Random_Access_Memories.jpg", 6 | "genreCode": "ELE", 7 | "artistId": "110e8300-e32b-41d4-a716-664400445500" 8 | }' 9 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/raml/samples/raml-tutorial-200/jukebox-include-album-retrieve.sample: -------------------------------------------------------------------------------- 1 | '{ 2 | "albumId": "183100e3-0e2b-4404-a716-66104d440550", 3 | "albumName": "Random Access Memories", 4 | "year": "2013", 5 | "genre": "Electric Funk", 6 | "imageURL": "http://upload.wikimedia.org/wikipedia/en/a/a7/Random_Access_Memories.jpg", 7 | "genre": { 8 | "countryCode": "ELE", 9 | "countryName": "Electronict" 10 | }, 11 | "songs": [ 12 | { 13 | "songId": "550e8400-e29b-41d4-a716-446655440000", 14 | "songTitle": "Get Lucky" 15 | }, 16 | { 17 | "songId": "550e8400-e29b-41d4-a716-446655440111", 18 | "songTitle": "Loose yourself to dance" 19 | }, 20 | { 21 | "songId": "550e8400-e29b-41d4-a716-446655440222", 22 | "songTitle": "Gio sorgio by Moroder" 23 | } 24 | ], 25 | "artist": { 26 | "artistId": "110e8300-e32b-41d4-a716-664400445500", 27 | "artistName": "Daft Punk", 28 | "imageURL": "http://travelhymns.com/wp-content/uploads/2013/06/random-access-memories1.jpg" 29 | } 30 | }' 31 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/raml/samples/raml-tutorial-200/jukebox-include-album-songs.sample: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "songId": "550e8400-e29b-41d4-a716-446655440000", 4 | "songTitle": "Get Lucky" 5 | }, 6 | { 7 | "songId": "550e8400-e29b-41d4-a716-446655440111", 8 | "songTitle": "Loose yourself to dance" 9 | }, 10 | { 11 | "songId": "550e8400-e29b-41d4-a716-446655440222", 12 | "songTitle": "Gio sorgio by Moroder" 13 | } 14 | ] 15 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/raml/samples/raml-tutorial-200/jukebox-include-album.schema: -------------------------------------------------------------------------------- 1 | '{ 2 | "type": "object", 3 | "$schema": "http://json-schema.org/draft-03/schema", 4 | "id": "http://jsonschema.net", 5 | "required":false, 6 | "properties": { 7 | "albumId": { 8 | "type": "string", 9 | "required":true, 10 | "minLength": 36, 11 | "maxLength": 36 12 | }, 13 | "albumName": { 14 | "type": "string", 15 | "required": true 16 | }, 17 | "year": { 18 | "type": "string", 19 | "required": false 20 | }, 21 | "iamgeURL": { 22 | "type": "string", 23 | "required": false 24 | }, 25 | "genreCode": { 26 | "type": "string", 27 | "required": true 28 | }, 29 | "artistId": { 30 | "type": "string", 31 | "required":true, 32 | "minLength": 36, 33 | "maxLength": 36 34 | } 35 | } 36 | }' 37 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/raml/samples/raml-tutorial-200/jukebox-include-albums.sample: -------------------------------------------------------------------------------- 1 | '[ 2 | { 3 | "albumId": "183100e3-0e2b-4404-a716-66104d440550", 4 | "albumName": "Random Access Memories", 5 | "imageURL": "http://upload.wikimedia.org/wikipedia/en/a/a7/Random_Access_Memories.jpg", 6 | "artistId": "110e8300-e32b-41d4-a716-664400445500", 7 | "genre": { 8 | "countryCode": "ELE", 9 | "countryName": "Electronict" 10 | } 11 | }, 12 | { 13 | "albumId": "183100e3-0e2b-4404-3123-66111d4de520", 14 | "albumName": "OK Computer", 15 | "imageURL": "http://www.greenplastic.com/dev/wp-content/uploads/2010/12/ok-computer.jpg", 16 | "artistId": "11032be3-41d4-4455-a716-664400a71600", 17 | "genre": { 18 | "countryCode": "ALT", 19 | "countryName": "Alternative Rock" 20 | } 21 | }, 22 | { 23 | "albumId": "183100e3-cccc-4404-1111-63204d64coda", 24 | "albumName": "The Dark Side of the Moon", 25 | "imageURL": "http://upload.wikimedia.org/wikipedia/en/3/3b/Dark_Side_of_the_Moon.png", 26 | "artistId": "110e8300-e32b-41d4-a716-229932554400", 27 | "genre": { 28 | "countryCode": "PRO", 29 | "countryName": "Progressive Rock" 30 | } 31 | } 32 | ]' 33 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/raml/samples/raml-tutorial-200/jukebox-include-artist-albums.sample: -------------------------------------------------------------------------------- 1 | '[ 2 | { 3 | "albumId": "183100e3-0e2b-4404-a716-66104d440550", 4 | "albumName": "Random Access Memories", 5 | "imageURL": "http://upload.wikimedia.org/wikipedia/en/a/a7/Random_Access_Memories.jpg" 6 | }, 7 | { 8 | "albumId": "183100e3-0e2b-4404-a716-66104d440551", 9 | "albumName": "TRON: Legacy R3CONF1GUR3D", 10 | "imageURL": "http://ecx.images-amazon.com/images/I/51Tvo-iArBL.jpg" 11 | }, 12 | { 13 | "albumId": "183100e3-0e2b-4404-a716-66104d440552", 14 | "albumName": "TRON: Legacy", 15 | "imageURL": "http://upload.wikimedia.org/wikipedia/en/3/39/Tron_Legacy_Soundtrack.jpg" 16 | }, 17 | { 18 | "albumId": "183100e3-0e2b-4404-a716-66104d440553", 19 | "albumName": "Alive", 20 | "imageURL": "http://upload.wikimedia.org/wikipedia/en/4/49/Daft_Punk_Alive_2007.JPG" 21 | }, 22 | { 23 | "albumId": "183100e3-0e2b-4404-a716-66104d440554", 24 | "albumName": "Musique Vol. 1", 25 | "imageURL": "http://upload.wikimedia.org/wikipedia/en/a/ab/Musique_Vol._1_1993%E2%80%932005.png" 26 | }, 27 | { 28 | "albumId": "183100e3-0e2b-4404-a716-66104d440555", 29 | "albumName": "Human After All", 30 | "imageURL": "http://upload.wikimedia.org/wikipedia/en/0/0d/Humanafterall.jpg" 31 | }, 32 | { 33 | "albumId": "183100e3-0e2b-4404-a716-66104d440556", 34 | "albumName": "Daft Club", 35 | "imageURL": "http://upload.wikimedia.org/wikipedia/en/f/fc/Daftclub.jpg" 36 | }, 37 | { 38 | "albumId": "183100e3-0e2b-4404-a716-66104d440557", 39 | "albumName": "Discovery", 40 | "imageURL": "http://ecx.images-amazon.com/images/I/71bsHTr6idL._SL1500_.jpg" 41 | } 42 | ]' 43 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/raml/samples/raml-tutorial-200/jukebox-include-artist-new.sample: -------------------------------------------------------------------------------- 1 | '{ 2 | "artistName": "Daft Punk", 3 | "description": "French electronic music duo consisting of musicians Guy-Manuel de Homem-Christo and Thomas Bangalter", 4 | "imageURL": "http://travelhymns.com/wp-content/uploads/2013/06/random-access-memories1.jpg" 5 | }' 6 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/raml/samples/raml-tutorial-200/jukebox-include-artist-retrieve.sample: -------------------------------------------------------------------------------- 1 | '{ 2 | "artistId": "110e8300-e32b-41d4-a716-664400445500", 3 | "artistName": "Daft Punk", 4 | "description": "French electronic music duo consisting of musicians Guy-Manuel de Homem-Christo and Thomas Bangalter", 5 | "imageURL": "http://travelhymns.com/wp-content/uploads/2013/06/random-access-memories1.jpg", 6 | "nationality": { 7 | "countryCode": "FRA", 8 | "countryName": "France" 9 | }, 10 | "albums": [ 11 | { 12 | "albumId": "183100e3-0e2b-4404-a716-66104d440550", 13 | "albumName": "Random Access Memories", 14 | "imageURL": "http://upload.wikimedia.org/wikipedia/en/a/a7/Random_Access_Memories.jpg" 15 | }, 16 | { 17 | "albumId": "183100e3-0e2b-4404-a716-66104d440551", 18 | "albumName": "TRON: Legacy R3CONF1GUR3D", 19 | "imageURL": "http://ecx.images-amazon.com/images/I/51Tvo-iArBL.jpg" 20 | }, 21 | { 22 | "albumId": "183100e3-0e2b-4404-a716-66104d440552", 23 | "albumName": "TRON: Legacy", 24 | "imageURL": "http://upload.wikimedia.org/wikipedia/en/3/39/Tron_Legacy_Soundtrack.jpg" 25 | }, 26 | { 27 | "albumId": "183100e3-0e2b-4404-a716-66104d440553", 28 | "albumName": "Alive", 29 | "imageURL": "http://upload.wikimedia.org/wikipedia/en/4/49/Daft_Punk_Alive_2007.JPG" 30 | }, 31 | { 32 | "albumId": "183100e3-0e2b-4404-a716-66104d440554", 33 | "albumName": "Musique Vol. 1", 34 | "imageURL": "http://upload.wikimedia.org/wikipedia/en/a/ab/Musique_Vol._1_1993%E2%80%932005.png" 35 | }, 36 | { 37 | "albumId": "183100e3-0e2b-4404-a716-66104d440555", 38 | "albumName": "Human After All", 39 | "imageURL": "http://upload.wikimedia.org/wikipedia/en/0/0d/Humanafterall.jpg" 40 | }, 41 | { 42 | "albumId": "183100e3-0e2b-4404-a716-66104d440556", 43 | "albumName": "Daft Club", 44 | "imageURL": "http://upload.wikimedia.org/wikipedia/en/f/fc/Daftclub.jpg" 45 | }, 46 | { 47 | "albumId": "183100e3-0e2b-4404-a716-66104d440557", 48 | "albumName": "Discovery", 49 | "imageURL": "http://ecx.images-amazon.com/images/I/71bsHTr6idL._SL1500_.jpg" 50 | }] 51 | }' 52 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/raml/samples/raml-tutorial-200/jukebox-include-artist.schema: -------------------------------------------------------------------------------- 1 | '{ 2 | "type": "object", 3 | "$schema": "http://json-schema.org/draft-03/schema", 4 | "id": "http://jsonschema.net", 5 | "required":false, 6 | "properties": { 7 | "artistName": { 8 | "type": "string", 9 | "required":true 10 | }, 11 | "description": { 12 | "type": "string", 13 | "required": false 14 | }, 15 | "imageURL": { 16 | "type": "string", 17 | "required": false 18 | } 19 | } 20 | }' 21 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/raml/samples/raml-tutorial-200/jukebox-include-artists.sample: -------------------------------------------------------------------------------- 1 | '[{ 2 | "artistId": "110e8300-e32b-41d4-a716-664400445500", 3 | "artistName": "Daft Punk", 4 | "description": "French electronic music duo consisting of musicians Guy-Manuel de Homem-Christo and Thomas Bangalter", 5 | "imageURL": "http://travelhymns.com/wp-content/uploads/2013/06/random-access-memories1.jpg", 6 | "nationality": { 7 | "countryCode": "FRA", 8 | "countryName": "France" 9 | } 10 | }, 11 | { 12 | "artistId": "110e8300-e32b-41d4-a716-229932554400", 13 | "artistName": "Pink Floyd", 14 | "description": "English rock band that achieved international acclaim with their progressive and psychedelic music.", 15 | "imageURL": "http://www.billboard.com/files/styles/promo_650/public/stylus/1251869-pink-floyd-reunions-617-409.jpg", 16 | "nationality": { 17 | "countryCode": "ENG", 18 | "countryName": "England" 19 | } 20 | }, 21 | { 22 | "artistId": "11032be3-41d4-4455-a716-664400a71600", 23 | "artistName": "Radiohead", 24 | "description": " English rock band from Abingdon, Oxfordshire, formed in 1985", 25 | "imageURL": "http://www.wired.com/images_blogs/photos/uncategorized/2007/10/01/radiohead.jpg", 26 | "nationality": { 27 | "countryCode": "ENG", 28 | "countryName": "England" 29 | } 30 | }, 31 | ]' 32 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/raml/samples/raml-tutorial-200/jukebox-include-song-new.sample: -------------------------------------------------------------------------------- 1 | '{ 2 | "songId": "550e8400-e29b-41d4-a716-446655440000", 3 | "songTitle": "Get Lucky", 4 | "albumId": "183100e3-0e2b-4404-a716-66104d440550" 5 | }' 6 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/raml/samples/raml-tutorial-200/jukebox-include-song-retrieve.sample: -------------------------------------------------------------------------------- 1 | '{ 2 | "songId": "550e8400-e29b-41d4-a716-446655440000", 3 | "songTitle": "Get Lucky", 4 | "duration": "6:07", 5 | "artist": { 6 | "artistId": "110e8300-e32b-41d4-a716-664400445500", 7 | "artistName": "Daft Punk", 8 | "imageURL": "http://travelhymns.com/wp-content/uploads/2013/06/random-access-memories1.jpg" 9 | }, 10 | "album": { 11 | "albumId": "183100e3-0e2b-4404-a716-66104d440550", 12 | "albumName": "Random Access Memories", 13 | "imageURL": "http://upload.wikimedia.org/wikipedia/en/a/a7/Random_Access_Memories.jpg" 14 | } 15 | }' 16 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/raml/samples/raml-tutorial-200/jukebox-include-song.schema: -------------------------------------------------------------------------------- 1 | '{ 2 | "type": "object", 3 | "$schema": "http://json-schema.org/draft-03/schema", 4 | "id": "http://jsonschema.net", 5 | "required": true, 6 | "properties": { 7 | "songId": { 8 | "type": "string", 9 | "required": true, 10 | "minLength": 36, 11 | "maxLength": 36 12 | }, 13 | "songTitle": { 14 | "type": "string", 15 | "required": true 16 | }, 17 | "albumId": { 18 | "type": "string", 19 | "required": true, 20 | "minLength": 36, 21 | "maxLength": 36 22 | } 23 | } 24 | }' 25 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/raml/samples/raml-tutorial-200/jukebox-include-songs.sample: -------------------------------------------------------------------------------- 1 | '[ 2 | { 3 | "songId": "550e8400-e29b-41d4-a716-446655440000", 4 | "songTitle": "Get Lucky" 5 | }, 6 | { 7 | "songId": "550e8400-e29b-41d4-a716-446655440111", 8 | "songTitle": "Loose yourself to dance" 9 | }, 10 | { 11 | "songId": "550e8400-e29b-41d4-a716-446655440222", 12 | "songTitle": "Gio sorgio by Morodera" 13 | } 14 | ]' 15 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/raml/samples/sample_documentation.yaml: -------------------------------------------------------------------------------- 1 | - title: Overview 2 | content: This is an example of a simple API for a "notes" service 3 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/raml/samples/simple_example.raml: -------------------------------------------------------------------------------- 1 | #%RAML 0.8 2 | title: Notes Example API 3 | /jobs: 4 | displayName: Jobs 5 | post: 6 | description: Create a job 7 | /projects: 8 | displayName: Projects 9 | post: 10 | description: Create a project 11 | /resources: 12 | displayName: Resources 13 | type: 14 | searchableCollection: 15 | queryParamName: title 16 | fallbackParamName: digest_all_fields 17 | get: 18 | description: Get a resource 19 | is: 20 | - secured: 21 | tokenName: access_token 22 | paged: 23 | maxPages: 10 24 | post: 25 | description: Post a resource 26 | /{resourceId}: 27 | get: 28 | description: Get a resource ID 29 | put: 30 | description: Put resource ID 31 | delete: 32 | description: Delete a resource ID 33 | /yet_another: 34 | get: 35 | description: Yet another 36 | uriParameters: 37 | communityDomain: 38 | displayName: Community Domain 39 | type: string 40 | communityPath: 41 | displayName: Community Path 42 | type: string 43 | pattern: ^[a-zA-Z0-9][-a-zA-Z0-9]*$ 44 | minLength: 1 45 | baseUri: /lala 46 | protocols: [HTTP, HTTPS] 47 | traits: 48 | - secured: 49 | queryParameters: 50 | <>: 51 | description: A valid <> is required 52 | paged: 53 | queryParameters: 54 | numPages: 55 | description: The number of pages to return, not to exceed <> 56 | resourceTypes: 57 | - rttest: 58 | description: test 59 | - one: 60 | description: hi 61 | - auditableResource: 62 | post?: 63 | body: 64 | application/x-www-form-urlencoded: 65 | formParameters: 66 | createAuthority: 67 | description: | 68 | If the resource has a post method defined, expect a createAuthority 69 | property in its body 70 | delete?: 71 | body: 72 | multipart/form-data: 73 | formParameters: 74 | deleteAuthority: 75 | description: | 76 | If the resource has a delete method defined, expect a deleteAuthority 77 | property in its body 78 | - searchableCollection: 79 | get: 80 | queryParameters: 81 | <>: 82 | description: Return <> that have their <> matching the given value 83 | <>: 84 | description: If no values match the value given for <>, use <> instead 85 | securitySchemes: 86 | - oauth_2_0: 87 | description: | 88 | Dropbox supports OAuth 2.0 for authenticating all API requests. 89 | type: OAuth 2.0 90 | describedBy: 91 | headers: 92 | Authorization: 93 | description: | 94 | Used to send a valid OAuth 2 access token. Do not use 95 | with the "access_token" query string parameter. 96 | type: string 97 | queryParameters: 98 | access_token: 99 | description: | 100 | Used to send a valid OAuth 2 access token. Do not use together with 101 | the "Authorization" header 102 | type: string 103 | responses: 104 | 401: 105 | description: | 106 | Bad or expired token. This can happen if the user or Dropbox 107 | revoked or expired an access token. To fix, you should re- 108 | authenticate the user. 109 | 403: 110 | description: | 111 | Bad OAuth request (wrong consumer key, bad nonce, expired 112 | timestamp...). Unfortunately, re-authenticating the user won't help here. 113 | settings: 114 | authorizationUri: https://www.dropbox.com/1/oauth2/authorize 115 | accessTokenUri: https://api.dropbox.com/1/oauth2/token 116 | authorizationGrants: [ code, token ] 117 | - oauth_1_0: 118 | description: | 119 | OAuth 1.0 continues to be supported for all API requests, but OAuth 2.0 is now preferred. 120 | type: OAuth 1.0 121 | settings: 122 | requestTokenUri: https://api.dropbox.com/1/oauth/request_token 123 | authorizationUri: https://www.dropbox.com/1/oauth/authorize 124 | tokenCredentialsUri: https://api.dropbox.com/1/oauth/access_token 125 | - customHeader: 126 | description: | 127 | A custom 128 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/raml/validator.go: -------------------------------------------------------------------------------- 1 | // Copyright 2014 DoAT. All rights reserved. 2 | // 3 | // Redistribution and use in source and binary forms, with or without modification, 4 | // are permitted provided that the following conditions are met: 5 | // 6 | // 1. Redistributions of source code must retain the above copyright notice, 7 | // this list of conditions and the following disclaimer. 8 | // 9 | // 2. Redistributions in binary form must reproduce the above copyright notice, 10 | // this list of conditions and the following disclaimer in the documentation and/or 11 | // other materials provided with the distribution. 12 | // 13 | // THIS SOFTWARE IS PROVIDED “AS IS” WITHOUT ANY WARRANTIES WHATSOEVER. 14 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 15 | // THE IMPLIED WARRANTIES OF NON INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A 16 | // PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL DoAT OR CONTRIBUTORS 17 | // BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 | // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 20 | // // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 21 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 22 | // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 | // 24 | // The views and conclusions contained in the software and documentation are those of 25 | // the authors and should not be interpreted as representing official policies, 26 | // either expressed or implied, of DoAT. 27 | 28 | package raml 29 | 30 | // This file contains all of the RAML schema validator related code. 31 | 32 | // TODO: Inspirations: 33 | // https://www.npmjs.com/package/raml-validate 34 | // https://github.com/go-validator/validator 35 | // https://github.com/asaskevich/govalidator 36 | 37 | // And of course: 38 | // https://github.com/raml-org/raml-java-parser/tree/master/src/main/java/org/raml/parser/rule 39 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/yaml/CONTRIBUTORS: -------------------------------------------------------------------------------- 1 | go-yaml Contributor List (ordered by date): 2 | 3 | Gustavo Niemeyer 4 | Dave Cheney 5 | John Arbash Meinel [https://github.com/jameinel] 6 | Roger Peppe [https://github.com/rogpeppe] 7 | Abel Deuring [https://github.com/adeuring] 8 | Jordan Liggitt [https://github.com/liggitt] 9 | Alon Diamant 10 | 11 | All contributors that provided patches. 12 | If they are missing, please update this file. 13 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/yaml/LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Copyright (c) 2011-2014 - Canonical Inc. 3 | 4 | This software is licensed under the LGPLv3, included below. 5 | 6 | As a special exception to the GNU Lesser General Public License version 3 7 | ("LGPL3"), the copyright holders of this Library give you permission to 8 | convey to a third party a Combined Work that links statically or dynamically 9 | to this Library without providing any Minimal Corresponding Source or 10 | Minimal Application Code as set out in 4d or providing the installation 11 | information set out in section 4e, provided that you comply with the other 12 | provisions of LGPL3 and provided that you meet, for the Application the 13 | terms and conditions of the license(s) which apply to the Application. 14 | 15 | Except as stated in this special exception, the provisions of LGPL3 will 16 | continue to comply in full to this Library. If you modify this Library, you 17 | may apply this exception to your version of this Library, but you are not 18 | obliged to do so. If you do not wish to do so, delete this exception 19 | statement from your version. This exception does not (and cannot) modify any 20 | license terms which apply to the Application, with which you must still 21 | comply. 22 | 23 | 24 | GNU LESSER GENERAL PUBLIC LICENSE 25 | Version 3, 29 June 2007 26 | 27 | Copyright (C) 2007 Free Software Foundation, Inc. 28 | Everyone is permitted to copy and distribute verbatim copies 29 | of this license document, but changing it is not allowed. 30 | 31 | 32 | This version of the GNU Lesser General Public License incorporates 33 | the terms and conditions of version 3 of the GNU General Public 34 | License, supplemented by the additional permissions listed below. 35 | 36 | 0. Additional Definitions. 37 | 38 | As used herein, "this License" refers to version 3 of the GNU Lesser 39 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 40 | General Public License. 41 | 42 | "The Library" refers to a covered work governed by this License, 43 | other than an Application or a Combined Work as defined below. 44 | 45 | An "Application" is any work that makes use of an interface provided 46 | by the Library, but which is not otherwise based on the Library. 47 | Defining a subclass of a class defined by the Library is deemed a mode 48 | of using an interface provided by the Library. 49 | 50 | A "Combined Work" is a work produced by combining or linking an 51 | Application with the Library. The particular version of the Library 52 | with which the Combined Work was made is also called the "Linked 53 | Version". 54 | 55 | The "Minimal Corresponding Source" for a Combined Work means the 56 | Corresponding Source for the Combined Work, excluding any source code 57 | for portions of the Combined Work that, considered in isolation, are 58 | based on the Application, and not on the Linked Version. 59 | 60 | The "Corresponding Application Code" for a Combined Work means the 61 | object code and/or source code for the Application, including any data 62 | and utility programs needed for reproducing the Combined Work from the 63 | Application, but excluding the System Libraries of the Combined Work. 64 | 65 | 1. Exception to Section 3 of the GNU GPL. 66 | 67 | You may convey a covered work under sections 3 and 4 of this License 68 | without being bound by section 3 of the GNU GPL. 69 | 70 | 2. Conveying Modified Versions. 71 | 72 | If you modify a copy of the Library, and, in your modifications, a 73 | facility refers to a function or data to be supplied by an Application 74 | that uses the facility (other than as an argument passed when the 75 | facility is invoked), then you may convey a copy of the modified 76 | version: 77 | 78 | a) under this License, provided that you make a good faith effort to 79 | ensure that, in the event an Application does not supply the 80 | function or data, the facility still operates, and performs 81 | whatever part of its purpose remains meaningful, or 82 | 83 | b) under the GNU GPL, with none of the additional permissions of 84 | this License applicable to that copy. 85 | 86 | 3. Object Code Incorporating Material from Library Header Files. 87 | 88 | The object code form of an Application may incorporate material from 89 | a header file that is part of the Library. You may convey such object 90 | code under terms of your choice, provided that, if the incorporated 91 | material is not limited to numerical parameters, data structure 92 | layouts and accessors, or small macros, inline functions and templates 93 | (ten or fewer lines in length), you do both of the following: 94 | 95 | a) Give prominent notice with each copy of the object code that the 96 | Library is used in it and that the Library and its use are 97 | covered by this License. 98 | 99 | b) Accompany the object code with a copy of the GNU GPL and this license 100 | document. 101 | 102 | 4. Combined Works. 103 | 104 | You may convey a Combined Work under terms of your choice that, 105 | taken together, effectively do not restrict modification of the 106 | portions of the Library contained in the Combined Work and reverse 107 | engineering for debugging such modifications, if you also do each of 108 | the following: 109 | 110 | a) Give prominent notice with each copy of the Combined Work that 111 | the Library is used in it and that the Library and its use are 112 | covered by this License. 113 | 114 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 115 | document. 116 | 117 | c) For a Combined Work that displays copyright notices during 118 | execution, include the copyright notice for the Library among 119 | these notices, as well as a reference directing the user to the 120 | copies of the GNU GPL and this license document. 121 | 122 | d) Do one of the following: 123 | 124 | 0) Convey the Minimal Corresponding Source under the terms of this 125 | License, and the Corresponding Application Code in a form 126 | suitable for, and under terms that permit, the user to 127 | recombine or relink the Application with a modified version of 128 | the Linked Version to produce a modified Combined Work, in the 129 | manner specified by section 6 of the GNU GPL for conveying 130 | Corresponding Source. 131 | 132 | 1) Use a suitable shared library mechanism for linking with the 133 | Library. A suitable mechanism is one that (a) uses at run time 134 | a copy of the Library already present on the user's computer 135 | system, and (b) will operate properly with a modified version 136 | of the Library that is interface-compatible with the Linked 137 | Version. 138 | 139 | e) Provide Installation Information, but only if you would otherwise 140 | be required to provide such information under section 6 of the 141 | GNU GPL, and only to the extent that such information is 142 | necessary to install and execute a modified version of the 143 | Combined Work produced by recombining or relinking the 144 | Application with a modified version of the Linked Version. (If 145 | you use option 4d0, the Installation Information must accompany 146 | the Minimal Corresponding Source and Corresponding Application 147 | Code. If you use option 4d1, you must provide the Installation 148 | Information in the manner specified by section 6 of the GNU GPL 149 | for conveying Corresponding Source.) 150 | 151 | 5. Combined Libraries. 152 | 153 | You may place library facilities that are a work based on the 154 | Library side by side in a single library together with other library 155 | facilities that are not Applications and are not covered by this 156 | License, and convey such a combined library under terms of your 157 | choice, if you do both of the following: 158 | 159 | a) Accompany the combined library with a copy of the same work based 160 | on the Library, uncombined with any other library facilities, 161 | conveyed under the terms of this License. 162 | 163 | b) Give prominent notice with the combined library that part of it 164 | is a work based on the Library, and explaining where to find the 165 | accompanying uncombined form of the same work. 166 | 167 | 6. Revised Versions of the GNU Lesser General Public License. 168 | 169 | The Free Software Foundation may publish revised and/or new versions 170 | of the GNU Lesser General Public License from time to time. Such new 171 | versions will be similar in spirit to the present version, but may 172 | differ in detail to address new problems or concerns. 173 | 174 | Each version is given a distinguishing version number. If the 175 | Library as you received it specifies that a certain numbered version 176 | of the GNU Lesser General Public License "or any later version" 177 | applies to it, you have the option of following the terms and 178 | conditions either of that published version or of any later version 179 | published by the Free Software Foundation. If the Library as you 180 | received it does not specify a version number of the GNU Lesser 181 | General Public License, you may choose any version of the GNU Lesser 182 | General Public License ever published by the Free Software Foundation. 183 | 184 | If the Library as you received it specifies that a proxy can decide 185 | whether future versions of the GNU Lesser General Public License shall 186 | apply, that proxy's public statement of acceptance of any version is 187 | permanent authorization for you to choose that version for the 188 | Library. 189 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/yaml/LICENSE.libyaml: -------------------------------------------------------------------------------- 1 | The following files were ported to Go from C files of libyaml, and thus 2 | are still covered by their original copyright and license: 3 | 4 | apic.go 5 | emitterc.go 6 | parserc.go 7 | readerc.go 8 | scannerc.go 9 | writerc.go 10 | yamlh.go 11 | yamlprivateh.go 12 | 13 | Copyright (c) 2006 Kirill Simonov 14 | 15 | Permission is hereby granted, free of charge, to any person obtaining a copy of 16 | this software and associated documentation files (the "Software"), to deal in 17 | the Software without restriction, including without limitation the rights to 18 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 19 | of the Software, and to permit persons to whom the Software is furnished to do 20 | so, subject to the following conditions: 21 | 22 | The above copyright notice and this permission notice shall be included in all 23 | copies or substantial portions of the Software. 24 | 25 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 26 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 27 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 28 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 29 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 30 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 31 | SOFTWARE. 32 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/yaml/README.md: -------------------------------------------------------------------------------- 1 | # YAML support for the Go language 2 | 3 | Introduction 4 | ------------ 5 | 6 | The yaml package enables Go programs to comfortably encode and decode YAML 7 | values. It was developed within [Canonical](https://www.canonical.com) as 8 | part of the [juju](https://juju.ubuntu.com) project, and is based on a 9 | pure Go port of the well-known [libyaml](http://pyyaml.org/wiki/LibYAML) 10 | C library to parse and generate YAML data quickly and reliably. 11 | 12 | Compatibility 13 | ------------- 14 | 15 | The yaml package supports most of YAML 1.1 and 1.2, including support for 16 | anchors, tags, map merging, etc. Multi-document unmarshalling is not yet 17 | implemented, and base-60 floats from YAML 1.1 are purposefully not 18 | supported since they're a poor design and are gone in YAML 1.2. 19 | 20 | Installation and usage 21 | ---------------------- 22 | 23 | The import path for the package is *gopkg.in/yaml.v2*. 24 | 25 | To install it, run: 26 | 27 | go get gopkg.in/yaml.v2 28 | 29 | API documentation 30 | ----------------- 31 | 32 | If opened in a browser, the import path itself leads to the API documentation: 33 | 34 | * [https://gopkg.in/yaml.v2](https://gopkg.in/yaml.v2) 35 | 36 | Additional information is available in the source files, specifically yaml.go. 37 | 38 | API stability 39 | ------------- 40 | 41 | The package API for yaml v2 will remain stable as described in [gopkg.in](https://gopkg.in). 42 | 43 | 44 | License 45 | ------- 46 | 47 | The yaml package is licensed under the LGPL with an exception that allows it to be linked statically. Please see the LICENSE file for details. 48 | 49 | 50 | Example 51 | ------- 52 | 53 | ```Go 54 | package main 55 | 56 | import ( 57 | "fmt" 58 | "log" 59 | 60 | "gopkg.in/yaml.v2" 61 | ) 62 | 63 | var data = ` 64 | a: Easy! 65 | b: 66 | c: 2 67 | d: [3, 4] 68 | ` 69 | 70 | type T struct { 71 | A string 72 | B struct{C int; D []int `yaml:",flow"`} 73 | } 74 | 75 | func main() { 76 | t := T{} 77 | 78 | err := yaml.Unmarshal([]byte(data), &t) 79 | if err != nil { 80 | log.Fatalf("error: %v", err) 81 | } 82 | fmt.Printf("--- t:\n%v\n\n", t) 83 | 84 | d, err := yaml.Marshal(&t) 85 | if err != nil { 86 | log.Fatalf("error: %v", err) 87 | } 88 | fmt.Printf("--- t dump:\n%s\n\n", string(d)) 89 | 90 | m := make(map[interface{}]interface{}) 91 | 92 | err = yaml.Unmarshal([]byte(data), &m) 93 | if err != nil { 94 | log.Fatalf("error: %v", err) 95 | } 96 | fmt.Printf("--- m:\n%v\n\n", m) 97 | 98 | d, err = yaml.Marshal(&m) 99 | if err != nil { 100 | log.Fatalf("error: %v", err) 101 | } 102 | fmt.Printf("--- m dump:\n%s\n\n", string(d)) 103 | } 104 | ``` 105 | 106 | This example will generate the following output: 107 | 108 | ``` 109 | --- t: 110 | {Easy! {2 [3 4]}} 111 | 112 | --- t dump: 113 | a: Easy! 114 | b: 115 | c: 2 116 | d: [3, 4] 117 | 118 | 119 | --- m: 120 | map[a:Easy! b:map[c:2 d:[3 4]]] 121 | 122 | --- m dump: 123 | a: Easy! 124 | b: 125 | c: 2 126 | d: 127 | - 3 128 | - 4 129 | ``` 130 | 131 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/yaml/apic.go: -------------------------------------------------------------------------------- 1 | package yaml 2 | 3 | import ( 4 | "io" 5 | "os" 6 | ) 7 | 8 | func yaml_insert_token(parser *yaml_parser_t, pos int, token *yaml_token_t) { 9 | //fmt.Println("yaml_insert_token", "pos:", pos, "typ:", token.typ, "head:", parser.tokens_head, "len:", len(parser.tokens)) 10 | 11 | // Check if we can move the queue at the beginning of the buffer. 12 | if parser.tokens_head > 0 && len(parser.tokens) == cap(parser.tokens) { 13 | if parser.tokens_head != len(parser.tokens) { 14 | copy(parser.tokens, parser.tokens[parser.tokens_head:]) 15 | } 16 | parser.tokens = parser.tokens[:len(parser.tokens)-parser.tokens_head] 17 | parser.tokens_head = 0 18 | } 19 | parser.tokens = append(parser.tokens, *token) 20 | if pos < 0 { 21 | return 22 | } 23 | copy(parser.tokens[parser.tokens_head+pos+1:], parser.tokens[parser.tokens_head+pos:]) 24 | parser.tokens[parser.tokens_head+pos] = *token 25 | } 26 | 27 | // Create a new parser object. 28 | func yaml_parser_initialize(parser *yaml_parser_t) bool { 29 | *parser = yaml_parser_t{ 30 | raw_buffer: make([]byte, 0, input_raw_buffer_size), 31 | buffer: make([]byte, 0, input_buffer_size), 32 | } 33 | return true 34 | } 35 | 36 | // Destroy a parser object. 37 | func yaml_parser_delete(parser *yaml_parser_t) { 38 | *parser = yaml_parser_t{} 39 | } 40 | 41 | // String read handler. 42 | func yaml_string_read_handler(parser *yaml_parser_t, buffer []byte) (n int, err error) { 43 | if parser.input_pos == len(parser.input) { 44 | return 0, io.EOF 45 | } 46 | n = copy(buffer, parser.input[parser.input_pos:]) 47 | parser.input_pos += n 48 | return n, nil 49 | } 50 | 51 | // File read handler. 52 | func yaml_file_read_handler(parser *yaml_parser_t, buffer []byte) (n int, err error) { 53 | return parser.input_file.Read(buffer) 54 | } 55 | 56 | // Set a string input. 57 | func yaml_parser_set_input_string(parser *yaml_parser_t, input []byte) { 58 | if parser.read_handler != nil { 59 | panic("must set the input source only once") 60 | } 61 | parser.read_handler = yaml_string_read_handler 62 | parser.input = input 63 | parser.input_pos = 0 64 | } 65 | 66 | // Set a file input. 67 | func yaml_parser_set_input_file(parser *yaml_parser_t, file *os.File) { 68 | if parser.read_handler != nil { 69 | panic("must set the input source only once") 70 | } 71 | parser.read_handler = yaml_file_read_handler 72 | parser.input_file = file 73 | } 74 | 75 | // Set the source encoding. 76 | func yaml_parser_set_encoding(parser *yaml_parser_t, encoding yaml_encoding_t) { 77 | if parser.encoding != yaml_ANY_ENCODING { 78 | panic("must set the encoding only once") 79 | } 80 | parser.encoding = encoding 81 | } 82 | 83 | // Create a new emitter object. 84 | func yaml_emitter_initialize(emitter *yaml_emitter_t) bool { 85 | *emitter = yaml_emitter_t{ 86 | buffer: make([]byte, output_buffer_size), 87 | raw_buffer: make([]byte, 0, output_raw_buffer_size), 88 | states: make([]yaml_emitter_state_t, 0, initial_stack_size), 89 | events: make([]yaml_event_t, 0, initial_queue_size), 90 | } 91 | return true 92 | } 93 | 94 | // Destroy an emitter object. 95 | func yaml_emitter_delete(emitter *yaml_emitter_t) { 96 | *emitter = yaml_emitter_t{} 97 | } 98 | 99 | // String write handler. 100 | func yaml_string_write_handler(emitter *yaml_emitter_t, buffer []byte) error { 101 | *emitter.output_buffer = append(*emitter.output_buffer, buffer...) 102 | return nil 103 | } 104 | 105 | // File write handler. 106 | func yaml_file_write_handler(emitter *yaml_emitter_t, buffer []byte) error { 107 | _, err := emitter.output_file.Write(buffer) 108 | return err 109 | } 110 | 111 | // Set a string output. 112 | func yaml_emitter_set_output_string(emitter *yaml_emitter_t, output_buffer *[]byte) { 113 | if emitter.write_handler != nil { 114 | panic("must set the output target only once") 115 | } 116 | emitter.write_handler = yaml_string_write_handler 117 | emitter.output_buffer = output_buffer 118 | } 119 | 120 | // Set a file output. 121 | func yaml_emitter_set_output_file(emitter *yaml_emitter_t, file io.Writer) { 122 | if emitter.write_handler != nil { 123 | panic("must set the output target only once") 124 | } 125 | emitter.write_handler = yaml_file_write_handler 126 | emitter.output_file = file 127 | } 128 | 129 | // Set the output encoding. 130 | func yaml_emitter_set_encoding(emitter *yaml_emitter_t, encoding yaml_encoding_t) { 131 | if emitter.encoding != yaml_ANY_ENCODING { 132 | panic("must set the output encoding only once") 133 | } 134 | emitter.encoding = encoding 135 | } 136 | 137 | // Set the canonical output style. 138 | func yaml_emitter_set_canonical(emitter *yaml_emitter_t, canonical bool) { 139 | emitter.canonical = canonical 140 | } 141 | 142 | //// Set the indentation increment. 143 | func yaml_emitter_set_indent(emitter *yaml_emitter_t, indent int) { 144 | if indent < 2 || indent > 9 { 145 | indent = 2 146 | } 147 | emitter.best_indent = indent 148 | } 149 | 150 | // Set the preferred line width. 151 | func yaml_emitter_set_width(emitter *yaml_emitter_t, width int) { 152 | if width < 0 { 153 | width = -1 154 | } 155 | emitter.best_width = width 156 | } 157 | 158 | // Set if unescaped non-ASCII characters are allowed. 159 | func yaml_emitter_set_unicode(emitter *yaml_emitter_t, unicode bool) { 160 | emitter.unicode = unicode 161 | } 162 | 163 | // Set the preferred line break character. 164 | func yaml_emitter_set_break(emitter *yaml_emitter_t, line_break yaml_break_t) { 165 | emitter.line_break = line_break 166 | } 167 | 168 | ///* 169 | // * Destroy a token object. 170 | // */ 171 | // 172 | //YAML_DECLARE(void) 173 | //yaml_token_delete(yaml_token_t *token) 174 | //{ 175 | // assert(token); // Non-NULL token object expected. 176 | // 177 | // switch (token.type) 178 | // { 179 | // case YAML_TAG_DIRECTIVE_TOKEN: 180 | // yaml_free(token.data.tag_directive.handle); 181 | // yaml_free(token.data.tag_directive.prefix); 182 | // break; 183 | // 184 | // case YAML_ALIAS_TOKEN: 185 | // yaml_free(token.data.alias.value); 186 | // break; 187 | // 188 | // case YAML_ANCHOR_TOKEN: 189 | // yaml_free(token.data.anchor.value); 190 | // break; 191 | // 192 | // case YAML_TAG_TOKEN: 193 | // yaml_free(token.data.tag.handle); 194 | // yaml_free(token.data.tag.suffix); 195 | // break; 196 | // 197 | // case YAML_SCALAR_TOKEN: 198 | // yaml_free(token.data.scalar.value); 199 | // break; 200 | // 201 | // default: 202 | // break; 203 | // } 204 | // 205 | // memset(token, 0, sizeof(yaml_token_t)); 206 | //} 207 | // 208 | ///* 209 | // * Check if a string is a valid UTF-8 sequence. 210 | // * 211 | // * Check 'reader.c' for more details on UTF-8 encoding. 212 | // */ 213 | // 214 | //static int 215 | //yaml_check_utf8(yaml_char_t *start, size_t length) 216 | //{ 217 | // yaml_char_t *end = start+length; 218 | // yaml_char_t *pointer = start; 219 | // 220 | // while (pointer < end) { 221 | // unsigned char octet; 222 | // unsigned int width; 223 | // unsigned int value; 224 | // size_t k; 225 | // 226 | // octet = pointer[0]; 227 | // width = (octet & 0x80) == 0x00 ? 1 : 228 | // (octet & 0xE0) == 0xC0 ? 2 : 229 | // (octet & 0xF0) == 0xE0 ? 3 : 230 | // (octet & 0xF8) == 0xF0 ? 4 : 0; 231 | // value = (octet & 0x80) == 0x00 ? octet & 0x7F : 232 | // (octet & 0xE0) == 0xC0 ? octet & 0x1F : 233 | // (octet & 0xF0) == 0xE0 ? octet & 0x0F : 234 | // (octet & 0xF8) == 0xF0 ? octet & 0x07 : 0; 235 | // if (!width) return 0; 236 | // if (pointer+width > end) return 0; 237 | // for (k = 1; k < width; k ++) { 238 | // octet = pointer[k]; 239 | // if ((octet & 0xC0) != 0x80) return 0; 240 | // value = (value << 6) + (octet & 0x3F); 241 | // } 242 | // if (!((width == 1) || 243 | // (width == 2 && value >= 0x80) || 244 | // (width == 3 && value >= 0x800) || 245 | // (width == 4 && value >= 0x10000))) return 0; 246 | // 247 | // pointer += width; 248 | // } 249 | // 250 | // return 1; 251 | //} 252 | // 253 | 254 | // Create STREAM-START. 255 | func yaml_stream_start_event_initialize(event *yaml_event_t, encoding yaml_encoding_t) bool { 256 | *event = yaml_event_t{ 257 | typ: yaml_STREAM_START_EVENT, 258 | encoding: encoding, 259 | } 260 | return true 261 | } 262 | 263 | // Create STREAM-END. 264 | func yaml_stream_end_event_initialize(event *yaml_event_t) bool { 265 | *event = yaml_event_t{ 266 | typ: yaml_STREAM_END_EVENT, 267 | } 268 | return true 269 | } 270 | 271 | // Create DOCUMENT-START. 272 | func yaml_document_start_event_initialize(event *yaml_event_t, version_directive *yaml_version_directive_t, 273 | tag_directives []yaml_tag_directive_t, implicit bool) bool { 274 | *event = yaml_event_t{ 275 | typ: yaml_DOCUMENT_START_EVENT, 276 | version_directive: version_directive, 277 | tag_directives: tag_directives, 278 | implicit: implicit, 279 | } 280 | return true 281 | } 282 | 283 | // Create DOCUMENT-END. 284 | func yaml_document_end_event_initialize(event *yaml_event_t, implicit bool) bool { 285 | *event = yaml_event_t{ 286 | typ: yaml_DOCUMENT_END_EVENT, 287 | implicit: implicit, 288 | } 289 | return true 290 | } 291 | 292 | ///* 293 | // * Create ALIAS. 294 | // */ 295 | // 296 | //YAML_DECLARE(int) 297 | //yaml_alias_event_initialize(event *yaml_event_t, anchor *yaml_char_t) 298 | //{ 299 | // mark yaml_mark_t = { 0, 0, 0 } 300 | // anchor_copy *yaml_char_t = NULL 301 | // 302 | // assert(event) // Non-NULL event object is expected. 303 | // assert(anchor) // Non-NULL anchor is expected. 304 | // 305 | // if (!yaml_check_utf8(anchor, strlen((char *)anchor))) return 0 306 | // 307 | // anchor_copy = yaml_strdup(anchor) 308 | // if (!anchor_copy) 309 | // return 0 310 | // 311 | // ALIAS_EVENT_INIT(*event, anchor_copy, mark, mark) 312 | // 313 | // return 1 314 | //} 315 | 316 | // Create SCALAR. 317 | func yaml_scalar_event_initialize(event *yaml_event_t, anchor, tag, value []byte, plain_implicit, quoted_implicit bool, style yaml_scalar_style_t) bool { 318 | *event = yaml_event_t{ 319 | typ: yaml_SCALAR_EVENT, 320 | anchor: anchor, 321 | tag: tag, 322 | value: value, 323 | implicit: plain_implicit, 324 | quoted_implicit: quoted_implicit, 325 | style: yaml_style_t(style), 326 | } 327 | return true 328 | } 329 | 330 | // Create SEQUENCE-START. 331 | func yaml_sequence_start_event_initialize(event *yaml_event_t, anchor, tag []byte, implicit bool, style yaml_sequence_style_t) bool { 332 | *event = yaml_event_t{ 333 | typ: yaml_SEQUENCE_START_EVENT, 334 | anchor: anchor, 335 | tag: tag, 336 | implicit: implicit, 337 | style: yaml_style_t(style), 338 | } 339 | return true 340 | } 341 | 342 | // Create SEQUENCE-END. 343 | func yaml_sequence_end_event_initialize(event *yaml_event_t) bool { 344 | *event = yaml_event_t{ 345 | typ: yaml_SEQUENCE_END_EVENT, 346 | } 347 | return true 348 | } 349 | 350 | // Create MAPPING-START. 351 | func yaml_mapping_start_event_initialize(event *yaml_event_t, anchor, tag []byte, implicit bool, style yaml_mapping_style_t) bool { 352 | *event = yaml_event_t{ 353 | typ: yaml_MAPPING_START_EVENT, 354 | anchor: anchor, 355 | tag: tag, 356 | implicit: implicit, 357 | style: yaml_style_t(style), 358 | } 359 | return true 360 | } 361 | 362 | // Create MAPPING-END. 363 | func yaml_mapping_end_event_initialize(event *yaml_event_t) bool { 364 | *event = yaml_event_t{ 365 | typ: yaml_MAPPING_END_EVENT, 366 | } 367 | return true 368 | } 369 | 370 | // Destroy an event object. 371 | func yaml_event_delete(event *yaml_event_t) { 372 | *event = yaml_event_t{} 373 | } 374 | 375 | ///* 376 | // * Create a document object. 377 | // */ 378 | // 379 | //YAML_DECLARE(int) 380 | //yaml_document_initialize(document *yaml_document_t, 381 | // version_directive *yaml_version_directive_t, 382 | // tag_directives_start *yaml_tag_directive_t, 383 | // tag_directives_end *yaml_tag_directive_t, 384 | // start_implicit int, end_implicit int) 385 | //{ 386 | // struct { 387 | // error yaml_error_type_t 388 | // } context 389 | // struct { 390 | // start *yaml_node_t 391 | // end *yaml_node_t 392 | // top *yaml_node_t 393 | // } nodes = { NULL, NULL, NULL } 394 | // version_directive_copy *yaml_version_directive_t = NULL 395 | // struct { 396 | // start *yaml_tag_directive_t 397 | // end *yaml_tag_directive_t 398 | // top *yaml_tag_directive_t 399 | // } tag_directives_copy = { NULL, NULL, NULL } 400 | // value yaml_tag_directive_t = { NULL, NULL } 401 | // mark yaml_mark_t = { 0, 0, 0 } 402 | // 403 | // assert(document) // Non-NULL document object is expected. 404 | // assert((tag_directives_start && tag_directives_end) || 405 | // (tag_directives_start == tag_directives_end)) 406 | // // Valid tag directives are expected. 407 | // 408 | // if (!STACK_INIT(&context, nodes, INITIAL_STACK_SIZE)) goto error 409 | // 410 | // if (version_directive) { 411 | // version_directive_copy = yaml_malloc(sizeof(yaml_version_directive_t)) 412 | // if (!version_directive_copy) goto error 413 | // version_directive_copy.major = version_directive.major 414 | // version_directive_copy.minor = version_directive.minor 415 | // } 416 | // 417 | // if (tag_directives_start != tag_directives_end) { 418 | // tag_directive *yaml_tag_directive_t 419 | // if (!STACK_INIT(&context, tag_directives_copy, INITIAL_STACK_SIZE)) 420 | // goto error 421 | // for (tag_directive = tag_directives_start 422 | // tag_directive != tag_directives_end; tag_directive ++) { 423 | // assert(tag_directive.handle) 424 | // assert(tag_directive.prefix) 425 | // if (!yaml_check_utf8(tag_directive.handle, 426 | // strlen((char *)tag_directive.handle))) 427 | // goto error 428 | // if (!yaml_check_utf8(tag_directive.prefix, 429 | // strlen((char *)tag_directive.prefix))) 430 | // goto error 431 | // value.handle = yaml_strdup(tag_directive.handle) 432 | // value.prefix = yaml_strdup(tag_directive.prefix) 433 | // if (!value.handle || !value.prefix) goto error 434 | // if (!PUSH(&context, tag_directives_copy, value)) 435 | // goto error 436 | // value.handle = NULL 437 | // value.prefix = NULL 438 | // } 439 | // } 440 | // 441 | // DOCUMENT_INIT(*document, nodes.start, nodes.end, version_directive_copy, 442 | // tag_directives_copy.start, tag_directives_copy.top, 443 | // start_implicit, end_implicit, mark, mark) 444 | // 445 | // return 1 446 | // 447 | //error: 448 | // STACK_DEL(&context, nodes) 449 | // yaml_free(version_directive_copy) 450 | // while (!STACK_EMPTY(&context, tag_directives_copy)) { 451 | // value yaml_tag_directive_t = POP(&context, tag_directives_copy) 452 | // yaml_free(value.handle) 453 | // yaml_free(value.prefix) 454 | // } 455 | // STACK_DEL(&context, tag_directives_copy) 456 | // yaml_free(value.handle) 457 | // yaml_free(value.prefix) 458 | // 459 | // return 0 460 | //} 461 | // 462 | ///* 463 | // * Destroy a document object. 464 | // */ 465 | // 466 | //YAML_DECLARE(void) 467 | //yaml_document_delete(document *yaml_document_t) 468 | //{ 469 | // struct { 470 | // error yaml_error_type_t 471 | // } context 472 | // tag_directive *yaml_tag_directive_t 473 | // 474 | // context.error = YAML_NO_ERROR // Eliminate a compliler warning. 475 | // 476 | // assert(document) // Non-NULL document object is expected. 477 | // 478 | // while (!STACK_EMPTY(&context, document.nodes)) { 479 | // node yaml_node_t = POP(&context, document.nodes) 480 | // yaml_free(node.tag) 481 | // switch (node.type) { 482 | // case YAML_SCALAR_NODE: 483 | // yaml_free(node.data.scalar.value) 484 | // break 485 | // case YAML_SEQUENCE_NODE: 486 | // STACK_DEL(&context, node.data.sequence.items) 487 | // break 488 | // case YAML_MAPPING_NODE: 489 | // STACK_DEL(&context, node.data.mapping.pairs) 490 | // break 491 | // default: 492 | // assert(0) // Should not happen. 493 | // } 494 | // } 495 | // STACK_DEL(&context, document.nodes) 496 | // 497 | // yaml_free(document.version_directive) 498 | // for (tag_directive = document.tag_directives.start 499 | // tag_directive != document.tag_directives.end 500 | // tag_directive++) { 501 | // yaml_free(tag_directive.handle) 502 | // yaml_free(tag_directive.prefix) 503 | // } 504 | // yaml_free(document.tag_directives.start) 505 | // 506 | // memset(document, 0, sizeof(yaml_document_t)) 507 | //} 508 | // 509 | ///** 510 | // * Get a document node. 511 | // */ 512 | // 513 | //YAML_DECLARE(yaml_node_t *) 514 | //yaml_document_get_node(document *yaml_document_t, index int) 515 | //{ 516 | // assert(document) // Non-NULL document object is expected. 517 | // 518 | // if (index > 0 && document.nodes.start + index <= document.nodes.top) { 519 | // return document.nodes.start + index - 1 520 | // } 521 | // return NULL 522 | //} 523 | // 524 | ///** 525 | // * Get the root object. 526 | // */ 527 | // 528 | //YAML_DECLARE(yaml_node_t *) 529 | //yaml_document_get_root_node(document *yaml_document_t) 530 | //{ 531 | // assert(document) // Non-NULL document object is expected. 532 | // 533 | // if (document.nodes.top != document.nodes.start) { 534 | // return document.nodes.start 535 | // } 536 | // return NULL 537 | //} 538 | // 539 | ///* 540 | // * Add a scalar node to a document. 541 | // */ 542 | // 543 | //YAML_DECLARE(int) 544 | //yaml_document_add_scalar(document *yaml_document_t, 545 | // tag *yaml_char_t, value *yaml_char_t, length int, 546 | // style yaml_scalar_style_t) 547 | //{ 548 | // struct { 549 | // error yaml_error_type_t 550 | // } context 551 | // mark yaml_mark_t = { 0, 0, 0 } 552 | // tag_copy *yaml_char_t = NULL 553 | // value_copy *yaml_char_t = NULL 554 | // node yaml_node_t 555 | // 556 | // assert(document) // Non-NULL document object is expected. 557 | // assert(value) // Non-NULL value is expected. 558 | // 559 | // if (!tag) { 560 | // tag = (yaml_char_t *)YAML_DEFAULT_SCALAR_TAG 561 | // } 562 | // 563 | // if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error 564 | // tag_copy = yaml_strdup(tag) 565 | // if (!tag_copy) goto error 566 | // 567 | // if (length < 0) { 568 | // length = strlen((char *)value) 569 | // } 570 | // 571 | // if (!yaml_check_utf8(value, length)) goto error 572 | // value_copy = yaml_malloc(length+1) 573 | // if (!value_copy) goto error 574 | // memcpy(value_copy, value, length) 575 | // value_copy[length] = '\0' 576 | // 577 | // SCALAR_NODE_INIT(node, tag_copy, value_copy, length, style, mark, mark) 578 | // if (!PUSH(&context, document.nodes, node)) goto error 579 | // 580 | // return document.nodes.top - document.nodes.start 581 | // 582 | //error: 583 | // yaml_free(tag_copy) 584 | // yaml_free(value_copy) 585 | // 586 | // return 0 587 | //} 588 | // 589 | ///* 590 | // * Add a sequence node to a document. 591 | // */ 592 | // 593 | //YAML_DECLARE(int) 594 | //yaml_document_add_sequence(document *yaml_document_t, 595 | // tag *yaml_char_t, style yaml_sequence_style_t) 596 | //{ 597 | // struct { 598 | // error yaml_error_type_t 599 | // } context 600 | // mark yaml_mark_t = { 0, 0, 0 } 601 | // tag_copy *yaml_char_t = NULL 602 | // struct { 603 | // start *yaml_node_item_t 604 | // end *yaml_node_item_t 605 | // top *yaml_node_item_t 606 | // } items = { NULL, NULL, NULL } 607 | // node yaml_node_t 608 | // 609 | // assert(document) // Non-NULL document object is expected. 610 | // 611 | // if (!tag) { 612 | // tag = (yaml_char_t *)YAML_DEFAULT_SEQUENCE_TAG 613 | // } 614 | // 615 | // if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error 616 | // tag_copy = yaml_strdup(tag) 617 | // if (!tag_copy) goto error 618 | // 619 | // if (!STACK_INIT(&context, items, INITIAL_STACK_SIZE)) goto error 620 | // 621 | // SEQUENCE_NODE_INIT(node, tag_copy, items.start, items.end, 622 | // style, mark, mark) 623 | // if (!PUSH(&context, document.nodes, node)) goto error 624 | // 625 | // return document.nodes.top - document.nodes.start 626 | // 627 | //error: 628 | // STACK_DEL(&context, items) 629 | // yaml_free(tag_copy) 630 | // 631 | // return 0 632 | //} 633 | // 634 | ///* 635 | // * Add a mapping node to a document. 636 | // */ 637 | // 638 | //YAML_DECLARE(int) 639 | //yaml_document_add_mapping(document *yaml_document_t, 640 | // tag *yaml_char_t, style yaml_mapping_style_t) 641 | //{ 642 | // struct { 643 | // error yaml_error_type_t 644 | // } context 645 | // mark yaml_mark_t = { 0, 0, 0 } 646 | // tag_copy *yaml_char_t = NULL 647 | // struct { 648 | // start *yaml_node_pair_t 649 | // end *yaml_node_pair_t 650 | // top *yaml_node_pair_t 651 | // } pairs = { NULL, NULL, NULL } 652 | // node yaml_node_t 653 | // 654 | // assert(document) // Non-NULL document object is expected. 655 | // 656 | // if (!tag) { 657 | // tag = (yaml_char_t *)YAML_DEFAULT_MAPPING_TAG 658 | // } 659 | // 660 | // if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error 661 | // tag_copy = yaml_strdup(tag) 662 | // if (!tag_copy) goto error 663 | // 664 | // if (!STACK_INIT(&context, pairs, INITIAL_STACK_SIZE)) goto error 665 | // 666 | // MAPPING_NODE_INIT(node, tag_copy, pairs.start, pairs.end, 667 | // style, mark, mark) 668 | // if (!PUSH(&context, document.nodes, node)) goto error 669 | // 670 | // return document.nodes.top - document.nodes.start 671 | // 672 | //error: 673 | // STACK_DEL(&context, pairs) 674 | // yaml_free(tag_copy) 675 | // 676 | // return 0 677 | //} 678 | // 679 | ///* 680 | // * Append an item to a sequence node. 681 | // */ 682 | // 683 | //YAML_DECLARE(int) 684 | //yaml_document_append_sequence_item(document *yaml_document_t, 685 | // sequence int, item int) 686 | //{ 687 | // struct { 688 | // error yaml_error_type_t 689 | // } context 690 | // 691 | // assert(document) // Non-NULL document is required. 692 | // assert(sequence > 0 693 | // && document.nodes.start + sequence <= document.nodes.top) 694 | // // Valid sequence id is required. 695 | // assert(document.nodes.start[sequence-1].type == YAML_SEQUENCE_NODE) 696 | // // A sequence node is required. 697 | // assert(item > 0 && document.nodes.start + item <= document.nodes.top) 698 | // // Valid item id is required. 699 | // 700 | // if (!PUSH(&context, 701 | // document.nodes.start[sequence-1].data.sequence.items, item)) 702 | // return 0 703 | // 704 | // return 1 705 | //} 706 | // 707 | ///* 708 | // * Append a pair of a key and a value to a mapping node. 709 | // */ 710 | // 711 | //YAML_DECLARE(int) 712 | //yaml_document_append_mapping_pair(document *yaml_document_t, 713 | // mapping int, key int, value int) 714 | //{ 715 | // struct { 716 | // error yaml_error_type_t 717 | // } context 718 | // 719 | // pair yaml_node_pair_t 720 | // 721 | // assert(document) // Non-NULL document is required. 722 | // assert(mapping > 0 723 | // && document.nodes.start + mapping <= document.nodes.top) 724 | // // Valid mapping id is required. 725 | // assert(document.nodes.start[mapping-1].type == YAML_MAPPING_NODE) 726 | // // A mapping node is required. 727 | // assert(key > 0 && document.nodes.start + key <= document.nodes.top) 728 | // // Valid key id is required. 729 | // assert(value > 0 && document.nodes.start + value <= document.nodes.top) 730 | // // Valid value id is required. 731 | // 732 | // pair.key = key 733 | // pair.value = value 734 | // 735 | // if (!PUSH(&context, 736 | // document.nodes.start[mapping-1].data.mapping.pairs, pair)) 737 | // return 0 738 | // 739 | // return 1 740 | //} 741 | // 742 | // 743 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/yaml/encode.go: -------------------------------------------------------------------------------- 1 | package yaml 2 | 3 | import ( 4 | "encoding" 5 | "reflect" 6 | "regexp" 7 | "sort" 8 | "strconv" 9 | "strings" 10 | "time" 11 | ) 12 | 13 | type encoder struct { 14 | emitter yaml_emitter_t 15 | event yaml_event_t 16 | out []byte 17 | flow bool 18 | } 19 | 20 | func newEncoder() (e *encoder) { 21 | e = &encoder{} 22 | e.must(yaml_emitter_initialize(&e.emitter)) 23 | yaml_emitter_set_output_string(&e.emitter, &e.out) 24 | yaml_emitter_set_unicode(&e.emitter, true) 25 | e.must(yaml_stream_start_event_initialize(&e.event, yaml_UTF8_ENCODING)) 26 | e.emit() 27 | e.must(yaml_document_start_event_initialize(&e.event, nil, nil, true)) 28 | e.emit() 29 | return e 30 | } 31 | 32 | func (e *encoder) finish() { 33 | e.must(yaml_document_end_event_initialize(&e.event, true)) 34 | e.emit() 35 | e.emitter.open_ended = false 36 | e.must(yaml_stream_end_event_initialize(&e.event)) 37 | e.emit() 38 | } 39 | 40 | func (e *encoder) destroy() { 41 | yaml_emitter_delete(&e.emitter) 42 | } 43 | 44 | func (e *encoder) emit() { 45 | // This will internally delete the e.event value. 46 | if !yaml_emitter_emit(&e.emitter, &e.event) && e.event.typ != yaml_DOCUMENT_END_EVENT && e.event.typ != yaml_STREAM_END_EVENT { 47 | e.must(false) 48 | } 49 | } 50 | 51 | func (e *encoder) must(ok bool) { 52 | if !ok { 53 | msg := e.emitter.problem 54 | if msg == "" { 55 | msg = "unknown problem generating YAML content" 56 | } 57 | failf("%s", msg) 58 | } 59 | } 60 | 61 | func (e *encoder) marshal(tag string, in reflect.Value) { 62 | if !in.IsValid() { 63 | e.nilv() 64 | return 65 | } 66 | iface := in.Interface() 67 | if m, ok := iface.(Marshaler); ok { 68 | v, err := m.MarshalYAML() 69 | if err != nil { 70 | fail(err) 71 | } 72 | if v == nil { 73 | e.nilv() 74 | return 75 | } 76 | in = reflect.ValueOf(v) 77 | } 78 | if m, ok := iface.(encoding.TextMarshaler); ok { 79 | text, err := m.MarshalText() 80 | if err != nil { 81 | fail(err) 82 | } 83 | in = reflect.ValueOf(string(text)) 84 | } 85 | switch in.Kind() { 86 | case reflect.Interface: 87 | if in.IsNil() { 88 | e.nilv() 89 | } else { 90 | e.marshal(tag, in.Elem()) 91 | } 92 | case reflect.Map: 93 | e.mapv(tag, in) 94 | case reflect.Ptr: 95 | if in.IsNil() { 96 | e.nilv() 97 | } else { 98 | e.marshal(tag, in.Elem()) 99 | } 100 | case reflect.Struct: 101 | e.structv(tag, in) 102 | case reflect.Slice: 103 | if in.Type().Elem() == mapItemType { 104 | e.itemsv(tag, in) 105 | } else { 106 | e.slicev(tag, in) 107 | } 108 | case reflect.String: 109 | e.stringv(tag, in) 110 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 111 | if in.Type() == durationType { 112 | e.stringv(tag, reflect.ValueOf(iface.(time.Duration).String())) 113 | } else { 114 | e.intv(tag, in) 115 | } 116 | case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: 117 | e.uintv(tag, in) 118 | case reflect.Float32, reflect.Float64: 119 | e.floatv(tag, in) 120 | case reflect.Bool: 121 | e.boolv(tag, in) 122 | default: 123 | panic("cannot marshal type: " + in.Type().String()) 124 | } 125 | } 126 | 127 | func (e *encoder) mapv(tag string, in reflect.Value) { 128 | e.mappingv(tag, func() { 129 | keys := keyList(in.MapKeys()) 130 | sort.Sort(keys) 131 | for _, k := range keys { 132 | e.marshal("", k) 133 | e.marshal("", in.MapIndex(k)) 134 | } 135 | }) 136 | } 137 | 138 | func (e *encoder) itemsv(tag string, in reflect.Value) { 139 | e.mappingv(tag, func() { 140 | slice := in.Convert(reflect.TypeOf([]MapItem{})).Interface().([]MapItem) 141 | for _, item := range slice { 142 | e.marshal("", reflect.ValueOf(item.Key)) 143 | e.marshal("", reflect.ValueOf(item.Value)) 144 | } 145 | }) 146 | } 147 | 148 | func (e *encoder) structv(tag string, in reflect.Value) { 149 | sinfo, err := getStructInfo(in.Type()) 150 | if err != nil { 151 | panic(err) 152 | } 153 | e.mappingv(tag, func() { 154 | for _, info := range sinfo.FieldsList { 155 | var value reflect.Value 156 | if info.Inline == nil { 157 | value = in.Field(info.Num) 158 | } else { 159 | value = in.FieldByIndex(info.Inline) 160 | } 161 | if info.OmitEmpty && isZero(value) { 162 | continue 163 | } 164 | e.marshal("", reflect.ValueOf(info.Key)) 165 | e.flow = info.Flow 166 | e.marshal("", value) 167 | } 168 | }) 169 | } 170 | 171 | func (e *encoder) mappingv(tag string, f func()) { 172 | implicit := tag == "" 173 | style := yaml_BLOCK_MAPPING_STYLE 174 | if e.flow { 175 | e.flow = false 176 | style = yaml_FLOW_MAPPING_STYLE 177 | } 178 | e.must(yaml_mapping_start_event_initialize(&e.event, nil, []byte(tag), implicit, style)) 179 | e.emit() 180 | f() 181 | e.must(yaml_mapping_end_event_initialize(&e.event)) 182 | e.emit() 183 | } 184 | 185 | func (e *encoder) slicev(tag string, in reflect.Value) { 186 | implicit := tag == "" 187 | style := yaml_BLOCK_SEQUENCE_STYLE 188 | if e.flow { 189 | e.flow = false 190 | style = yaml_FLOW_SEQUENCE_STYLE 191 | } 192 | e.must(yaml_sequence_start_event_initialize(&e.event, nil, []byte(tag), implicit, style)) 193 | e.emit() 194 | n := in.Len() 195 | for i := 0; i < n; i++ { 196 | e.marshal("", in.Index(i)) 197 | } 198 | e.must(yaml_sequence_end_event_initialize(&e.event)) 199 | e.emit() 200 | } 201 | 202 | // isBase60 returns whether s is in base 60 notation as defined in YAML 1.1. 203 | // 204 | // The base 60 float notation in YAML 1.1 is a terrible idea and is unsupported 205 | // in YAML 1.2 and by this package, but these should be marshalled quoted for 206 | // the time being for compatibility with other parsers. 207 | func isBase60Float(s string) (result bool) { 208 | // Fast path. 209 | if s == "" { 210 | return false 211 | } 212 | c := s[0] 213 | if !(c == '+' || c == '-' || c >= '0' && c <= '9') || strings.IndexByte(s, ':') < 0 { 214 | return false 215 | } 216 | // Do the full match. 217 | return base60float.MatchString(s) 218 | } 219 | 220 | // From http://yaml.org/type/float.html, except the regular expression there 221 | // is bogus. In practice parsers do not enforce the "\.[0-9_]*" suffix. 222 | var base60float = regexp.MustCompile(`^[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+(?:\.[0-9_]*)?$`) 223 | 224 | func (e *encoder) stringv(tag string, in reflect.Value) { 225 | var style yaml_scalar_style_t 226 | s := in.String() 227 | rtag, rs := resolve("", s) 228 | if rtag == yaml_BINARY_TAG { 229 | if tag == "" || tag == yaml_STR_TAG { 230 | tag = rtag 231 | s = rs.(string) 232 | } else if tag == yaml_BINARY_TAG { 233 | failf("explicitly tagged !!binary data must be base64-encoded") 234 | } else { 235 | failf("cannot marshal invalid UTF-8 data as %s", shortTag(tag)) 236 | } 237 | } 238 | if tag == "" && (rtag != yaml_STR_TAG || isBase60Float(s)) { 239 | style = yaml_DOUBLE_QUOTED_SCALAR_STYLE 240 | } else if strings.Contains(s, "\n") { 241 | style = yaml_LITERAL_SCALAR_STYLE 242 | } else { 243 | style = yaml_PLAIN_SCALAR_STYLE 244 | } 245 | e.emitScalar(s, "", tag, style) 246 | } 247 | 248 | func (e *encoder) boolv(tag string, in reflect.Value) { 249 | var s string 250 | if in.Bool() { 251 | s = "true" 252 | } else { 253 | s = "false" 254 | } 255 | e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE) 256 | } 257 | 258 | func (e *encoder) intv(tag string, in reflect.Value) { 259 | s := strconv.FormatInt(in.Int(), 10) 260 | e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE) 261 | } 262 | 263 | func (e *encoder) uintv(tag string, in reflect.Value) { 264 | s := strconv.FormatUint(in.Uint(), 10) 265 | e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE) 266 | } 267 | 268 | func (e *encoder) floatv(tag string, in reflect.Value) { 269 | // FIXME: Handle 64 bits here. 270 | s := strconv.FormatFloat(float64(in.Float()), 'g', -1, 32) 271 | switch s { 272 | case "+Inf": 273 | s = ".inf" 274 | case "-Inf": 275 | s = "-.inf" 276 | case "NaN": 277 | s = ".nan" 278 | } 279 | e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE) 280 | } 281 | 282 | func (e *encoder) nilv() { 283 | e.emitScalar("null", "", "", yaml_PLAIN_SCALAR_STYLE) 284 | } 285 | 286 | func (e *encoder) emitScalar(value, anchor, tag string, style yaml_scalar_style_t) { 287 | implicit := tag == "" 288 | e.must(yaml_scalar_event_initialize(&e.event, []byte(anchor), []byte(tag), []byte(value), implicit, implicit, style)) 289 | e.emit() 290 | } 291 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/yaml/encode_test.go: -------------------------------------------------------------------------------- 1 | package yaml_test 2 | 3 | import ( 4 | "fmt" 5 | "math" 6 | "strconv" 7 | "strings" 8 | "time" 9 | 10 | . "gopkg.in/check.v1" 11 | "gopkg.in/yaml.v2" 12 | "net" 13 | ) 14 | 15 | var marshalIntTest = 123 16 | 17 | var marshalTests = []struct { 18 | value interface{} 19 | data string 20 | }{ 21 | { 22 | nil, 23 | "null\n", 24 | }, { 25 | &struct{}{}, 26 | "{}\n", 27 | }, { 28 | map[string]string{"v": "hi"}, 29 | "v: hi\n", 30 | }, { 31 | map[string]interface{}{"v": "hi"}, 32 | "v: hi\n", 33 | }, { 34 | map[string]string{"v": "true"}, 35 | "v: \"true\"\n", 36 | }, { 37 | map[string]string{"v": "false"}, 38 | "v: \"false\"\n", 39 | }, { 40 | map[string]interface{}{"v": true}, 41 | "v: true\n", 42 | }, { 43 | map[string]interface{}{"v": false}, 44 | "v: false\n", 45 | }, { 46 | map[string]interface{}{"v": 10}, 47 | "v: 10\n", 48 | }, { 49 | map[string]interface{}{"v": -10}, 50 | "v: -10\n", 51 | }, { 52 | map[string]uint{"v": 42}, 53 | "v: 42\n", 54 | }, { 55 | map[string]interface{}{"v": int64(4294967296)}, 56 | "v: 4294967296\n", 57 | }, { 58 | map[string]int64{"v": int64(4294967296)}, 59 | "v: 4294967296\n", 60 | }, { 61 | map[string]uint64{"v": 4294967296}, 62 | "v: 4294967296\n", 63 | }, { 64 | map[string]interface{}{"v": "10"}, 65 | "v: \"10\"\n", 66 | }, { 67 | map[string]interface{}{"v": 0.1}, 68 | "v: 0.1\n", 69 | }, { 70 | map[string]interface{}{"v": float64(0.1)}, 71 | "v: 0.1\n", 72 | }, { 73 | map[string]interface{}{"v": -0.1}, 74 | "v: -0.1\n", 75 | }, { 76 | map[string]interface{}{"v": math.Inf(+1)}, 77 | "v: .inf\n", 78 | }, { 79 | map[string]interface{}{"v": math.Inf(-1)}, 80 | "v: -.inf\n", 81 | }, { 82 | map[string]interface{}{"v": math.NaN()}, 83 | "v: .nan\n", 84 | }, { 85 | map[string]interface{}{"v": nil}, 86 | "v: null\n", 87 | }, { 88 | map[string]interface{}{"v": ""}, 89 | "v: \"\"\n", 90 | }, { 91 | map[string][]string{"v": []string{"A", "B"}}, 92 | "v:\n- A\n- B\n", 93 | }, { 94 | map[string][]string{"v": []string{"A", "B\nC"}}, 95 | "v:\n- A\n- |-\n B\n C\n", 96 | }, { 97 | map[string][]interface{}{"v": []interface{}{"A", 1, map[string][]int{"B": []int{2, 3}}}}, 98 | "v:\n- A\n- 1\n- B:\n - 2\n - 3\n", 99 | }, { 100 | map[string]interface{}{"a": map[interface{}]interface{}{"b": "c"}}, 101 | "a:\n b: c\n", 102 | }, { 103 | map[string]interface{}{"a": "-"}, 104 | "a: '-'\n", 105 | }, 106 | 107 | // Simple values. 108 | { 109 | &marshalIntTest, 110 | "123\n", 111 | }, 112 | 113 | // Structures 114 | { 115 | &struct{ Hello string }{"world"}, 116 | "hello: world\n", 117 | }, { 118 | &struct { 119 | A struct { 120 | B string 121 | } 122 | }{struct{ B string }{"c"}}, 123 | "a:\n b: c\n", 124 | }, { 125 | &struct { 126 | A *struct { 127 | B string 128 | } 129 | }{&struct{ B string }{"c"}}, 130 | "a:\n b: c\n", 131 | }, { 132 | &struct { 133 | A *struct { 134 | B string 135 | } 136 | }{}, 137 | "a: null\n", 138 | }, { 139 | &struct{ A int }{1}, 140 | "a: 1\n", 141 | }, { 142 | &struct{ A []int }{[]int{1, 2}}, 143 | "a:\n- 1\n- 2\n", 144 | }, { 145 | &struct { 146 | B int "a" 147 | }{1}, 148 | "a: 1\n", 149 | }, { 150 | &struct{ A bool }{true}, 151 | "a: true\n", 152 | }, 153 | 154 | // Conditional flag 155 | { 156 | &struct { 157 | A int "a,omitempty" 158 | B int "b,omitempty" 159 | }{1, 0}, 160 | "a: 1\n", 161 | }, { 162 | &struct { 163 | A int "a,omitempty" 164 | B int "b,omitempty" 165 | }{0, 0}, 166 | "{}\n", 167 | }, { 168 | &struct { 169 | A *struct{ X int } "a,omitempty" 170 | B int "b,omitempty" 171 | }{nil, 0}, 172 | "{}\n", 173 | }, 174 | 175 | // Flow flag 176 | { 177 | &struct { 178 | A []int "a,flow" 179 | }{[]int{1, 2}}, 180 | "a: [1, 2]\n", 181 | }, { 182 | &struct { 183 | A map[string]string "a,flow" 184 | }{map[string]string{"b": "c", "d": "e"}}, 185 | "a: {b: c, d: e}\n", 186 | }, { 187 | &struct { 188 | A struct { 189 | B, D string 190 | } "a,flow" 191 | }{struct{ B, D string }{"c", "e"}}, 192 | "a: {b: c, d: e}\n", 193 | }, 194 | 195 | // Unexported field 196 | { 197 | &struct { 198 | u int 199 | A int 200 | }{0, 1}, 201 | "a: 1\n", 202 | }, 203 | 204 | // Ignored field 205 | { 206 | &struct { 207 | A int 208 | B int "-" 209 | }{1, 2}, 210 | "a: 1\n", 211 | }, 212 | 213 | // Struct inlining 214 | { 215 | &struct { 216 | A int 217 | C inlineB `yaml:",inline"` 218 | }{1, inlineB{2, inlineC{3}}}, 219 | "a: 1\nb: 2\nc: 3\n", 220 | }, 221 | 222 | // Duration 223 | { 224 | map[string]time.Duration{"a": 3 * time.Second}, 225 | "a: 3s\n", 226 | }, 227 | 228 | // Issue #24: bug in map merging logic. 229 | { 230 | map[string]string{"a": ""}, 231 | "a: \n", 232 | }, 233 | 234 | // Issue #34: marshal unsupported base 60 floats quoted for compatibility 235 | // with old YAML 1.1 parsers. 236 | { 237 | map[string]string{"a": "1:1"}, 238 | "a: \"1:1\"\n", 239 | }, 240 | 241 | // Binary data. 242 | { 243 | map[string]string{"a": "\x00"}, 244 | "a: \"\\0\"\n", 245 | }, { 246 | map[string]string{"a": "\x80\x81\x82"}, 247 | "a: !!binary gIGC\n", 248 | }, { 249 | map[string]string{"a": strings.Repeat("\x90", 54)}, 250 | "a: !!binary |\n " + strings.Repeat("kJCQ", 17) + "kJ\n CQ\n", 251 | }, 252 | 253 | // Ordered maps. 254 | { 255 | &yaml.MapSlice{{"b", 2}, {"a", 1}, {"d", 4}, {"c", 3}, {"sub", yaml.MapSlice{{"e", 5}}}}, 256 | "b: 2\na: 1\nd: 4\nc: 3\nsub:\n e: 5\n", 257 | }, 258 | 259 | // Encode unicode as utf-8 rather than in escaped form. 260 | { 261 | map[string]string{"a": "你好"}, 262 | "a: 你好\n", 263 | }, 264 | 265 | // Support encoding.TextMarshaler. 266 | { 267 | map[string]net.IP{"a": net.IPv4(1, 2, 3, 4)}, 268 | "a: 1.2.3.4\n", 269 | }, 270 | } 271 | 272 | func (s *S) TestMarshal(c *C) { 273 | for _, item := range marshalTests { 274 | data, err := yaml.Marshal(item.value) 275 | c.Assert(err, IsNil) 276 | c.Assert(string(data), Equals, item.data) 277 | } 278 | } 279 | 280 | var marshalErrorTests = []struct { 281 | value interface{} 282 | error string 283 | panic string 284 | }{{ 285 | value: &struct { 286 | B int 287 | inlineB ",inline" 288 | }{1, inlineB{2, inlineC{3}}}, 289 | panic: `Duplicated key 'b' in struct struct \{ B int; .*`, 290 | }} 291 | 292 | func (s *S) TestMarshalErrors(c *C) { 293 | for _, item := range marshalErrorTests { 294 | if item.panic != "" { 295 | c.Assert(func() { yaml.Marshal(item.value) }, PanicMatches, item.panic) 296 | } else { 297 | _, err := yaml.Marshal(item.value) 298 | c.Assert(err, ErrorMatches, item.error) 299 | } 300 | } 301 | } 302 | 303 | func (s *S) TestMarshalTypeCache(c *C) { 304 | var data []byte 305 | var err error 306 | func() { 307 | type T struct{ A int } 308 | data, err = yaml.Marshal(&T{}) 309 | c.Assert(err, IsNil) 310 | }() 311 | func() { 312 | type T struct{ B int } 313 | data, err = yaml.Marshal(&T{}) 314 | c.Assert(err, IsNil) 315 | }() 316 | c.Assert(string(data), Equals, "b: 0\n") 317 | } 318 | 319 | var marshalerTests = []struct { 320 | data string 321 | value interface{} 322 | }{ 323 | {"_:\n hi: there\n", map[interface{}]interface{}{"hi": "there"}}, 324 | {"_:\n- 1\n- A\n", []interface{}{1, "A"}}, 325 | {"_: 10\n", 10}, 326 | {"_: null\n", nil}, 327 | {"_: BAR!\n", "BAR!"}, 328 | } 329 | 330 | type marshalerType struct { 331 | value interface{} 332 | } 333 | 334 | func (o marshalerType) MarshalYAML() (interface{}, error) { 335 | return o.value, nil 336 | } 337 | 338 | type marshalerValue struct { 339 | Field marshalerType "_" 340 | } 341 | 342 | func (s *S) TestMarshaler(c *C) { 343 | for _, item := range marshalerTests { 344 | obj := &marshalerValue{} 345 | obj.Field.value = item.value 346 | data, err := yaml.Marshal(obj) 347 | c.Assert(err, IsNil) 348 | c.Assert(string(data), Equals, string(item.data)) 349 | } 350 | } 351 | 352 | func (s *S) TestMarshalerWholeDocument(c *C) { 353 | obj := &marshalerType{} 354 | obj.value = map[string]string{"hello": "world!"} 355 | data, err := yaml.Marshal(obj) 356 | c.Assert(err, IsNil) 357 | c.Assert(string(data), Equals, "hello: world!\n") 358 | } 359 | 360 | type failingMarshaler struct{} 361 | 362 | func (ft *failingMarshaler) MarshalYAML() (interface{}, error) { 363 | return nil, failingErr 364 | } 365 | 366 | func (s *S) TestMarshalerError(c *C) { 367 | _, err := yaml.Marshal(&failingMarshaler{}) 368 | c.Assert(err, Equals, failingErr) 369 | } 370 | 371 | func (s *S) TestSortedOutput(c *C) { 372 | order := []interface{}{ 373 | false, 374 | true, 375 | 1, 376 | uint(1), 377 | 1.0, 378 | 1.1, 379 | 1.2, 380 | 2, 381 | uint(2), 382 | 2.0, 383 | 2.1, 384 | "", 385 | ".1", 386 | ".2", 387 | ".a", 388 | "1", 389 | "2", 390 | "a!10", 391 | "a/2", 392 | "a/10", 393 | "a~10", 394 | "ab/1", 395 | "b/1", 396 | "b/01", 397 | "b/2", 398 | "b/02", 399 | "b/3", 400 | "b/03", 401 | "b1", 402 | "b01", 403 | "b3", 404 | "c2.10", 405 | "c10.2", 406 | "d1", 407 | "d12", 408 | "d12a", 409 | } 410 | m := make(map[interface{}]int) 411 | for _, k := range order { 412 | m[k] = 1 413 | } 414 | data, err := yaml.Marshal(m) 415 | c.Assert(err, IsNil) 416 | out := "\n" + string(data) 417 | last := 0 418 | for i, k := range order { 419 | repr := fmt.Sprint(k) 420 | if s, ok := k.(string); ok { 421 | if _, err = strconv.ParseFloat(repr, 32); s == "" || err == nil { 422 | repr = `"` + repr + `"` 423 | } 424 | } 425 | index := strings.Index(out, "\n"+repr+":") 426 | if index == -1 { 427 | c.Fatalf("%#v is not in the output: %#v", k, out) 428 | } 429 | if index < last { 430 | c.Fatalf("%#v was generated before %#v: %q", k, order[i-1], out) 431 | } 432 | last = index 433 | } 434 | } 435 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/yaml/readerc.go: -------------------------------------------------------------------------------- 1 | package yaml 2 | 3 | import ( 4 | "io" 5 | ) 6 | 7 | // Set the reader error and return 0. 8 | func yaml_parser_set_reader_error(parser *yaml_parser_t, problem string, offset int, value int) bool { 9 | parser.error = yaml_READER_ERROR 10 | parser.problem = problem 11 | parser.problem_offset = offset 12 | parser.problem_value = value 13 | return false 14 | } 15 | 16 | // Byte order marks. 17 | const ( 18 | bom_UTF8 = "\xef\xbb\xbf" 19 | bom_UTF16LE = "\xff\xfe" 20 | bom_UTF16BE = "\xfe\xff" 21 | ) 22 | 23 | // Determine the input stream encoding by checking the BOM symbol. If no BOM is 24 | // found, the UTF-8 encoding is assumed. Return 1 on success, 0 on failure. 25 | func yaml_parser_determine_encoding(parser *yaml_parser_t) bool { 26 | // Ensure that we had enough bytes in the raw buffer. 27 | for !parser.eof && len(parser.raw_buffer)-parser.raw_buffer_pos < 3 { 28 | if !yaml_parser_update_raw_buffer(parser) { 29 | return false 30 | } 31 | } 32 | 33 | // Determine the encoding. 34 | buf := parser.raw_buffer 35 | pos := parser.raw_buffer_pos 36 | avail := len(buf) - pos 37 | if avail >= 2 && buf[pos] == bom_UTF16LE[0] && buf[pos+1] == bom_UTF16LE[1] { 38 | parser.encoding = yaml_UTF16LE_ENCODING 39 | parser.raw_buffer_pos += 2 40 | parser.offset += 2 41 | } else if avail >= 2 && buf[pos] == bom_UTF16BE[0] && buf[pos+1] == bom_UTF16BE[1] { 42 | parser.encoding = yaml_UTF16BE_ENCODING 43 | parser.raw_buffer_pos += 2 44 | parser.offset += 2 45 | } else if avail >= 3 && buf[pos] == bom_UTF8[0] && buf[pos+1] == bom_UTF8[1] && buf[pos+2] == bom_UTF8[2] { 46 | parser.encoding = yaml_UTF8_ENCODING 47 | parser.raw_buffer_pos += 3 48 | parser.offset += 3 49 | } else { 50 | parser.encoding = yaml_UTF8_ENCODING 51 | } 52 | return true 53 | } 54 | 55 | // Update the raw buffer. 56 | func yaml_parser_update_raw_buffer(parser *yaml_parser_t) bool { 57 | size_read := 0 58 | 59 | // Return if the raw buffer is full. 60 | if parser.raw_buffer_pos == 0 && len(parser.raw_buffer) == cap(parser.raw_buffer) { 61 | return true 62 | } 63 | 64 | // Return on EOF. 65 | if parser.eof { 66 | return true 67 | } 68 | 69 | // Move the remaining bytes in the raw buffer to the beginning. 70 | if parser.raw_buffer_pos > 0 && parser.raw_buffer_pos < len(parser.raw_buffer) { 71 | copy(parser.raw_buffer, parser.raw_buffer[parser.raw_buffer_pos:]) 72 | } 73 | parser.raw_buffer = parser.raw_buffer[:len(parser.raw_buffer)-parser.raw_buffer_pos] 74 | parser.raw_buffer_pos = 0 75 | 76 | // Call the read handler to fill the buffer. 77 | size_read, err := parser.read_handler(parser, parser.raw_buffer[len(parser.raw_buffer):cap(parser.raw_buffer)]) 78 | parser.raw_buffer = parser.raw_buffer[:len(parser.raw_buffer)+size_read] 79 | if err == io.EOF { 80 | parser.eof = true 81 | } else if err != nil { 82 | return yaml_parser_set_reader_error(parser, "input error: "+err.Error(), parser.offset, -1) 83 | } 84 | return true 85 | } 86 | 87 | // Ensure that the buffer contains at least `length` characters. 88 | // Return true on success, false on failure. 89 | // 90 | // The length is supposed to be significantly less that the buffer size. 91 | func yaml_parser_update_buffer(parser *yaml_parser_t, length int) bool { 92 | if parser.read_handler == nil { 93 | panic("read handler must be set") 94 | } 95 | 96 | // If the EOF flag is set and the raw buffer is empty, do nothing. 97 | if parser.eof && parser.raw_buffer_pos == len(parser.raw_buffer) { 98 | return true 99 | } 100 | 101 | // Return if the buffer contains enough characters. 102 | if parser.unread >= length { 103 | return true 104 | } 105 | 106 | // Determine the input encoding if it is not known yet. 107 | if parser.encoding == yaml_ANY_ENCODING { 108 | if !yaml_parser_determine_encoding(parser) { 109 | return false 110 | } 111 | } 112 | 113 | // Move the unread characters to the beginning of the buffer. 114 | buffer_len := len(parser.buffer) 115 | if parser.buffer_pos > 0 && parser.buffer_pos < buffer_len { 116 | copy(parser.buffer, parser.buffer[parser.buffer_pos:]) 117 | buffer_len -= parser.buffer_pos 118 | parser.buffer_pos = 0 119 | } else if parser.buffer_pos == buffer_len { 120 | buffer_len = 0 121 | parser.buffer_pos = 0 122 | } 123 | 124 | // Open the whole buffer for writing, and cut it before returning. 125 | parser.buffer = parser.buffer[:cap(parser.buffer)] 126 | 127 | // Fill the buffer until it has enough characters. 128 | first := true 129 | for parser.unread < length { 130 | 131 | // Fill the raw buffer if necessary. 132 | if !first || parser.raw_buffer_pos == len(parser.raw_buffer) { 133 | if !yaml_parser_update_raw_buffer(parser) { 134 | parser.buffer = parser.buffer[:buffer_len] 135 | return false 136 | } 137 | } 138 | first = false 139 | 140 | // Decode the raw buffer. 141 | inner: 142 | for parser.raw_buffer_pos != len(parser.raw_buffer) { 143 | var value rune 144 | var width int 145 | 146 | raw_unread := len(parser.raw_buffer) - parser.raw_buffer_pos 147 | 148 | // Decode the next character. 149 | switch parser.encoding { 150 | case yaml_UTF8_ENCODING: 151 | // Decode a UTF-8 character. Check RFC 3629 152 | // (http://www.ietf.org/rfc/rfc3629.txt) for more details. 153 | // 154 | // The following table (taken from the RFC) is used for 155 | // decoding. 156 | // 157 | // Char. number range | UTF-8 octet sequence 158 | // (hexadecimal) | (binary) 159 | // --------------------+------------------------------------ 160 | // 0000 0000-0000 007F | 0xxxxxxx 161 | // 0000 0080-0000 07FF | 110xxxxx 10xxxxxx 162 | // 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx 163 | // 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx 164 | // 165 | // Additionally, the characters in the range 0xD800-0xDFFF 166 | // are prohibited as they are reserved for use with UTF-16 167 | // surrogate pairs. 168 | 169 | // Determine the length of the UTF-8 sequence. 170 | octet := parser.raw_buffer[parser.raw_buffer_pos] 171 | switch { 172 | case octet&0x80 == 0x00: 173 | width = 1 174 | case octet&0xE0 == 0xC0: 175 | width = 2 176 | case octet&0xF0 == 0xE0: 177 | width = 3 178 | case octet&0xF8 == 0xF0: 179 | width = 4 180 | default: 181 | // The leading octet is invalid. 182 | return yaml_parser_set_reader_error(parser, 183 | "invalid leading UTF-8 octet", 184 | parser.offset, int(octet)) 185 | } 186 | 187 | // Check if the raw buffer contains an incomplete character. 188 | if width > raw_unread { 189 | if parser.eof { 190 | return yaml_parser_set_reader_error(parser, 191 | "incomplete UTF-8 octet sequence", 192 | parser.offset, -1) 193 | } 194 | break inner 195 | } 196 | 197 | // Decode the leading octet. 198 | switch { 199 | case octet&0x80 == 0x00: 200 | value = rune(octet & 0x7F) 201 | case octet&0xE0 == 0xC0: 202 | value = rune(octet & 0x1F) 203 | case octet&0xF0 == 0xE0: 204 | value = rune(octet & 0x0F) 205 | case octet&0xF8 == 0xF0: 206 | value = rune(octet & 0x07) 207 | default: 208 | value = 0 209 | } 210 | 211 | // Check and decode the trailing octets. 212 | for k := 1; k < width; k++ { 213 | octet = parser.raw_buffer[parser.raw_buffer_pos+k] 214 | 215 | // Check if the octet is valid. 216 | if (octet & 0xC0) != 0x80 { 217 | return yaml_parser_set_reader_error(parser, 218 | "invalid trailing UTF-8 octet", 219 | parser.offset+k, int(octet)) 220 | } 221 | 222 | // Decode the octet. 223 | value = (value << 6) + rune(octet&0x3F) 224 | } 225 | 226 | // Check the length of the sequence against the value. 227 | switch { 228 | case width == 1: 229 | case width == 2 && value >= 0x80: 230 | case width == 3 && value >= 0x800: 231 | case width == 4 && value >= 0x10000: 232 | default: 233 | return yaml_parser_set_reader_error(parser, 234 | "invalid length of a UTF-8 sequence", 235 | parser.offset, -1) 236 | } 237 | 238 | // Check the range of the value. 239 | if value >= 0xD800 && value <= 0xDFFF || value > 0x10FFFF { 240 | return yaml_parser_set_reader_error(parser, 241 | "invalid Unicode character", 242 | parser.offset, int(value)) 243 | } 244 | 245 | case yaml_UTF16LE_ENCODING, yaml_UTF16BE_ENCODING: 246 | var low, high int 247 | if parser.encoding == yaml_UTF16LE_ENCODING { 248 | low, high = 0, 1 249 | } else { 250 | high, low = 1, 0 251 | } 252 | 253 | // The UTF-16 encoding is not as simple as one might 254 | // naively think. Check RFC 2781 255 | // (http://www.ietf.org/rfc/rfc2781.txt). 256 | // 257 | // Normally, two subsequent bytes describe a Unicode 258 | // character. However a special technique (called a 259 | // surrogate pair) is used for specifying character 260 | // values larger than 0xFFFF. 261 | // 262 | // A surrogate pair consists of two pseudo-characters: 263 | // high surrogate area (0xD800-0xDBFF) 264 | // low surrogate area (0xDC00-0xDFFF) 265 | // 266 | // The following formulas are used for decoding 267 | // and encoding characters using surrogate pairs: 268 | // 269 | // U = U' + 0x10000 (0x01 00 00 <= U <= 0x10 FF FF) 270 | // U' = yyyyyyyyyyxxxxxxxxxx (0 <= U' <= 0x0F FF FF) 271 | // W1 = 110110yyyyyyyyyy 272 | // W2 = 110111xxxxxxxxxx 273 | // 274 | // where U is the character value, W1 is the high surrogate 275 | // area, W2 is the low surrogate area. 276 | 277 | // Check for incomplete UTF-16 character. 278 | if raw_unread < 2 { 279 | if parser.eof { 280 | return yaml_parser_set_reader_error(parser, 281 | "incomplete UTF-16 character", 282 | parser.offset, -1) 283 | } 284 | break inner 285 | } 286 | 287 | // Get the character. 288 | value = rune(parser.raw_buffer[parser.raw_buffer_pos+low]) + 289 | (rune(parser.raw_buffer[parser.raw_buffer_pos+high]) << 8) 290 | 291 | // Check for unexpected low surrogate area. 292 | if value&0xFC00 == 0xDC00 { 293 | return yaml_parser_set_reader_error(parser, 294 | "unexpected low surrogate area", 295 | parser.offset, int(value)) 296 | } 297 | 298 | // Check for a high surrogate area. 299 | if value&0xFC00 == 0xD800 { 300 | width = 4 301 | 302 | // Check for incomplete surrogate pair. 303 | if raw_unread < 4 { 304 | if parser.eof { 305 | return yaml_parser_set_reader_error(parser, 306 | "incomplete UTF-16 surrogate pair", 307 | parser.offset, -1) 308 | } 309 | break inner 310 | } 311 | 312 | // Get the next character. 313 | value2 := rune(parser.raw_buffer[parser.raw_buffer_pos+low+2]) + 314 | (rune(parser.raw_buffer[parser.raw_buffer_pos+high+2]) << 8) 315 | 316 | // Check for a low surrogate area. 317 | if value2&0xFC00 != 0xDC00 { 318 | return yaml_parser_set_reader_error(parser, 319 | "expected low surrogate area", 320 | parser.offset+2, int(value2)) 321 | } 322 | 323 | // Generate the value of the surrogate pair. 324 | value = 0x10000 + ((value & 0x3FF) << 10) + (value2 & 0x3FF) 325 | } else { 326 | width = 2 327 | } 328 | 329 | default: 330 | panic("impossible") 331 | } 332 | 333 | // Check if the character is in the allowed range: 334 | // #x9 | #xA | #xD | [#x20-#x7E] (8 bit) 335 | // | #x85 | [#xA0-#xD7FF] | [#xE000-#xFFFD] (16 bit) 336 | // | [#x10000-#x10FFFF] (32 bit) 337 | switch { 338 | case value == 0x09: 339 | case value == 0x0A: 340 | case value == 0x0D: 341 | case value >= 0x20 && value <= 0x7E: 342 | case value == 0x85: 343 | case value >= 0xA0 && value <= 0xD7FF: 344 | case value >= 0xE000 && value <= 0xFFFD: 345 | case value >= 0x10000 && value <= 0x10FFFF: 346 | default: 347 | return yaml_parser_set_reader_error(parser, 348 | "control characters are not allowed", 349 | parser.offset, int(value)) 350 | } 351 | 352 | // Move the raw pointers. 353 | parser.raw_buffer_pos += width 354 | parser.offset += width 355 | 356 | // Finally put the character into the buffer. 357 | if value <= 0x7F { 358 | // 0000 0000-0000 007F . 0xxxxxxx 359 | parser.buffer[buffer_len+0] = byte(value) 360 | } else if value <= 0x7FF { 361 | // 0000 0080-0000 07FF . 110xxxxx 10xxxxxx 362 | parser.buffer[buffer_len+0] = byte(0xC0 + (value >> 6)) 363 | parser.buffer[buffer_len+1] = byte(0x80 + (value & 0x3F)) 364 | } else if value <= 0xFFFF { 365 | // 0000 0800-0000 FFFF . 1110xxxx 10xxxxxx 10xxxxxx 366 | parser.buffer[buffer_len+0] = byte(0xE0 + (value >> 12)) 367 | parser.buffer[buffer_len+1] = byte(0x80 + ((value >> 6) & 0x3F)) 368 | parser.buffer[buffer_len+2] = byte(0x80 + (value & 0x3F)) 369 | } else { 370 | // 0001 0000-0010 FFFF . 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx 371 | parser.buffer[buffer_len+0] = byte(0xF0 + (value >> 18)) 372 | parser.buffer[buffer_len+1] = byte(0x80 + ((value >> 12) & 0x3F)) 373 | parser.buffer[buffer_len+2] = byte(0x80 + ((value >> 6) & 0x3F)) 374 | parser.buffer[buffer_len+3] = byte(0x80 + (value & 0x3F)) 375 | } 376 | buffer_len += width 377 | 378 | parser.unread++ 379 | } 380 | 381 | // On EOF, put NUL into the buffer and return. 382 | if parser.eof { 383 | parser.buffer[buffer_len] = 0 384 | buffer_len++ 385 | parser.unread++ 386 | break 387 | } 388 | } 389 | parser.buffer = parser.buffer[:buffer_len] 390 | return true 391 | } 392 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/yaml/resolve.go: -------------------------------------------------------------------------------- 1 | package yaml 2 | 3 | import ( 4 | "encoding/base64" 5 | "math" 6 | "strconv" 7 | "strings" 8 | "unicode/utf8" 9 | ) 10 | 11 | type resolveMapItem struct { 12 | value interface{} 13 | tag string 14 | } 15 | 16 | var resolveTable = make([]byte, 256) 17 | var resolveMap = make(map[string]resolveMapItem) 18 | 19 | func init() { 20 | t := resolveTable 21 | t[int('+')] = 'S' // Sign 22 | t[int('-')] = 'S' 23 | for _, c := range "0123456789" { 24 | t[int(c)] = 'D' // Digit 25 | } 26 | for _, c := range "yYnNtTfFoO~" { 27 | t[int(c)] = 'M' // In map 28 | } 29 | t[int('.')] = '.' // Float (potentially in map) 30 | 31 | var resolveMapList = []struct { 32 | v interface{} 33 | tag string 34 | l []string 35 | }{ 36 | {true, yaml_BOOL_TAG, []string{"y", "Y", "yes", "Yes", "YES"}}, 37 | {true, yaml_BOOL_TAG, []string{"true", "True", "TRUE"}}, 38 | {true, yaml_BOOL_TAG, []string{"on", "On", "ON"}}, 39 | {false, yaml_BOOL_TAG, []string{"n", "N", "no", "No", "NO"}}, 40 | {false, yaml_BOOL_TAG, []string{"false", "False", "FALSE"}}, 41 | {false, yaml_BOOL_TAG, []string{"off", "Off", "OFF"}}, 42 | {nil, yaml_NULL_TAG, []string{"", "~", "null", "Null", "NULL"}}, 43 | {math.NaN(), yaml_FLOAT_TAG, []string{".nan", ".NaN", ".NAN"}}, 44 | {math.Inf(+1), yaml_FLOAT_TAG, []string{".inf", ".Inf", ".INF"}}, 45 | {math.Inf(+1), yaml_FLOAT_TAG, []string{"+.inf", "+.Inf", "+.INF"}}, 46 | {math.Inf(-1), yaml_FLOAT_TAG, []string{"-.inf", "-.Inf", "-.INF"}}, 47 | {"<<", yaml_MERGE_TAG, []string{"<<"}}, 48 | } 49 | 50 | m := resolveMap 51 | for _, item := range resolveMapList { 52 | for _, s := range item.l { 53 | m[s] = resolveMapItem{item.v, item.tag} 54 | } 55 | } 56 | } 57 | 58 | const longTagPrefix = "tag:yaml.org,2002:" 59 | 60 | func shortTag(tag string) string { 61 | // TODO This can easily be made faster and produce less garbage. 62 | if strings.HasPrefix(tag, longTagPrefix) { 63 | return "!!" + tag[len(longTagPrefix):] 64 | } 65 | return tag 66 | } 67 | 68 | func longTag(tag string) string { 69 | if strings.HasPrefix(tag, "!!") { 70 | return longTagPrefix + tag[2:] 71 | } 72 | return tag 73 | } 74 | 75 | func resolvableTag(tag string) bool { 76 | switch tag { 77 | case "", yaml_STR_TAG, yaml_BOOL_TAG, yaml_INT_TAG, yaml_FLOAT_TAG, yaml_NULL_TAG: 78 | return true 79 | } 80 | return false 81 | } 82 | 83 | func resolve(tag string, in string) (rtag string, out interface{}) { 84 | if !resolvableTag(tag) { 85 | return tag, in 86 | } 87 | 88 | defer func() { 89 | switch tag { 90 | case "", rtag, yaml_STR_TAG, yaml_BINARY_TAG: 91 | return 92 | } 93 | failf("cannot decode %s `%s` as a %s", shortTag(rtag), in, shortTag(tag)) 94 | }() 95 | 96 | // Any data is accepted as a !!str or !!binary. 97 | // Otherwise, the prefix is enough of a hint about what it might be. 98 | hint := byte('N') 99 | if in != "" { 100 | hint = resolveTable[in[0]] 101 | } 102 | if hint != 0 && tag != yaml_STR_TAG && tag != yaml_BINARY_TAG { 103 | // Handle things we can lookup in a map. 104 | if item, ok := resolveMap[in]; ok { 105 | return item.tag, item.value 106 | } 107 | 108 | // Base 60 floats are a bad idea, were dropped in YAML 1.2, and 109 | // are purposefully unsupported here. They're still quoted on 110 | // the way out for compatibility with other parser, though. 111 | 112 | switch hint { 113 | case 'M': 114 | // We've already checked the map above. 115 | 116 | case '.': 117 | // Not in the map, so maybe a normal float. 118 | floatv, err := strconv.ParseFloat(in, 64) 119 | if err == nil { 120 | return yaml_FLOAT_TAG, floatv 121 | } 122 | 123 | case 'D', 'S': 124 | // Int, float, or timestamp. 125 | plain := strings.Replace(in, "_", "", -1) 126 | intv, err := strconv.ParseInt(plain, 0, 64) 127 | if err == nil { 128 | if intv == int64(int(intv)) { 129 | return yaml_INT_TAG, int(intv) 130 | } else { 131 | return yaml_INT_TAG, intv 132 | } 133 | } 134 | uintv, err := strconv.ParseUint(plain, 0, 64) 135 | if err == nil { 136 | return yaml_INT_TAG, uintv 137 | } 138 | floatv, err := strconv.ParseFloat(plain, 64) 139 | if err == nil { 140 | return yaml_FLOAT_TAG, floatv 141 | } 142 | if strings.HasPrefix(plain, "0b") { 143 | intv, err := strconv.ParseInt(plain[2:], 2, 64) 144 | if err == nil { 145 | if intv == int64(int(intv)) { 146 | return yaml_INT_TAG, int(intv) 147 | } else { 148 | return yaml_INT_TAG, intv 149 | } 150 | } 151 | uintv, err := strconv.ParseUint(plain[2:], 2, 64) 152 | if err == nil { 153 | return yaml_INT_TAG, uintv 154 | } 155 | } else if strings.HasPrefix(plain, "-0b") { 156 | intv, err := strconv.ParseInt(plain[3:], 2, 64) 157 | if err == nil { 158 | if intv == int64(int(intv)) { 159 | return yaml_INT_TAG, -int(intv) 160 | } else { 161 | return yaml_INT_TAG, -intv 162 | } 163 | } 164 | } 165 | // XXX Handle timestamps here. 166 | 167 | default: 168 | panic("resolveTable item not yet handled: " + string(rune(hint)) + " (with " + in + ")") 169 | } 170 | } 171 | if tag == yaml_BINARY_TAG { 172 | return yaml_BINARY_TAG, in 173 | } 174 | if utf8.ValidString(in) { 175 | return yaml_STR_TAG, in 176 | } 177 | return yaml_BINARY_TAG, encodeBase64(in) 178 | } 179 | 180 | // encodeBase64 encodes s as base64 that is broken up into multiple lines 181 | // as appropriate for the resulting length. 182 | func encodeBase64(s string) string { 183 | const lineLen = 70 184 | encLen := base64.StdEncoding.EncodedLen(len(s)) 185 | lines := encLen/lineLen + 1 186 | buf := make([]byte, encLen*2+lines) 187 | in := buf[0:encLen] 188 | out := buf[encLen:] 189 | base64.StdEncoding.Encode(in, []byte(s)) 190 | k := 0 191 | for i := 0; i < len(in); i += lineLen { 192 | j := i + lineLen 193 | if j > len(in) { 194 | j = len(in) 195 | } 196 | k += copy(out[k:], in[i:j]) 197 | if lines > 1 { 198 | out[k] = '\n' 199 | k++ 200 | } 201 | } 202 | return string(out[:k]) 203 | } 204 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/yaml/sorter.go: -------------------------------------------------------------------------------- 1 | package yaml 2 | 3 | import ( 4 | "reflect" 5 | "unicode" 6 | ) 7 | 8 | type keyList []reflect.Value 9 | 10 | func (l keyList) Len() int { return len(l) } 11 | func (l keyList) Swap(i, j int) { l[i], l[j] = l[j], l[i] } 12 | func (l keyList) Less(i, j int) bool { 13 | a := l[i] 14 | b := l[j] 15 | ak := a.Kind() 16 | bk := b.Kind() 17 | for (ak == reflect.Interface || ak == reflect.Ptr) && !a.IsNil() { 18 | a = a.Elem() 19 | ak = a.Kind() 20 | } 21 | for (bk == reflect.Interface || bk == reflect.Ptr) && !b.IsNil() { 22 | b = b.Elem() 23 | bk = b.Kind() 24 | } 25 | af, aok := keyFloat(a) 26 | bf, bok := keyFloat(b) 27 | if aok && bok { 28 | if af != bf { 29 | return af < bf 30 | } 31 | if ak != bk { 32 | return ak < bk 33 | } 34 | return numLess(a, b) 35 | } 36 | if ak != reflect.String || bk != reflect.String { 37 | return ak < bk 38 | } 39 | ar, br := []rune(a.String()), []rune(b.String()) 40 | for i := 0; i < len(ar) && i < len(br); i++ { 41 | if ar[i] == br[i] { 42 | continue 43 | } 44 | al := unicode.IsLetter(ar[i]) 45 | bl := unicode.IsLetter(br[i]) 46 | if al && bl { 47 | return ar[i] < br[i] 48 | } 49 | if al || bl { 50 | return bl 51 | } 52 | var ai, bi int 53 | var an, bn int64 54 | for ai = i; ai < len(ar) && unicode.IsDigit(ar[ai]); ai++ { 55 | an = an*10 + int64(ar[ai]-'0') 56 | } 57 | for bi = i; bi < len(br) && unicode.IsDigit(br[bi]); bi++ { 58 | bn = bn*10 + int64(br[bi]-'0') 59 | } 60 | if an != bn { 61 | return an < bn 62 | } 63 | if ai != bi { 64 | return ai < bi 65 | } 66 | return ar[i] < br[i] 67 | } 68 | return len(ar) < len(br) 69 | } 70 | 71 | // keyFloat returns a float value for v if it is a number/bool 72 | // and whether it is a number/bool or not. 73 | func keyFloat(v reflect.Value) (f float64, ok bool) { 74 | switch v.Kind() { 75 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 76 | return float64(v.Int()), true 77 | case reflect.Float32, reflect.Float64: 78 | return v.Float(), true 79 | case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: 80 | return float64(v.Uint()), true 81 | case reflect.Bool: 82 | if v.Bool() { 83 | return 1, true 84 | } 85 | return 0, true 86 | } 87 | return 0, false 88 | } 89 | 90 | // numLess returns whether a < b. 91 | // a and b must necessarily have the same kind. 92 | func numLess(a, b reflect.Value) bool { 93 | switch a.Kind() { 94 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 95 | return a.Int() < b.Int() 96 | case reflect.Float32, reflect.Float64: 97 | return a.Float() < b.Float() 98 | case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: 99 | return a.Uint() < b.Uint() 100 | case reflect.Bool: 101 | return !a.Bool() && b.Bool() 102 | } 103 | panic("not a number") 104 | } 105 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/yaml/suite_test.go: -------------------------------------------------------------------------------- 1 | package yaml_test 2 | 3 | import ( 4 | . "gopkg.in/check.v1" 5 | "testing" 6 | ) 7 | 8 | func Test(t *testing.T) { TestingT(t) } 9 | 10 | type S struct{} 11 | 12 | var _ = Suite(&S{}) 13 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/yaml/writerc.go: -------------------------------------------------------------------------------- 1 | package yaml 2 | 3 | // Set the writer error and return false. 4 | func yaml_emitter_set_writer_error(emitter *yaml_emitter_t, problem string) bool { 5 | emitter.error = yaml_WRITER_ERROR 6 | emitter.problem = problem 7 | return false 8 | } 9 | 10 | // Flush the output buffer. 11 | func yaml_emitter_flush(emitter *yaml_emitter_t) bool { 12 | if emitter.write_handler == nil { 13 | panic("write handler not set") 14 | } 15 | 16 | // Check if the buffer is empty. 17 | if emitter.buffer_pos == 0 { 18 | return true 19 | } 20 | 21 | // If the output encoding is UTF-8, we don't need to recode the buffer. 22 | if emitter.encoding == yaml_UTF8_ENCODING { 23 | if err := emitter.write_handler(emitter, emitter.buffer[:emitter.buffer_pos]); err != nil { 24 | return yaml_emitter_set_writer_error(emitter, "write error: "+err.Error()) 25 | } 26 | emitter.buffer_pos = 0 27 | return true 28 | } 29 | 30 | // Recode the buffer into the raw buffer. 31 | var low, high int 32 | if emitter.encoding == yaml_UTF16LE_ENCODING { 33 | low, high = 0, 1 34 | } else { 35 | high, low = 1, 0 36 | } 37 | 38 | pos := 0 39 | for pos < emitter.buffer_pos { 40 | // See the "reader.c" code for more details on UTF-8 encoding. Note 41 | // that we assume that the buffer contains a valid UTF-8 sequence. 42 | 43 | // Read the next UTF-8 character. 44 | octet := emitter.buffer[pos] 45 | 46 | var w int 47 | var value rune 48 | switch { 49 | case octet&0x80 == 0x00: 50 | w, value = 1, rune(octet&0x7F) 51 | case octet&0xE0 == 0xC0: 52 | w, value = 2, rune(octet&0x1F) 53 | case octet&0xF0 == 0xE0: 54 | w, value = 3, rune(octet&0x0F) 55 | case octet&0xF8 == 0xF0: 56 | w, value = 4, rune(octet&0x07) 57 | } 58 | for k := 1; k < w; k++ { 59 | octet = emitter.buffer[pos+k] 60 | value = (value << 6) + (rune(octet) & 0x3F) 61 | } 62 | pos += w 63 | 64 | // Write the character. 65 | if value < 0x10000 { 66 | var b [2]byte 67 | b[high] = byte(value >> 8) 68 | b[low] = byte(value & 0xFF) 69 | emitter.raw_buffer = append(emitter.raw_buffer, b[0], b[1]) 70 | } else { 71 | // Write the character using a surrogate pair (check "reader.c"). 72 | var b [4]byte 73 | value -= 0x10000 74 | b[high] = byte(0xD8 + (value >> 18)) 75 | b[low] = byte((value >> 10) & 0xFF) 76 | b[high+2] = byte(0xDC + ((value >> 8) & 0xFF)) 77 | b[low+2] = byte(value & 0xFF) 78 | emitter.raw_buffer = append(emitter.raw_buffer, b[0], b[1], b[2], b[3]) 79 | } 80 | } 81 | 82 | // Write the raw buffer. 83 | if err := emitter.write_handler(emitter, emitter.raw_buffer); err != nil { 84 | return yaml_emitter_set_writer_error(emitter, "write error: "+err.Error()) 85 | } 86 | emitter.buffer_pos = 0 87 | emitter.raw_buffer = emitter.raw_buffer[:0] 88 | return true 89 | } 90 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/yaml/yaml.go: -------------------------------------------------------------------------------- 1 | // Package yaml implements YAML support for the Go language. 2 | // 3 | // Source code and other details for the project are available at GitHub: 4 | // 5 | // https://github.com/go-yaml/yaml 6 | // 7 | package yaml 8 | 9 | import ( 10 | "errors" 11 | "fmt" 12 | "reflect" 13 | "regexp" 14 | "strings" 15 | "sync" 16 | ) 17 | 18 | // MapSlice encodes and decodes as a YAML map. 19 | // The order of keys is preserved when encoding and decoding. 20 | type MapSlice []MapItem 21 | 22 | // MapItem is an item in a MapSlice. 23 | type MapItem struct { 24 | Key, Value interface{} 25 | } 26 | 27 | // The Unmarshaler interface may be implemented by types to customize their 28 | // behavior when being unmarshaled from a YAML document. The UnmarshalYAML 29 | // method receives a function that may be called to unmarshal the original 30 | // YAML value into a field or variable. It is safe to call the unmarshal 31 | // function parameter more than once if necessary. 32 | // Generally, the idea is to call the method to unmarshal into a value of 33 | // the correct type, then use this unmarshalled value wherever you need to. 34 | // 35 | // For example: 36 | // 37 | // type T struct { 38 | // values map[string]int 39 | // sum int 40 | // } 41 | // 42 | // func (t *T) UnmarshalYAML(unmarshaler func(interface{}) error) error { 43 | // 44 | // if err := unmarshaler(t.values); err != nil { 45 | // return err 46 | // } 47 | // 48 | // for _, value := range t.values { 49 | // t.sum += value 50 | // } 51 | // 52 | // return nil 53 | // } 54 | // 55 | // var t T 56 | // yaml.Unmarshal([]byte("T:\n a: 1\n b: 2\n c:3"), &t) 57 | // 58 | // 59 | type Unmarshaler interface { 60 | UnmarshalYAML(unmarshal func(interface{}) error) error 61 | } 62 | 63 | // The Marshaler interface may be implemented by types to customize their 64 | // behavior when being marshaled into a YAML document. The returned value 65 | // is marshaled in place of the original value implementing Marshaler. 66 | // 67 | // If an error is returned by MarshalYAML, the marshaling procedure stops 68 | // and returns with the provided error. 69 | type Marshaler interface { 70 | MarshalYAML() (interface{}, error) 71 | } 72 | 73 | // Unmarshal decodes the first document found within the in byte slice 74 | // and assigns decoded values into the out value. 75 | // 76 | // Maps and pointers (to a struct, string, int, etc) are accepted as out 77 | // values. If an internal pointer within a struct is not initialized, 78 | // the yaml package will initialize it if necessary for unmarshalling 79 | // the provided data. The out parameter must not be nil. 80 | // 81 | // The type of the decoded values should be compatible with the respective 82 | // values in out. If one or more values cannot be decoded due to a type 83 | // mismatches, decoding continues partially until the end of the YAML 84 | // content, and a *yaml.TypeError is returned with details for all 85 | // missed values. 86 | // 87 | // Struct fields are only unmarshalled if they are exported (have an 88 | // upper case first letter), and are unmarshalled using the field name 89 | // lowercased as the default key. Custom keys may be defined via the 90 | // "yaml" name in the field tag: the content preceding the first comma 91 | // is used as the key, and the following comma-separated options are 92 | // used to tweak the marshalling process (see Marshal). 93 | // Conflicting names result in a runtime error. 94 | // 95 | // For example: 96 | // 97 | // type T struct { 98 | // F int `yaml:"a,omitempty"` 99 | // B int 100 | // } 101 | // var t T 102 | // yaml.Unmarshal([]byte("a: 1\nb: 2"), &t) 103 | // 104 | // 105 | // Another flag which is supported during umarshaling, but must be used on 106 | // its own, is: 107 | // 108 | // regexp Unmarshal all encountered YAML values with keys that 109 | // match the regular expression into the tagged field, 110 | // which must be a map or a slice of a type that the 111 | // YAML value should be unmarshaled into. 112 | // [Unmarshaling] 113 | // 114 | // For example: 115 | // 116 | // type T struct { 117 | // A int 118 | // B int 119 | // Numbers map[string]int `yaml:",regexp:num.*"` 120 | // Phrases []string `yaml:",regexp:phr.*"` 121 | // } 122 | // var t T 123 | // yaml.Unmarshal([]byte("a: 1\nb: 2\nnum1: 1\nnum2: 50\n" + 124 | // "phraseOne: to be or not to be\n" + 125 | // "phraseTwo: you can't touch my key!\n" + 126 | // "anotherKey: ThisValueWillNotBeUnmarshalled"), &t) 127 | // 128 | // You can also use the regexp flag to get all unmapped values into a map for 129 | // runtime usage: 130 | // 131 | // type T struct { 132 | // A int 133 | // B int 134 | // EverythingElse map[string]interface{} `yaml:",regexp:.*"` 135 | // } 136 | // var t T 137 | // yaml.Unmarshal([]byte("a: 1\nb: 2\nnum1: 1\nnum2: 50\n" + 138 | // "anInteger: 111\n" + 139 | // "aFloat: 0.5555\n" + 140 | // "anotherKey: WhichIsAstring\n" + 141 | // "aSequence: [1, 2, 3]\n" + 142 | // "aMapping: {hello: world}"), &t) 143 | // 144 | // The resulting EverythingElse map will contain everything except the values of 145 | // a and b. 146 | // 147 | // See the documentation of Marshal for the format of additional tags and a list 148 | // of supported tag options. 149 | // 150 | func Unmarshal(in []byte, out interface{}) (err error) { 151 | defer handleErr(&err) 152 | d := newDecoder() 153 | p := newParser(in) 154 | defer p.destroy() 155 | node := p.parse() 156 | if node != nil { 157 | v := reflect.ValueOf(out) 158 | if v.Kind() == reflect.Ptr && !v.IsNil() { 159 | v = v.Elem() 160 | } 161 | d.unmarshal(node, v) 162 | } 163 | if d.terrors != nil && len(d.terrors) > 0 { 164 | return &TypeError{d.terrors} 165 | } 166 | return nil 167 | } 168 | 169 | // Marshal serializes the value provided into a YAML document. The structure 170 | // of the generated document will reflect the structure of the value itself. 171 | // Maps and pointers (to struct, string, int, etc) are accepted as the in value. 172 | // 173 | // Struct fields are only unmarshalled if they are exported (have an upper case 174 | // first letter), and are unmarshalled using the field name lowercased as the 175 | // default key. Custom keys may be defined via the "yaml" name in the field 176 | // tag: the content preceding the first comma is used as the key, and the 177 | // following comma-separated options are used to tweak the marshalling process. 178 | // Conflicting names result in a runtime error. 179 | // 180 | // The field tag format accepted is: 181 | // 182 | // `(...) yaml:"[][,[,]]" (...)` 183 | // 184 | // The following flags are currently supported: 185 | // 186 | // omitempty Only include the field when marshaling if it's 187 | // not set to the zero value for the type or to empty 188 | // slices or maps. Does not apply to zero valued structs. 189 | // [Marshaling] 190 | // 191 | // flow Use a flow style when marshaling (useful for structs, 192 | // sequences and maps.) 193 | // [Marshaling] 194 | // 195 | // inline Inline the struct it's applied to, so its fields 196 | // are processed (during marshaling and unmarshaling) 197 | // as if they were part of the outer struct. 198 | // [Marshaling, Unmarshaling] 199 | // 200 | // In addition, if the key is "-", the field is ignored. 201 | // 202 | // For example: 203 | // 204 | // type T struct { 205 | // F int "a,omitempty" 206 | // B int 207 | // } 208 | // yaml.Marshal(&T{B: 2}) // Returns "b: 2\n" 209 | // yaml.Marshal(&T{F: 1}} // Returns "a: 1\nb: 0\n" 210 | // 211 | func Marshal(in interface{}) (out []byte, err error) { 212 | defer handleErr(&err) 213 | e := newEncoder() 214 | defer e.destroy() 215 | e.marshal("", reflect.ValueOf(in)) 216 | e.finish() 217 | out = e.out 218 | return 219 | } 220 | 221 | func handleErr(err *error) { 222 | if v := recover(); v != nil { 223 | if e, ok := v.(yamlError); ok { 224 | *err = e.err 225 | } else { 226 | panic(v) 227 | } 228 | } 229 | } 230 | 231 | type yamlError struct { 232 | err error 233 | } 234 | 235 | func fail(err error) { 236 | panic(yamlError{err}) 237 | } 238 | 239 | func failf(format string, args ...interface{}) { 240 | panic(yamlError{fmt.Errorf("yaml: "+format, args...)}) 241 | } 242 | 243 | // A TypeError is returned by Unmarshal when one or more fields in 244 | // the YAML document cannot be properly decoded into the requested 245 | // types. When this error is returned, the value is still 246 | // unmarshaled partially. 247 | type TypeError struct { 248 | Errors []string 249 | } 250 | 251 | func (e *TypeError) Error() string { 252 | return fmt.Sprintf("yaml: unmarshal errors:\n %s", strings.Join(e.Errors, "\n ")) 253 | } 254 | 255 | // -------------------------------------------------------------------------- 256 | // Maintain a mapping of keys to structure field indexes 257 | 258 | // The code in this section was copied from mgo/bson. 259 | 260 | // structInfo holds details for the serialization of fields of 261 | // a given struct. 262 | type structInfo struct { 263 | Type reflect.Type 264 | FieldsMap map[string]fieldInfo 265 | FieldsList []fieldInfo 266 | 267 | // InlineMap is the number of the field in the struct that 268 | // contains an ,inline map, or -1 if there's none. 269 | InlineMap int 270 | 271 | // This a list of fields with regexps that are tested during unmarshaling, 272 | // and when matched by a YAML key, will write the value to the designated 273 | // field value. This is check from top to bottom, the first match wins. 274 | // Exact key match (using FieldsMap) is checked before the regular 275 | // expression phase. 276 | RegexpFieldsList []fieldInfo 277 | } 278 | 279 | type fieldInfo struct { 280 | 281 | // YAML key to use for marshaling/unmarshaling of this field 282 | Key string 283 | 284 | // Index of the field in the struct 285 | Num int 286 | 287 | // When marshaling, whether to omit this field when it is set to Zero 288 | OmitEmpty bool 289 | 290 | // Whether to marhsal using the YAML flow style 291 | Flow bool 292 | 293 | // Regular expression that the YAML key must match for unmarshaling into 294 | // this field 295 | Regexp *regexp.Regexp 296 | 297 | // Inline holds the field index if the field is part of an inlined struct. 298 | Inline []int 299 | } 300 | 301 | var structMap = make(map[reflect.Type]*structInfo) 302 | var fieldMapMutex sync.RWMutex 303 | 304 | func getStructInfo(st reflect.Type) (*structInfo, error) { 305 | 306 | // Try and get the relevant structInfo 307 | fieldMapMutex.RLock() 308 | sinfo, found := structMap[st] 309 | fieldMapMutex.RUnlock() 310 | 311 | // Return it, if found 312 | if found { 313 | return sinfo, nil 314 | } 315 | 316 | // Otherwise, let's create it. 317 | n := st.NumField() 318 | fieldsMap := make(map[string]fieldInfo) 319 | fieldsList := make([]fieldInfo, 0, n) 320 | regexpFieldsList := make([]fieldInfo, 0) 321 | inlineMap := -1 322 | 323 | // Go over each field 324 | for i := 0; i != n; i++ { 325 | 326 | // Get the StructField 327 | field := st.Field(i) 328 | 329 | if field.PkgPath != "" { 330 | continue // Skip private fields 331 | } 332 | 333 | // Create a fieldInfo struct 334 | info := fieldInfo{Num: i} 335 | 336 | // Try and get the yaml tag from the field 337 | tag := field.Tag.Get("yaml") 338 | 339 | // An empty tag means a possibly badly formatted tag. We try and act nice 340 | if tag == "" { 341 | 342 | rawTagString := string(field.Tag) 343 | 344 | if strings.Index(string(field.Tag), ":") < 0 { 345 | // Handle tags with no yaml: prefix, just use the raw comment 346 | // tag string 347 | tag = rawTagString 348 | } else if strings.HasPrefix(rawTagString, "yaml:") { 349 | // Handle badly formatted yaml: tags (no quotes, for example) 350 | failf("Detected badly formatted tag for field %s; missing quotes?\n", 351 | field.Name) 352 | } 353 | 354 | // TODO: Consider whether we should be more strict: 355 | // if tag != "" { 356 | // return nil, 357 | // fmt.Errof("Badly formatted yaml tag detected: %s", 358 | // string(field.Tag) 359 | // } 360 | } 361 | 362 | // '-' means - skip this field 363 | if tag == "-" { 364 | continue 365 | } 366 | 367 | // First, try and see if we have a regexp flag set - if so, handle it. 368 | if strings.HasPrefix(tag, ",regexp:") { 369 | 370 | // Store just the pattern 371 | regex := tag[8:] 372 | 373 | // Compile parses a regular expression. Use it as the key in the 374 | // hash. 375 | compiledRegexp := regexp.MustCompile(regex) 376 | 377 | // Verify that the type is indeed a map or a slice 378 | if field.Type.Kind() != reflect.Map && 379 | field.Type.Kind() != reflect.Slice { 380 | 381 | // Die 382 | failf("field %s.%s has regexp flag set but is not a map or slice", 383 | st.Name(), field.Name) 384 | } 385 | 386 | info.Regexp = compiledRegexp 387 | regexpFieldsList = append(regexpFieldsList, info) 388 | continue 389 | } 390 | 391 | // Try and see what flags are set 392 | inline := false 393 | if fields := strings.Split(tag, ","); len(fields) > 1 { 394 | for _, flag := range fields[1:] { 395 | switch flag { 396 | 397 | // Only include the field if it's not set to the zero 398 | // value for the type or to empty slices or maps. 399 | // Does not apply to zero valued structs. [Marshaling] 400 | case "omitempty": 401 | info.OmitEmpty = true 402 | 403 | // Marshal using a flow style (useful for structs, sequences and 404 | // maps.) [Marshaling] 405 | case "flow": 406 | info.Flow = true 407 | 408 | // Inline the struct it's applied to, so its fields are processed 409 | // as if they were part of the outer struct. 410 | // [Marshaling, Unmarshaling] 411 | case "inline": 412 | inline = true 413 | 414 | // Unsupported flag? 415 | default: 416 | return nil, errors.New(fmt.Sprintf("Unsupported flag %q in tag %q of type %s", flag, tag, st)) 417 | } 418 | } 419 | tag = fields[0] 420 | } 421 | 422 | // Handle the struct fields as if they were part of the outer struct. 423 | if inline { 424 | switch field.Type.Kind() { 425 | // TODO: Implement support for inline maps. 426 | //case reflect.Map: 427 | // if inlineMap >= 0 { 428 | // return nil, errors.New("Multiple ,inline maps in struct " + st.String()) 429 | // } 430 | // if field.Type.Key() != reflect.TypeOf("") { 431 | // return nil, errors.New("Option ,inline needs a map with string keys in struct " + st.String()) 432 | // } 433 | // inlineMap = info.Num 434 | case reflect.Struct: 435 | sinfo, err := getStructInfo(field.Type) 436 | if err != nil { 437 | return nil, err 438 | } 439 | for _, finfo := range sinfo.FieldsList { 440 | if _, found := fieldsMap[finfo.Key]; found { 441 | msg := "Duplicated key '" + finfo.Key + "' in struct " + st.String() 442 | return nil, errors.New(msg) 443 | } 444 | if finfo.Inline == nil { 445 | finfo.Inline = []int{i, finfo.Num} 446 | } else { 447 | finfo.Inline = append([]int{i}, finfo.Inline...) 448 | } 449 | fieldsMap[finfo.Key] = finfo 450 | fieldsList = append(fieldsList, finfo) 451 | } 452 | default: 453 | //return nil, errors.New("Option ,inline needs a struct value or map field") 454 | return nil, errors.New("Option ,inline needs a struct value field") 455 | } 456 | continue 457 | } 458 | 459 | if tag != "" { 460 | // If we have a yaml tag with a custom mapping key, then use it 461 | info.Key = tag 462 | } else { 463 | // Otherwise, use the lower-case name of the field 464 | info.Key = strings.ToLower(field.Name) 465 | } 466 | 467 | // Search for duplicate mapping keys, error if found 468 | if _, found = fieldsMap[info.Key]; found { 469 | msg := "Duplicated key '" + info.Key + "' in struct " + st.String() 470 | return nil, errors.New(msg) 471 | } 472 | 473 | // Add the generated fieldInfo to the fields list and map 474 | fieldsList = append(fieldsList, info) 475 | fieldsMap[info.Key] = info 476 | } 477 | 478 | // Create a new structInfo object, with all the metadata we collected 479 | sinfo = &structInfo{st, fieldsMap, fieldsList, inlineMap, regexpFieldsList} 480 | 481 | // Set it to the struct map, return it 482 | fieldMapMutex.Lock() 483 | structMap[st] = sinfo 484 | fieldMapMutex.Unlock() 485 | 486 | return sinfo, nil 487 | } 488 | 489 | func isZero(v reflect.Value) bool { 490 | switch v.Kind() { 491 | case reflect.String: 492 | return len(v.String()) == 0 493 | case reflect.Interface, reflect.Ptr: 494 | return v.IsNil() 495 | case reflect.Slice: 496 | return v.Len() == 0 497 | case reflect.Map: 498 | return v.Len() == 0 499 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 500 | return v.Int() == 0 501 | case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: 502 | return v.Uint() == 0 503 | case reflect.Bool: 504 | return !v.Bool() 505 | } 506 | return false 507 | } 508 | -------------------------------------------------------------------------------- /Godeps/_workspace/src/github.com/buddhamagnet/yaml/yamlprivateh.go: -------------------------------------------------------------------------------- 1 | package yaml 2 | 3 | const ( 4 | // The size of the input raw buffer. 5 | input_raw_buffer_size = 512 6 | 7 | // The size of the input buffer. 8 | // It should be possible to decode the whole raw buffer. 9 | input_buffer_size = input_raw_buffer_size * 3 10 | 11 | // The size of the output buffer. 12 | output_buffer_size = 128 13 | 14 | // The size of the output raw buffer. 15 | // It should be possible to encode the whole output buffer. 16 | output_raw_buffer_size = (output_buffer_size*2 + 2) 17 | 18 | // The size of other stacks and queues. 19 | initial_stack_size = 16 20 | initial_queue_size = 16 21 | initial_string_size = 16 22 | ) 23 | 24 | // Check if the character at the specified position is an alphabetical 25 | // character, a digit, '_', or '-'. 26 | func is_alpha(b []byte, i int) bool { 27 | return b[i] >= '0' && b[i] <= '9' || b[i] >= 'A' && b[i] <= 'Z' || b[i] >= 'a' && b[i] <= 'z' || b[i] == '_' || b[i] == '-' 28 | } 29 | 30 | // Check if the character at the specified position is a digit. 31 | func is_digit(b []byte, i int) bool { 32 | return b[i] >= '0' && b[i] <= '9' 33 | } 34 | 35 | // Get the value of a digit. 36 | func as_digit(b []byte, i int) int { 37 | return int(b[i]) - '0' 38 | } 39 | 40 | // Check if the character at the specified position is a hex-digit. 41 | func is_hex(b []byte, i int) bool { 42 | return b[i] >= '0' && b[i] <= '9' || b[i] >= 'A' && b[i] <= 'F' || b[i] >= 'a' && b[i] <= 'f' 43 | } 44 | 45 | // Get the value of a hex-digit. 46 | func as_hex(b []byte, i int) int { 47 | bi := b[i] 48 | if bi >= 'A' && bi <= 'F' { 49 | return int(bi) - 'A' + 10 50 | } 51 | if bi >= 'a' && bi <= 'f' { 52 | return int(bi) - 'a' + 10 53 | } 54 | return int(bi) - '0' 55 | } 56 | 57 | // Check if the character is ASCII. 58 | func is_ascii(b []byte, i int) bool { 59 | return b[i] <= 0x7F 60 | } 61 | 62 | // Check if the character at the start of the buffer can be printed unescaped. 63 | func is_printable(b []byte, i int) bool { 64 | return ((b[i] == 0x0A) || // . == #x0A 65 | (b[i] >= 0x20 && b[i] <= 0x7E) || // #x20 <= . <= #x7E 66 | (b[i] == 0xC2 && b[i+1] >= 0xA0) || // #0xA0 <= . <= #xD7FF 67 | (b[i] > 0xC2 && b[i] < 0xED) || 68 | (b[i] == 0xED && b[i+1] < 0xA0) || 69 | (b[i] == 0xEE) || 70 | (b[i] == 0xEF && // #xE000 <= . <= #xFFFD 71 | !(b[i+1] == 0xBB && b[i+2] == 0xBF) && // && . != #xFEFF 72 | !(b[i+1] == 0xBF && (b[i+2] == 0xBE || b[i+2] == 0xBF)))) 73 | } 74 | 75 | // Check if the character at the specified position is NUL. 76 | func is_z(b []byte, i int) bool { 77 | return b[i] == 0x00 78 | } 79 | 80 | // Check if the beginning of the buffer is a BOM. 81 | func is_bom(b []byte, i int) bool { 82 | return b[0] == 0xEF && b[1] == 0xBB && b[2] == 0xBF 83 | } 84 | 85 | // Check if the character at the specified position is space. 86 | func is_space(b []byte, i int) bool { 87 | return b[i] == ' ' 88 | } 89 | 90 | // Check if the character at the specified position is tab. 91 | func is_tab(b []byte, i int) bool { 92 | return b[i] == '\t' 93 | } 94 | 95 | // Check if the character at the specified position is blank (space or tab). 96 | func is_blank(b []byte, i int) bool { 97 | //return is_space(b, i) || is_tab(b, i) 98 | return b[i] == ' ' || b[i] == '\t' 99 | } 100 | 101 | // Check if the character at the specified position is a line break. 102 | func is_break(b []byte, i int) bool { 103 | return (b[i] == '\r' || // CR (#xD) 104 | b[i] == '\n' || // LF (#xA) 105 | b[i] == 0xC2 && b[i+1] == 0x85 || // NEL (#x85) 106 | b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA8 || // LS (#x2028) 107 | b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA9) // PS (#x2029) 108 | } 109 | 110 | func is_crlf(b []byte, i int) bool { 111 | return b[i] == '\r' && b[i+1] == '\n' 112 | } 113 | 114 | // Check if the character is a line break or NUL. 115 | func is_breakz(b []byte, i int) bool { 116 | //return is_break(b, i) || is_z(b, i) 117 | return ( // is_break: 118 | b[i] == '\r' || // CR (#xD) 119 | b[i] == '\n' || // LF (#xA) 120 | b[i] == 0xC2 && b[i+1] == 0x85 || // NEL (#x85) 121 | b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA8 || // LS (#x2028) 122 | b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA9 || // PS (#x2029) 123 | // is_z: 124 | b[i] == 0) 125 | } 126 | 127 | // Check if the character is a line break, space, or NUL. 128 | func is_spacez(b []byte, i int) bool { 129 | //return is_space(b, i) || is_breakz(b, i) 130 | return ( // is_space: 131 | b[i] == ' ' || 132 | // is_breakz: 133 | b[i] == '\r' || // CR (#xD) 134 | b[i] == '\n' || // LF (#xA) 135 | b[i] == 0xC2 && b[i+1] == 0x85 || // NEL (#x85) 136 | b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA8 || // LS (#x2028) 137 | b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA9 || // PS (#x2029) 138 | b[i] == 0) 139 | } 140 | 141 | // Check if the character is a line break, space, tab, or NUL. 142 | func is_blankz(b []byte, i int) bool { 143 | //return is_blank(b, i) || is_breakz(b, i) 144 | return ( // is_blank: 145 | b[i] == ' ' || b[i] == '\t' || 146 | // is_breakz: 147 | b[i] == '\r' || // CR (#xD) 148 | b[i] == '\n' || // LF (#xA) 149 | b[i] == 0xC2 && b[i+1] == 0x85 || // NEL (#x85) 150 | b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA8 || // LS (#x2028) 151 | b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA9 || // PS (#x2029) 152 | b[i] == 0) 153 | } 154 | 155 | // Determine the width of the character. 156 | func width(b byte) int { 157 | // Don't replace these by a switch without first 158 | // confirming that it is being inlined. 159 | if b&0x80 == 0x00 { 160 | return 1 161 | } 162 | if b&0xE0 == 0xC0 { 163 | return 2 164 | } 165 | if b&0xF0 == 0xE0 { 166 | return 3 167 | } 168 | if b&0xF8 == 0xF0 { 169 | return 4 170 | } 171 | return 0 172 | 173 | } 174 | -------------------------------------------------------------------------------- /LICENCE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 The Economist 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This package is designed to work with [RAML](http://raml.org) files and build the appropriate handlers and routing rules in Go applications. 2 | 3 | [![GoDoc](https://godoc.org/github.com/EconomistDigitalSolutions/ramlapi?status.svg)](https://godoc.org/github.com/EconomistDigitalSolutions/ramlapi) 4 | 5 | The ramlapi codebase contains two packages: 6 | 7 | * Ramlapi - used to parse a RAML file and wire it up to a router. 8 | * Ramlgen - used to parse a RAML file and write a set of HTTP handlers. 9 | 10 | #### RAML Compatibility 11 | 12 | The current version of the Ramlapi and Ramlgen packages supports *most* of the 0.8 RAML specification. 13 | 14 | Our intention is to implement further support as: 15 | 16 | 1. Preliminary 1.0 support 17 | 2. Full 1.0 support 18 | 3. Additional 0.8 support 19 | 20 | Enhancing 0.8 support is a low priority as users are strongly urged to migrate to 1.0 as soon as possible 21 | 22 | #### HOW TO RAML-GEN 23 | 24 | 1. Build your API design in RAML. 25 | 2. Inside your Go project, run `raml-gen --ramlfile=`. 26 | 3. Copy the resulting `handlers_gen.go` file to the correct location. 27 | 28 | You now have a set of HTTP handlers built from your RAML specification. 29 | 30 | Right now raml-gen only supports routers that use the standard http.Handlerfunc handlers. If you want to use something like 31 | Echo or HttpRouter you'll need to make some amendments as described in the examples below. 32 | 33 | The handlers map generated in `handlers_gen.go` contains camel-cased 34 | key/value names derived from the `displayName` property in your RAML file. 35 | 36 | This RAML fragment: 37 | 38 | ```yaml 39 | /version: 40 | get: 41 | displayName: Version info 42 | /docs: 43 | get: 44 | displayName: documentation 45 | ``` 46 | 47 | Will generates this handler map: 48 | 49 | ```go 50 | // RouteMap maps RAML identifiers to application handlers. 51 | var RouteMap = map[string]http.HandlerFunc{ 52 | "VersionInfo": VersionInfo, 53 | "Documentation": Documentation, 54 | } 55 | ``` 56 | 57 | #### HOW TO RAMLAPI 58 | 59 | The ramlapi package makes no assumptions about your choice of router as the 60 | method to wire up the API takes a function provided by your code and 61 | passes details of the API back to that function on each resource defined 62 | in the RAML file. The router can then hook the data up however it likes. 63 | 64 | #### EXAMPLES 65 | 66 | ##### STANDARD LIBRARY 67 | 68 | ```go 69 | 70 | var RouteMap = map[string]http.HandlerFunc{ 71 | 72 | "Root": Root, 73 | "Version": Version, 74 | } 75 | 76 | func main() { 77 | router := http.NewServeMux() 78 | api, _ := ramlapi.Process("api.raml") 79 | 80 | ramlapi.Build(api, routerFunc) 81 | log.Fatal(http.ListenAndServe(":9494", router)) 82 | } 83 | 84 | func routerFunc(ep *ramlapi.Endpoint) { 85 | handler := http.HandlerFunc(RouteMap[ep.Handler]) 86 | router.Handle(ep.Path, handler) 87 | } 88 | 89 | ``` 90 | 91 | ##### PAT 92 | 93 | ```go 94 | 95 | var RouteMap = map[string]http.HandlerFunc{ 96 | 97 | "Root": Root, 98 | "Version": Version, 99 | } 100 | 101 | func Version(w http.ResponseWriter, r *http.Request) { 102 | w.WriteHeader(http.StatusOK) 103 | } 104 | 105 | func Root(w http.ResponseWriter, r *http.Request) { 106 | w.WriteHeader(http.StatusOK) 107 | } 108 | 109 | func main() { 110 | router := pat.New() 111 | api, _ := ramlapi.Process("api.raml") 112 | 113 | ramlapi.Build(api, routerFunc) 114 | log.Fatal(http.ListenAndServe(":9494", router)) 115 | } 116 | 117 | func routerFunc(ep *ramlapi.Endpoint) { 118 | router.Add(ep.Verb, ep.Path, RouteMap[ep.Handler]) 119 | } 120 | ``` 121 | 122 | ##### GORILLA MUX 123 | 124 | ```go 125 | 126 | var RouteMap = map[string]http.HandlerFunc{ 127 | 128 | "Root": Root, 129 | "Version": Version, 130 | } 131 | 132 | func Version(w http.ResponseWriter, r *http.Request) { 133 | w.WriteHeader(http.StatusOK) 134 | } 135 | 136 | func Root(w http.ResponseWriter, r *http.Request) { 137 | w.WriteHeader(http.StatusOK) 138 | } 139 | 140 | func main() { 141 | router := mux.NewRouter().StrictSlash(true) 142 | api, _ := ramlapi.Process("api.raml") 143 | 144 | ramlapi.Build(api, routerFunc) 145 | log.Fatal(http.ListenAndServe(":9494", router)) 146 | 147 | } 148 | 149 | func routerFunc(ep *ramlapi.Endpoint) { 150 | path := ep.Path 151 | 152 | for _, up := range ep.URIParameters { 153 | if up.Pattern != "" { 154 | path = strings.Replace( 155 | path, 156 | fmt.Sprintf("{%s}", up.Key), 157 | fmt.Sprintf("{%s:%s}", up.Key, up.Pattern), 158 | 1) 159 | } 160 | } 161 | 162 | route := router. 163 | Methods(ep.Verb). 164 | Path(path). 165 | Handler(RouteMap[ep.Handler]) 166 | 167 | for _, qp := range ep.QueryParameters { 168 | if qp.Required { 169 | if qp.Pattern != "" { 170 | route.Queries(qp.Key, fmt.Sprintf("{%s:%s}", qp.Key, qp.Pattern)) 171 | } else { 172 | route.Queries(qp.Key, "") 173 | } 174 | } 175 | } 176 | } 177 | ``` 178 | 179 | ##### ECHO 180 | 181 | ```go 182 | 183 | var RouteMap = map[string]func(c *echo.Context) error{ 184 | 185 | "Root": Root, 186 | "Version": Version, 187 | } 188 | 189 | func Version(c *echo.Context) error { 190 | return c.String(http.StatusOK, "VERSION") 191 | } 192 | 193 | func Root(c *echo.Context) error { 194 | return c.String(http.StatusOK, "HOME") 195 | } 196 | 197 | func main() { 198 | router := echo.New() 199 | 200 | api, _ := ramlapi.ProcessRAML("api.raml") 201 | 202 | ramlapi.Build(api, routerFunc) 203 | 204 | router.Run(":9494") 205 | } 206 | 207 | func routerFunc(ep *ramlapi.Endpoint) { 208 | switch ep.Verb { 209 | case "GET": 210 | router.Get(ep.Path, RouteMap[ep.Handler]) 211 | } 212 | } 213 | ``` 214 | 215 | ##### HTTPROUTER 216 | 217 | ```go 218 | 219 | var RouteMap = map[string]httprouter.Handle{ 220 | 221 | "Root": Root, 222 | "Version": Version, 223 | } 224 | 225 | func Version(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { 226 | fmt.Fprint(w, "VERSION\n") 227 | } 228 | 229 | func Root(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { 230 | fmt.Fprint(w, "HOME\n") 231 | } 232 | 233 | func main() { 234 | api, _ := ramlapi.ProcessRAML("api.raml") 235 | 236 | router := httprouter.New() 237 | ramlapi.Build(api, routerFunc) 238 | 239 | log.Fatal(http.ListenAndServe(":9494", router)) 240 | } 241 | 242 | func routerFunc(ep *ramlapi.Endpoint) { 243 | switch ep.Verb { 244 | case "GET": 245 | router.GET(ep.Path, RouteMap[ep.Handler]) 246 | } 247 | } 248 | ``` 249 | -------------------------------------------------------------------------------- /fixtures/valid.raml: -------------------------------------------------------------------------------- 1 | #%RAML 0.8 2 | title: ramlapi 3 | version: 1 4 | 5 | baseUri: http://github.com/buddhamagnet/ramlapi 6 | documentation: 7 | - title: Overview 8 | 9 | /testapi: 10 | description: Test API for the ramlapi package. 11 | get: 12 | displayName: get 13 | post: 14 | displayName: post 15 | put: 16 | displayName: put 17 | patch: 18 | displayName: patch 19 | head: 20 | displayName: head 21 | delete: 22 | displayName: delete 23 | -------------------------------------------------------------------------------- /ramlapi.go: -------------------------------------------------------------------------------- 1 | package ramlapi 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "log" 7 | "regexp" 8 | "strings" 9 | 10 | "github.com/buddhamagnet/raml" 11 | ) 12 | 13 | var vizer *regexp.Regexp 14 | 15 | func init() { 16 | vizer = regexp.MustCompile("[^A-Za-z0-9]+") 17 | } 18 | 19 | // Parameter is a path or query string parameter. 20 | type Parameter struct { 21 | Key string 22 | Type string 23 | Pattern string 24 | Required bool 25 | } 26 | 27 | // Endpoint describes an API endpoint. 28 | type Endpoint struct { 29 | Verb string 30 | Handler string 31 | Path string 32 | Description string 33 | URIParameters []*Parameter 34 | QueryParameters []*Parameter 35 | } 36 | 37 | // String returns the string representation of an Endpoint. 38 | func (e *Endpoint) String() string { 39 | return fmt.Sprintf("verb: %s handler: %s path:%s\n", e.Verb, e.Handler, e.Path) 40 | } 41 | 42 | func (e *Endpoint) setQueryParameters(method *raml.Method) { 43 | for name, param := range method.QueryParameters { 44 | e.QueryParameters = append(e.QueryParameters, newParam(name, ¶m)) 45 | } 46 | } 47 | 48 | // Build takes a RAML API definition, a router and a routing map, 49 | // and wires them all together. 50 | func Build(api *raml.APIDefinition, routerFunc func(s *Endpoint)) error { 51 | for name, resource := range api.Resources { 52 | var resourceParams []*Parameter 53 | err := processResource("", name, &resource, resourceParams, routerFunc) 54 | if err != nil { 55 | return err 56 | } 57 | } 58 | 59 | return nil 60 | } 61 | 62 | // Process processes a RAML file and returns an API definition. 63 | func Process(file string) (*raml.APIDefinition, error) { 64 | routes, err := raml.ParseFile(file) 65 | if err != nil { 66 | return nil, fmt.Errorf("Failed parsing RAML file: %s\n", err.Error()) 67 | } 68 | return routes, nil 69 | } 70 | 71 | // Variableize normalises RAML display names. 72 | func Variableize(s string) string { 73 | return vizer.ReplaceAllString(strings.Title(s), "") 74 | } 75 | 76 | func newParam(name string, param *raml.NamedParameter) *Parameter { 77 | p := &Parameter{ 78 | Key: name, 79 | Type: param.Type, 80 | Required: param.Required, 81 | } 82 | if param.Pattern != nil { 83 | p.Pattern = *param.Pattern 84 | } 85 | 86 | return p 87 | } 88 | 89 | func appendEndpoint(s []*Endpoint, method *raml.Method, params []*Parameter) ([]*Endpoint, error) { 90 | if method.DisplayName == "" { 91 | return s, errors.New("DisplayName property not set in RAML method") 92 | } 93 | 94 | if method != nil { 95 | ep := &Endpoint{ 96 | Verb: method.Name, 97 | Handler: Variableize(method.DisplayName), 98 | Description: method.Description, 99 | } 100 | // set query parameters 101 | ep.setQueryParameters(method) 102 | // set uri parameters 103 | for _, param := range params { 104 | ep.URIParameters = append(ep.URIParameters, param) 105 | } 106 | s = append(s, ep) 107 | } 108 | 109 | return s, nil 110 | } 111 | 112 | // processResource recursively process resources and their nested children 113 | // and returns the path so far for the children. The function takes a routerFunc 114 | // as an argument that is invoked with the verb, resource path and handler as 115 | // the resources are processed, so the calling code can use pat, mux, httprouter 116 | // or whatever router they desire and we don't need to know about it. 117 | func processResource(parent, name string, resource *raml.Resource, params []*Parameter, routerFunc func(s *Endpoint)) error { 118 | var path = parent + name 119 | var err error 120 | for name, param := range resource.UriParameters { 121 | params = append(params, newParam(name, ¶m)) 122 | } 123 | 124 | s := make([]*Endpoint, 0, 6) 125 | for _, m := range resource.Methods() { 126 | s, err = appendEndpoint(s, m, params) 127 | if err != nil { 128 | return err 129 | } 130 | } 131 | 132 | for _, ep := range s { 133 | ep.Path = path 134 | log.Println("processing", ep) 135 | routerFunc(ep) 136 | } 137 | 138 | // Get all children. 139 | for nestname, nested := range resource.Nested { 140 | return processResource(path, nestname, nested, params, routerFunc) 141 | } 142 | 143 | return nil 144 | } 145 | -------------------------------------------------------------------------------- /ramlapi_test.go: -------------------------------------------------------------------------------- 1 | package ramlapi_test 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | 7 | . "github.com/EconomistDigitalSolutions/ramlapi" 8 | "github.com/buddhamagnet/raml" 9 | ) 10 | 11 | var ( 12 | endpoints []*Endpoint 13 | patt1 string 14 | patt2 string 15 | ) 16 | 17 | func init() { 18 | patt1 = "[a-z]" 19 | patt2 = "[0-9]" 20 | } 21 | 22 | func testFunc(ep *Endpoint) { 23 | endpoints = append(endpoints, ep) 24 | } 25 | 26 | // TestData used to test raml.APIDefinition to ramlapi.Endpoints. The order 27 | // of expected methods is important (GET, POST, PUT, PATCH, HEAD, DELETE). 28 | var TestData = []struct { 29 | api *raml.APIDefinition 30 | expected []map[string]interface{} 31 | }{ 32 | { 33 | // test simple endpoint 34 | &raml.APIDefinition{ 35 | Resources: map[string]raml.Resource{ 36 | "/test": raml.Resource{ 37 | Post: &raml.Method{ 38 | Name: "POST", 39 | DisplayName: "Post me", 40 | }, 41 | Get: &raml.Method{ 42 | Name: "GET", 43 | DisplayName: "Get me", 44 | }, 45 | }, 46 | }, 47 | }, 48 | []map[string]interface{}{ 49 | {"verb": "GET", "handler": "GetMe", "path": "/test"}, 50 | {"verb": "POST", "handler": "PostMe", "path": "/test"}, 51 | }, 52 | }, 53 | { 54 | // test URI parameters with nested resource 55 | &raml.APIDefinition{ 56 | Resources: map[string]raml.Resource{ 57 | "/{foo}": raml.Resource{ 58 | Get: &raml.Method{ 59 | Name: "GET", 60 | DisplayName: "Get me", 61 | }, 62 | UriParameters: map[string]raml.NamedParameter{ 63 | "foo": raml.NamedParameter{ 64 | Pattern: &patt1, 65 | }, 66 | }, 67 | Nested: map[string]*raml.Resource{ 68 | "/{bar}": &raml.Resource{ 69 | Get: &raml.Method{ 70 | Name: "GET", 71 | DisplayName: "Nested get", 72 | }, 73 | UriParameters: map[string]raml.NamedParameter{ 74 | "bar": raml.NamedParameter{ 75 | Pattern: &patt2, 76 | }, 77 | }, 78 | }, 79 | }, 80 | }, 81 | }, 82 | }, 83 | []map[string]interface{}{ 84 | { 85 | "verb": "GET", 86 | "handler": "GetMe", 87 | "path": "/{foo}", 88 | "uri_params": []map[string]string{ 89 | { 90 | "key": "foo", 91 | "pattern": "[a-z]", 92 | }, 93 | }, 94 | }, 95 | { 96 | "verb": "GET", 97 | "handler": "NestedGet", 98 | "path": "/{foo}/{bar}", 99 | "uri_params": []map[string]string{ 100 | { 101 | "key": "foo", 102 | "pattern": "[a-z]", 103 | }, 104 | { 105 | "key": "bar", 106 | "pattern": "[0-9]", 107 | }, 108 | }, 109 | }, 110 | }, 111 | }, 112 | { 113 | // test query parameters 114 | &raml.APIDefinition{ 115 | Resources: map[string]raml.Resource{ 116 | "/query": raml.Resource{ 117 | Get: &raml.Method{ 118 | Name: "GET", 119 | DisplayName: "Get me", 120 | QueryParameters: map[string]raml.NamedParameter{ 121 | "foo": raml.NamedParameter{ 122 | Pattern: &patt1, 123 | Required: true, 124 | }, 125 | "bar": raml.NamedParameter{ 126 | Pattern: &patt2, 127 | Required: false, 128 | }, 129 | }, 130 | }, 131 | }, 132 | }, 133 | }, 134 | []map[string]interface{}{ 135 | { 136 | "verb": "GET", 137 | "handler": "GetMe", 138 | "path": "/query", 139 | "query_params": []map[string]string{ 140 | { 141 | "key": "foo", 142 | "pattern": "[a-z]", 143 | "required": "true", 144 | }, 145 | { 146 | "key": "bar", 147 | "pattern": "[0-9]", 148 | "required": "false", 149 | }, 150 | }, 151 | }, 152 | }, 153 | }, 154 | } 155 | 156 | func TestProcess(t *testing.T) { 157 | _, err := Process("fixtures/valid.raml") 158 | if err != nil { 159 | t.Error("could not process valid RAML file") 160 | } 161 | } 162 | 163 | func TestEndpoints(t *testing.T) { 164 | for _, data := range TestData { 165 | Build(data.api, testFunc) 166 | if !checkEndpoints(t, data.expected, endpoints) { 167 | t.Errorf("expected endpoint with: %s", data.expected) 168 | } 169 | endpoints = make([]*Endpoint, 0) 170 | } 171 | } 172 | 173 | func checkEndpoints(t *testing.T, exp []map[string]interface{}, got []*Endpoint) bool { 174 | var foundHandler, foundPath, foundVerb bool 175 | var found int 176 | 177 | if len(exp) != len(got) { 178 | t.Errorf("expected %d endpoints, got %d", len(exp), len(got)) 179 | } 180 | 181 | for _, e := range exp { 182 | for _, ep := range got { 183 | 184 | foundHandler = true 185 | if ep.Handler != e["handler"] { 186 | foundHandler = false 187 | } 188 | 189 | foundPath = true 190 | if ep.Path != e["path"] { 191 | foundPath = false 192 | } 193 | 194 | foundVerb = true 195 | if ep.Verb != e["verb"] { 196 | foundVerb = false 197 | } 198 | 199 | if foundHandler && foundPath && foundVerb { 200 | found += 1 201 | if uParams, ok := e["uri_params"]; ok { 202 | u := uParams.([]map[string]string) 203 | if !checkParameters(t, u, ep.URIParameters) { 204 | t.Errorf("expected uri parameters: %s", u) 205 | } 206 | } 207 | if qParams, ok := e["query_params"]; ok { 208 | q := qParams.([]map[string]string) 209 | if !checkParameters(t, q, ep.QueryParameters) { 210 | t.Errorf("expected query parameters: %s", q) 211 | } 212 | } 213 | } 214 | } 215 | } 216 | 217 | return found == len(exp) 218 | } 219 | 220 | func checkParameters(t *testing.T, exp []map[string]string, got []*Parameter) bool { 221 | var foundKey, foundPatt, foundReq bool 222 | var found int 223 | 224 | if len(got) != len(exp) { 225 | t.Errorf("expected %d parameters, got %d", len(exp), len(got)) 226 | } 227 | 228 | for _, expParam := range exp { 229 | for _, gotParam := range got { 230 | foundKey = true 231 | if key, ok := expParam["key"]; ok { 232 | if key != gotParam.Key { 233 | foundKey = false 234 | } 235 | } 236 | 237 | foundPatt = true 238 | if patt, ok := expParam["pattern"]; ok { 239 | if patt != gotParam.Pattern { 240 | foundPatt = false 241 | } 242 | } 243 | 244 | foundReq = true 245 | if req, ok := expParam["required"]; ok { 246 | // convert bool to string 247 | gotReq := fmt.Sprintf("%t", gotParam.Required) 248 | if req != gotReq { 249 | foundReq = false 250 | } 251 | } 252 | 253 | if foundKey && foundPatt && foundReq { 254 | found += 1 255 | } 256 | } 257 | } 258 | 259 | return found == len(exp) 260 | } 261 | -------------------------------------------------------------------------------- /ramlgen/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "log" 6 | "os" 7 | "os/exec" 8 | "text/template" 9 | 10 | "github.com/EconomistDigitalSolutions/ramlapi" 11 | "github.com/buddhamagnet/raml" 12 | ) 13 | 14 | var ( 15 | ramlFile string 16 | genFile string 17 | ) 18 | 19 | // RouteMapEntry represents an entry in a route map. 20 | type RouteMapEntry struct { 21 | Name, Struct string 22 | } 23 | 24 | // HandlerInfo contains handler information. 25 | type HandlerInfo struct { 26 | Name, Verb, Path, Doc string 27 | } 28 | 29 | func init() { 30 | flag.StringVar(&ramlFile, "ramlfile", "api.raml", "RAML file to parse") 31 | flag.StringVar(&genFile, "genfile", "handlers_gen.go", "Filename to use for output") 32 | } 33 | 34 | func main() { 35 | flag.Parse() 36 | api, err := ramlapi.Process(ramlFile) 37 | if err != nil { 38 | log.Fatal(err) 39 | } 40 | log.Println("Processing API spec for", ramlFile) 41 | generate(api, genFile) 42 | log.Println("Created handlers in ", genFile) 43 | } 44 | 45 | // Generate handler functions based on an API definition. 46 | func generate(api *raml.APIDefinition, genFile string) { 47 | 48 | f, err := os.Create(genFile) 49 | if err != nil { 50 | log.Fatal(err) 51 | } 52 | defer f.Close() 53 | // Write the header - import statements and root handler. 54 | f.WriteString(handlerHead) 55 | // Start the route map (string to handler). 56 | f.WriteString(mapStart) 57 | // Add the route map entries. 58 | e := template.Must(template.New("mapEntry").Parse(mapEntry)) 59 | for name, resource := range api.Resources { 60 | generateMap("", name, &resource, e, f) 61 | } 62 | // Close the route map. 63 | f.WriteString(mapEnd) 64 | // Now add the HTTP handlers. 65 | t := template.Must(template.New("handlerText").Parse(handlerText)) 66 | for name, resource := range api.Resources { 67 | generateResource("", name, &resource, t, f) 68 | } 69 | format(f) 70 | } 71 | 72 | // format runs go fmt on a file. 73 | func format(f *os.File) { 74 | // Run go fmt on the file. 75 | cmd := exec.Command("go", "fmt") 76 | cmd.Stdin = f 77 | _ = cmd.Run() 78 | } 79 | 80 | // generateResource creates a handler struct from an API resource 81 | // and executes the associated template. 82 | func generateResource(parent, name string, resource *raml.Resource, t *template.Template, f *os.File) string { 83 | path := parent + name 84 | 85 | for _, method := range resource.Methods() { 86 | err := t.Execute(f, HandlerInfo{ramlapi.Variableize(method.DisplayName), method.Name, path, method.Description}) 87 | if err != nil { 88 | log.Println("executing template:", err) 89 | } 90 | } 91 | 92 | // Get all children. 93 | for nestname, nested := range resource.Nested { 94 | return generateResource(path, nestname, nested, t, f) 95 | } 96 | return path 97 | } 98 | 99 | // generateMap builds a map of string labels to handler funcs - this is 100 | // used by the calling code to link the display name strings that come 101 | // from the RAML file to handler funcs in the client code. 102 | func generateMap(parent, name string, resource *raml.Resource, e *template.Template, f *os.File) { 103 | path := parent + name 104 | 105 | for _, method := range resource.Methods() { 106 | name := ramlapi.Variableize(method.DisplayName) 107 | err := e.Execute(f, RouteMapEntry{name, name}) 108 | if err != nil { 109 | log.Println("executing template:", err) 110 | } 111 | } 112 | 113 | // Get all children. 114 | for nestname, nested := range resource.Nested { 115 | generateMap(path, nestname, nested, e, f) 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /ramlgen/ramlgen_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | "testing" 7 | "time" 8 | 9 | "github.com/EconomistDigitalSolutions/ramlapi" 10 | ) 11 | 12 | var output = "/%s/test_gen_%d" 13 | 14 | func TestGenerate(t *testing.T) { 15 | api, _ := ramlapi.Process("../fixtures/valid.raml") 16 | currentOutput := fmt.Sprintf(output, os.TempDir(), int32(time.Now().Unix())) 17 | generate(api, currentOutput) 18 | _, err := os.Open(currentOutput) 19 | if err != nil { 20 | t.Fatalf("Expected output file to exist, got %v\n", err) 21 | } 22 | os.Remove(currentOutput) 23 | } 24 | -------------------------------------------------------------------------------- /ramlgen/templates.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | const handlerHead = `package main 4 | 5 | import ( 6 | "encoding/json" 7 | "net/http" 8 | ) 9 | ` 10 | 11 | const mapStart = ` 12 | var RouteMap = map[string]http.HandlerFunc{ 13 | ` 14 | 15 | const mapEntry = ` 16 | "{{.Name}}": {{.Struct}}, 17 | ` 18 | 19 | const mapEnd = ` 20 | } 21 | ` 22 | 23 | const handlerText = ` 24 | // {{.Name}} - handler for URI {{.Path}} HTTP verb {{.Verb}} 25 | // {{.Doc}} 26 | func {{.Name}}(w http.ResponseWriter, r *http.Request) { 27 | json, _ := json.Marshal(map[string]string{ 28 | "message": "{{.Name}}{{.Verb}}", 29 | }) 30 | w.Write(json) 31 | } 32 | ` 33 | --------------------------------------------------------------------------------