hcl/json/parse.go

41 lines
784 B
Go
Raw Normal View History

2014-08-02 18:38:41 +00:00
package json
import (
"sync"
2014-08-11 23:23:28 +00:00
"github.com/hashicorp/hcl/hcl"
2014-08-02 18:38:41 +00:00
"github.com/hashicorp/terraform/helper/multierror"
)
// jsonErrors are the errors built up from parsing. These should not
// be accessed directly.
var jsonErrors []error
var jsonLock sync.Mutex
2014-08-11 23:23:28 +00:00
var jsonResult *hcl.Object
2014-08-02 18:38:41 +00:00
// Parse parses the given string and returns the result.
2014-08-11 23:23:28 +00:00
func Parse(v string) (*hcl.Object, error) {
2014-08-02 18:38:41 +00:00
jsonLock.Lock()
defer jsonLock.Unlock()
jsonErrors = nil
jsonResult = nil
// Parse
lex := &jsonLex{Input: v}
jsonParse(lex)
// If we have an error in the lexer itself, return it
if lex.err != nil {
return nil, lex.err
}
// Build up the errors
var err error
if len(jsonErrors) > 0 {
err = &multierror.Error{Errors: jsonErrors}
jsonResult = nil
}
return jsonResult, err
}