vendor hcl

This commit is contained in:
Alex Dadgar 2016-06-13 18:15:41 -07:00
parent 57931b4bf0
commit 48d27fc5f8
7 changed files with 65 additions and 23 deletions

View file

@ -1,7 +0,0 @@
y.output
# ignore intellij files
.idea
*.iml
*.ipr
*.iws

View file

@ -1,3 +0,0 @@
sudo: false
language: go
go: 1.5

View file

@ -29,7 +29,7 @@ and some people wanted machine-friendly languages.
JSON fits a nice balance in this, but is fairly verbose and most JSON fits a nice balance in this, but is fairly verbose and most
importantly doesn't support comments. With YAML, we found that beginners importantly doesn't support comments. With YAML, we found that beginners
had a really hard time determining what the actual structure was, and had a really hard time determining what the actual structure was, and
ended up guessing more than not whether to use a hyphen, colon, etc. ended up guessing more often than not whether to use a hyphen, colon, etc.
in order to represent some configuration key. in order to represent some configuration key.
Full programming languages such as Ruby enable complex behavior Full programming languages such as Ruby enable complex behavior

View file

@ -21,6 +21,17 @@ var (
nodeType reflect.Type = findNodeType() nodeType reflect.Type = findNodeType()
) )
// Unmarshal accepts a byte slice as input and writes the
// data to the value pointed to by v.
func Unmarshal(bs []byte, v interface{}) error {
root, err := parse(bs)
if err != nil {
return err
}
return DecodeObject(v, root)
}
// Decode reads the given input and decodes it into the structure // Decode reads the given input and decodes it into the structure
// given by `out`. // given by `out`.
func Decode(out interface{}, in string) error { func Decode(out interface{}, in string) error {
@ -326,6 +337,14 @@ func (d *decoder) decodeMap(name string, node ast.Node, result reflect.Value) er
continue continue
} }
// github.com/hashicorp/terraform/issue/5740
if len(item.Keys) == 0 {
return &parser.PosError{
Pos: node.Pos(),
Err: fmt.Errorf("%s: map must have string keys", name),
}
}
// Get the key we're dealing with, which is the first item // Get the key we're dealing with, which is the first item
keyStr := item.Keys[0].Token.Value().(string) keyStr := item.Keys[0].Token.Value().(string)
@ -466,6 +485,14 @@ func (d *decoder) decodeStruct(name string, node ast.Node, result reflect.Value)
node = ot.List node = ot.List
} }
// Handle the special case where the object itself is a literal. Previously
// the yacc parser would always ensure top-level elements were arrays. The new
// parser does not make the same guarantees, thus we need to convert any
// top-level literal elements into a list.
if _, ok := node.(*ast.LiteralType); ok {
node = &ast.ObjectList{Items: []*ast.ObjectItem{item}}
}
list, ok := node.(*ast.ObjectList) list, ok := node.(*ast.ObjectList)
if !ok { if !ok {
return &parser.PosError{ return &parser.PosError{

View file

@ -2,6 +2,7 @@ package hcl
import ( import (
"unicode" "unicode"
"unicode/utf8"
) )
type lexModeValue byte type lexModeValue byte
@ -14,17 +15,23 @@ const (
// lexMode returns whether we're going to be parsing in JSON // lexMode returns whether we're going to be parsing in JSON
// mode or HCL mode. // mode or HCL mode.
func lexMode(v string) lexModeValue { func lexMode(v []byte) lexModeValue {
for _, r := range v { var (
r rune
w int
offset int
)
for {
r, w = utf8.DecodeRune(v[offset:])
offset += w
if unicode.IsSpace(r) { if unicode.IsSpace(r) {
continue continue
} }
if r == '{' { if r == '{' {
return lexModeJson return lexModeJson
} else {
return lexModeHcl
} }
break
} }
return lexModeHcl return lexModeHcl

View file

@ -8,16 +8,32 @@ import (
jsonParser "github.com/hashicorp/hcl/json/parser" jsonParser "github.com/hashicorp/hcl/json/parser"
) )
// Parse parses the given input and returns the root object. // ParseBytes accepts as input byte slice and returns ast tree.
// //
// The input format can be either HCL or JSON. // Input can be either JSON or HCL
func Parse(input string) (*ast.File, error) { func ParseBytes(in []byte) (*ast.File, error) {
switch lexMode(input) { return parse(in)
}
// ParseString accepts input as a string and returns ast tree.
func ParseString(input string) (*ast.File, error) {
return parse([]byte(input))
}
func parse(in []byte) (*ast.File, error) {
switch lexMode(in) {
case lexModeHcl: case lexModeHcl:
return hclParser.Parse([]byte(input)) return hclParser.Parse(in)
case lexModeJson: case lexModeJson:
return jsonParser.Parse([]byte(input)) return jsonParser.Parse(in)
} }
return nil, fmt.Errorf("unknown config format") return nil, fmt.Errorf("unknown config format")
} }
// Parse parses the given input and returns the root object.
//
// The input format can be either HCL or JSON.
func Parse(input string) (*ast.File, error) {
return parse([]byte(input))
}

4
vendor/vendor.json vendored
View file

@ -479,8 +479,10 @@
"revision": "a0d98a5f288019575c6d1f4bb1573fef2d1fcdc4" "revision": "a0d98a5f288019575c6d1f4bb1573fef2d1fcdc4"
}, },
{ {
"checksumSHA1": "5LrCq/ydlbL6pq1cdmuxiw7QV98=",
"path": "github.com/hashicorp/hcl", "path": "github.com/hashicorp/hcl",
"revision": "1c284ec98f4b398443cbabb0d9197f7f4cc0077c" "revision": "d7400db7143f8e869812e50a53acd6c8d92af3b8",
"revisionTime": "2016-06-07T00:19:40Z"
}, },
{ {
"path": "github.com/hashicorp/hcl/hcl/ast", "path": "github.com/hashicorp/hcl/hcl/ast",