forked from benc-uk/hcl2-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
32 lines (28 loc) · 860 Bytes
/
parser.go
File metadata and controls
32 lines (28 loc) · 860 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package main
import (
"github.com/gopherjs/gopherjs/js"
"github.com/miratronix/jopher"
"github.com/tmccombs/hcl2json/convert"
)
func main() {
js.Module.Get("exports").Set("parseToString", jopher.Promisify(parseToString))
js.Module.Get("exports").Set("parseToObject", jopher.Promisify(parseToObject))
}
// Parse a HCL string into a JSON string
func parseToString(input string) (output string, err error) {
convertedBytes, err := convert.Bytes([]byte(input), "", convert.Options{})
if err != nil {
return "", err
}
output = string(convertedBytes)
return output, nil
}
// Parse a HCL string into a JSON object
func parseToObject(input string) (output *js.Object, err error) {
jsonString, err := parseToString(input)
if err != nil {
return &js.Object{}, err
}
obj := js.Global.Get("JSON").Call("parse", string(jsonString))
return obj, nil
}