|
| 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 | +// This package contains the parser, validator and types that implement the |
| 29 | +// RAML specification, as documented here: |
| 30 | +// http://raml.org/spec.html |
| 31 | +package raml |
| 32 | + |
| 33 | +import ( |
| 34 | + "bufio" |
| 35 | + "bytes" |
| 36 | + "errors" |
| 37 | + "fmt" |
| 38 | + "io" |
| 39 | + "io/ioutil" |
| 40 | + "path/filepath" |
| 41 | + "strings" |
| 42 | + |
| 43 | + yaml "github.com/advance512/yaml" |
| 44 | + "github.com/kr/pretty" |
| 45 | +) |
| 46 | + |
| 47 | +// A RamlError is returned by the ParseFile function when RAML or YAML problems |
| 48 | +// are encountered when parsing the RAML document. |
| 49 | +// When this error is returned, the value is still parsed partially. |
| 50 | +type RamlError struct { |
| 51 | + Errors []string |
| 52 | +} |
| 53 | + |
| 54 | +// Parse a RAML file. Returns a raml.APIDefinition value or an error if |
| 55 | +// everything is something went wrong. |
| 56 | +func ParseFile(filePath string) (*APIDefinition, error) { |
| 57 | + |
| 58 | + // Get the working directory |
| 59 | + workingDirectory, fileName := filepath.Split(filePath) |
| 60 | + |
| 61 | + // Read original file contents into a byte array |
| 62 | + mainFileBytes, err := readFileContents(workingDirectory, fileName) |
| 63 | + |
| 64 | + if err != nil { |
| 65 | + return nil, err |
| 66 | + } |
| 67 | + |
| 68 | + // Get the contents of the main file |
| 69 | + mainFileBuffer := bytes.NewBuffer(mainFileBytes) |
| 70 | + |
| 71 | + // Verify the YAML version |
| 72 | + var ramlVersion string |
| 73 | + if firstLine, err := mainFileBuffer.ReadString('\n'); err != nil { |
| 74 | + return nil, fmt.Errorf("Problem reading RAML file (Error: %s)", err.Error()) |
| 75 | + } else { |
| 76 | + |
| 77 | + // We read some data... |
| 78 | + if len(firstLine) >= 10 { |
| 79 | + ramlVersion = firstLine[:10] |
| 80 | + } |
| 81 | + |
| 82 | + // TODO: Make this smart. We probably won't support multiple RAML |
| 83 | + // versions in the same package - we'll have different branches |
| 84 | + // for different versions. This one is hard-coded to 0.8. |
| 85 | + // Still, would be good to think about this. |
| 86 | + if ramlVersion != "#%RAML 0.8" { |
| 87 | + return nil, errors.New("Input file is not a RAML 0.8 file. Make " + |
| 88 | + "sure the file starts with #%RAML 0.8") |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // Pre-process the original file, following !include directive |
| 93 | + preprocessedContentsBytes, err := |
| 94 | + preProcess(mainFileBuffer, workingDirectory) |
| 95 | + |
| 96 | + if err != nil { |
| 97 | + return nil, |
| 98 | + fmt.Errorf("Error preprocessing RAML file (Error: %s)", err.Error()) |
| 99 | + } |
| 100 | + |
| 101 | + pretty.Println(string(preprocessedContentsBytes)) |
| 102 | + |
| 103 | + // Unmarshal into an APIDefinition value |
| 104 | + apiDefinition := new(APIDefinition) |
| 105 | + apiDefinition.RAMLVersion = ramlVersion |
| 106 | + |
| 107 | + // Go! |
| 108 | + err = yaml.Unmarshal(preprocessedContentsBytes, apiDefinition) |
| 109 | + |
| 110 | + // Any errors? |
| 111 | + if err != nil { |
| 112 | + |
| 113 | + return nil, fmt.Errorf("Problems parsing RAML:\n %s", err.Error()) |
| 114 | + } |
| 115 | + |
| 116 | + // Good. |
| 117 | + return apiDefinition, nil |
| 118 | + |
| 119 | +} |
| 120 | + |
| 121 | +// Reads the contents of a file, returns a bytes buffer |
| 122 | +func readFileContents(workingDirectory string, fileName string) ([]byte, error) { |
| 123 | + |
| 124 | + filePath := filepath.Join(workingDirectory, fileName) |
| 125 | + |
| 126 | + if fileName == "" { |
| 127 | + return nil, fmt.Errorf("File name cannot be nil: %s", filePath) |
| 128 | + } |
| 129 | + |
| 130 | + // Read the file |
| 131 | + fileContentsArray, err := ioutil.ReadFile(filePath) |
| 132 | + if err != nil { |
| 133 | + return nil, |
| 134 | + fmt.Errorf("Could not read file %s (Error: %s)", |
| 135 | + filePath, err.Error()) |
| 136 | + } |
| 137 | + |
| 138 | + return fileContentsArray, nil |
| 139 | +} |
| 140 | + |
| 141 | +// preProcess acts as a preprocessor for a RAML document in YAML format, |
| 142 | +// including files referenced via !include. It returns a pre-processed document. |
| 143 | +func preProcess(originalContents io.Reader, workingDirectory string) ([]byte, error) { |
| 144 | + |
| 145 | + // NOTE: Since YAML doesn't support !include directives, and since go-yaml |
| 146 | + // does NOT play nice with !include tags, this has to be done like this. |
| 147 | + // I am considering modifying go-yaml to add custom handlers for specific |
| 148 | + // tags, to add support for !include, but for now - this method is |
| 149 | + // GoodEnough(TM) and since it will only happen once, I am not prematurely |
| 150 | + // optimizing it. |
| 151 | + |
| 152 | + var preprocessedContents bytes.Buffer |
| 153 | + |
| 154 | + // Go over each line, looking for !include tags |
| 155 | + scanner := bufio.NewScanner(originalContents) |
| 156 | + var line string |
| 157 | + |
| 158 | + // Scan the file until we reach EOF or error out |
| 159 | + for scanner.Scan() { |
| 160 | + line = scanner.Text() |
| 161 | + |
| 162 | + // Did we find an !include directive to handle? |
| 163 | + if idx := strings.Index(line, "!include"); idx != -1 { |
| 164 | + |
| 165 | + // TODO: Do this better |
| 166 | + includeLength := len("!include ") |
| 167 | + |
| 168 | + includedFile := line[idx+includeLength:] |
| 169 | + |
| 170 | + preprocessedContents.Write([]byte(line[:idx])) |
| 171 | + |
| 172 | + // Get the included file contents |
| 173 | + includedContents, err := |
| 174 | + readFileContents(workingDirectory, includedFile) |
| 175 | + |
| 176 | + if err != nil { |
| 177 | + return nil, |
| 178 | + fmt.Errorf("Error including file %s:\n %s", |
| 179 | + includedFile, err.Error()) |
| 180 | + } |
| 181 | + |
| 182 | + // TODO: Check that you only insert .yaml, .raml, .txt and .md files |
| 183 | + // In case of .raml or .yaml, remove the comments |
| 184 | + // In case of other files, Base64 them first. |
| 185 | + |
| 186 | + // TODO: Better, step by step checks .. though prolly it'll panic |
| 187 | + // Write text files in the same indentation as the first line |
| 188 | + internalScanner := |
| 189 | + bufio.NewScanner(bytes.NewBuffer(includedContents)) |
| 190 | + |
| 191 | + // Indent by this much |
| 192 | + firstLine := true |
| 193 | + indentationString := "" |
| 194 | + |
| 195 | + // Go over each line, write it |
| 196 | + for internalScanner.Scan() { |
| 197 | + internalLine := internalScanner.Text() |
| 198 | + |
| 199 | + preprocessedContents.WriteString(indentationString) |
| 200 | + if firstLine { |
| 201 | + indentationString = strings.Repeat(" ", idx) |
| 202 | + firstLine = false |
| 203 | + } |
| 204 | + |
| 205 | + preprocessedContents.WriteString(internalLine) |
| 206 | + preprocessedContents.WriteByte('\n') |
| 207 | + } |
| 208 | + |
| 209 | + } else { |
| 210 | + |
| 211 | + // No, just a simple line.. write it |
| 212 | + preprocessedContents.WriteString(line) |
| 213 | + preprocessedContents.WriteByte('\n') |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + // Any errors encountered? |
| 218 | + if err := scanner.Err(); err != nil { |
| 219 | + return nil, fmt.Errorf("Error reading YAML file: %s", err.Error()) |
| 220 | + } |
| 221 | + |
| 222 | + // Return the preprocessed contents |
| 223 | + return preprocessedContents.Bytes(), nil |
| 224 | +} |
0 commit comments