2014-08-05 00:04:48 -05:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
|
2016-01-19 13:28:45 -06:00
|
|
|
"github.com/hashicorp/go-multierror"
|
2014-08-05 00:04:48 -05:00
|
|
|
"github.com/hashicorp/hcl"
|
2015-11-07 18:53:42 -06:00
|
|
|
"github.com/hashicorp/hcl/hcl/ast"
|
2015-06-08 00:04:23 -05:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2014-08-05 00:04:48 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// hclConfigurable is an implementation of configurable that knows
|
|
|
|
// how to turn HCL configuration into a *Config object.
|
|
|
|
type hclConfigurable struct {
|
2015-11-07 18:53:42 -06:00
|
|
|
File string
|
|
|
|
Root *ast.File
|
2014-08-05 00:04:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *hclConfigurable) Config() (*Config, error) {
|
|
|
|
validKeys := map[string]struct{}{
|
2016-11-12 18:20:33 -06:00
|
|
|
"atlas": struct{}{},
|
|
|
|
"data": struct{}{},
|
|
|
|
"module": struct{}{},
|
|
|
|
"output": struct{}{},
|
|
|
|
"provider": struct{}{},
|
|
|
|
"resource": struct{}{},
|
|
|
|
"terraform": struct{}{},
|
|
|
|
"variable": struct{}{},
|
2014-08-05 00:04:48 -05:00
|
|
|
}
|
|
|
|
|
2015-11-07 18:53:42 -06:00
|
|
|
// Top-level item should be the object list
|
|
|
|
list, ok := t.Root.Node.(*ast.ObjectList)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("error parsing: file doesn't contain a root object")
|
|
|
|
}
|
|
|
|
|
2016-11-02 16:59:16 -05:00
|
|
|
// Start building up the actual configuration.
|
2014-08-05 00:04:48 -05:00
|
|
|
config := new(Config)
|
2016-01-21 13:33:16 -06:00
|
|
|
|
2016-11-12 18:20:33 -06:00
|
|
|
// Terraform config
|
|
|
|
if o := list.Filter("terraform"); len(o.Items) > 0 {
|
|
|
|
var err error
|
|
|
|
config.Terraform, err = loadTerraformHcl(o)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 16:59:16 -05:00
|
|
|
// Build the variables
|
|
|
|
if vars := list.Filter("variable"); len(vars.Items) > 0 {
|
|
|
|
var err error
|
|
|
|
config.Variables, err = loadVariablesHcl(vars)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-08-05 00:04:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-05 14:56:31 -06:00
|
|
|
// Get Atlas configuration
|
2015-11-07 18:53:42 -06:00
|
|
|
if atlas := list.Filter("atlas"); len(atlas.Items) > 0 {
|
2015-03-05 14:56:31 -06:00
|
|
|
var err error
|
|
|
|
config.Atlas, err = loadAtlasHcl(atlas)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-11 17:58:30 -05:00
|
|
|
// Build the modules
|
2015-11-07 18:53:42 -06:00
|
|
|
if modules := list.Filter("module"); len(modules.Items) > 0 {
|
2014-09-11 17:58:30 -05:00
|
|
|
var err error
|
|
|
|
config.Modules, err = loadModulesHcl(modules)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-05 00:04:48 -05:00
|
|
|
// Build the provider configs
|
2015-11-07 18:53:42 -06:00
|
|
|
if providers := list.Filter("provider"); len(providers.Items) > 0 {
|
2014-08-05 00:04:48 -05:00
|
|
|
var err error
|
|
|
|
config.ProviderConfigs, err = loadProvidersHcl(providers)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the resources
|
2016-01-16 18:20:00 -06:00
|
|
|
{
|
2014-08-05 00:04:48 -05:00
|
|
|
var err error
|
2016-01-16 18:20:00 -06:00
|
|
|
managedResourceConfigs := list.Filter("resource")
|
|
|
|
dataResourceConfigs := list.Filter("data")
|
|
|
|
|
|
|
|
config.Resources = make(
|
|
|
|
[]*Resource, 0,
|
|
|
|
len(managedResourceConfigs.Items)+len(dataResourceConfigs.Items),
|
|
|
|
)
|
|
|
|
|
|
|
|
managedResources, err := loadManagedResourcesHcl(managedResourceConfigs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dataResources, err := loadDataResourcesHcl(dataResourceConfigs)
|
2014-08-05 00:04:48 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-01-16 18:20:00 -06:00
|
|
|
|
|
|
|
config.Resources = append(config.Resources, dataResources...)
|
|
|
|
config.Resources = append(config.Resources, managedResources...)
|
2014-08-05 00:04:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build the outputs
|
2015-11-07 18:53:42 -06:00
|
|
|
if outputs := list.Filter("output"); len(outputs.Items) > 0 {
|
2014-08-05 00:04:48 -05:00
|
|
|
var err error
|
|
|
|
config.Outputs, err = loadOutputsHcl(outputs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for invalid keys
|
2015-11-07 18:53:42 -06:00
|
|
|
for _, item := range list.Items {
|
|
|
|
if len(item.Keys) == 0 {
|
|
|
|
// Not sure how this would happen, but let's avoid a panic
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
k := item.Keys[0].Token.Value().(string)
|
2014-08-05 00:04:48 -05:00
|
|
|
if _, ok := validKeys[k]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
config.unknownKeys = append(config.unknownKeys, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// loadFileHcl is a fileLoaderFunc that knows how to read HCL
|
|
|
|
// files and turn them into hclConfigurables.
|
|
|
|
func loadFileHcl(root string) (configurable, []string, error) {
|
|
|
|
// Read the HCL file and prepare for parsing
|
|
|
|
d, err := ioutil.ReadFile(root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf(
|
|
|
|
"Error reading %s: %s", root, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse it
|
2015-11-07 18:53:42 -06:00
|
|
|
hclRoot, err := hcl.Parse(string(d))
|
2014-08-05 00:04:48 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf(
|
|
|
|
"Error parsing %s: %s", root, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start building the result
|
|
|
|
result := &hclConfigurable{
|
2015-11-07 18:53:42 -06:00
|
|
|
File: root,
|
|
|
|
Root: hclRoot,
|
2014-08-05 00:04:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Dive in, find the imports. This is disabled for now since
|
|
|
|
// imports were removed prior to Terraform 0.1. The code is
|
|
|
|
// remaining here commented for historical purposes.
|
|
|
|
/*
|
|
|
|
imports := obj.Get("import")
|
|
|
|
if imports == nil {
|
|
|
|
result.Object.Ref()
|
|
|
|
return result, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if imports.Type() != libucl.ObjectTypeString {
|
|
|
|
imports.Close()
|
|
|
|
|
|
|
|
return nil, nil, fmt.Errorf(
|
|
|
|
"Error in %s: all 'import' declarations should be in the format\n"+
|
|
|
|
"`import \"foo\"` (Got type %s)",
|
|
|
|
root,
|
|
|
|
imports.Type())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gather all the import paths
|
|
|
|
importPaths := make([]string, 0, imports.Len())
|
|
|
|
iter := imports.Iterate(false)
|
|
|
|
for imp := iter.Next(); imp != nil; imp = iter.Next() {
|
|
|
|
path := imp.ToString()
|
|
|
|
if !filepath.IsAbs(path) {
|
|
|
|
// Relative paths are relative to the Terraform file itself
|
|
|
|
dir := filepath.Dir(root)
|
|
|
|
path = filepath.Join(dir, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
importPaths = append(importPaths, path)
|
|
|
|
imp.Close()
|
|
|
|
}
|
|
|
|
iter.Close()
|
|
|
|
imports.Close()
|
|
|
|
|
|
|
|
result.Object.Ref()
|
|
|
|
*/
|
|
|
|
|
|
|
|
return result, nil, nil
|
|
|
|
}
|
|
|
|
|
2016-11-12 18:20:33 -06:00
|
|
|
// Given a handle to a HCL object, this transforms it into the Terraform config
|
|
|
|
func loadTerraformHcl(list *ast.ObjectList) (*Terraform, error) {
|
|
|
|
if len(list.Items) > 1 {
|
|
|
|
return nil, fmt.Errorf("only one 'terraform' block allowed per module")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get our one item
|
|
|
|
item := list.Items[0]
|
|
|
|
|
|
|
|
// NOTE: We purposely don't validate unknown HCL keys here so that
|
|
|
|
// we can potentially read _future_ Terraform version config (to
|
|
|
|
// still be able to validate the required version).
|
|
|
|
//
|
|
|
|
// We should still keep track of unknown keys to validate later, but
|
|
|
|
// HCL doesn't currently support that.
|
|
|
|
|
|
|
|
var config Terraform
|
|
|
|
if err := hcl.DecodeObject(&config, item.Val); err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Error reading terraform config: %s",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &config, nil
|
|
|
|
}
|
|
|
|
|
2015-03-05 14:56:31 -06:00
|
|
|
// Given a handle to a HCL object, this transforms it into the Atlas
|
|
|
|
// configuration.
|
2015-11-07 18:53:42 -06:00
|
|
|
func loadAtlasHcl(list *ast.ObjectList) (*AtlasConfig, error) {
|
|
|
|
if len(list.Items) > 1 {
|
|
|
|
return nil, fmt.Errorf("only one 'atlas' block allowed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get our one item
|
|
|
|
item := list.Items[0]
|
|
|
|
|
2015-03-05 14:56:31 -06:00
|
|
|
var config AtlasConfig
|
2015-11-07 18:53:42 -06:00
|
|
|
if err := hcl.DecodeObject(&config, item.Val); err != nil {
|
2015-03-05 14:56:31 -06:00
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Error reading atlas config: %s",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &config, nil
|
|
|
|
}
|
|
|
|
|
2014-09-11 17:58:30 -05:00
|
|
|
// Given a handle to a HCL object, this recurses into the structure
|
|
|
|
// and pulls out a list of modules.
|
|
|
|
//
|
|
|
|
// The resulting modules may not be unique, but each module
|
|
|
|
// represents exactly one module definition in the HCL configuration.
|
|
|
|
// We leave it up to another pass to merge them together.
|
2015-11-07 18:53:42 -06:00
|
|
|
func loadModulesHcl(list *ast.ObjectList) ([]*Module, error) {
|
|
|
|
list = list.Children()
|
|
|
|
if len(list.Items) == 0 {
|
|
|
|
return nil, nil
|
2014-09-11 17:58:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Where all the results will go
|
|
|
|
var result []*Module
|
|
|
|
|
|
|
|
// Now go over all the types and their children in order to get
|
|
|
|
// all of the actual resources.
|
2015-11-07 18:53:42 -06:00
|
|
|
for _, item := range list.Items {
|
|
|
|
k := item.Keys[0].Token.Value().(string)
|
|
|
|
|
|
|
|
var listVal *ast.ObjectList
|
|
|
|
if ot, ok := item.Val.(*ast.ObjectType); ok {
|
|
|
|
listVal = ot.List
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("module '%s': should be an object", k)
|
|
|
|
}
|
2014-09-11 17:58:30 -05:00
|
|
|
|
2014-09-14 16:43:54 -05:00
|
|
|
var config map[string]interface{}
|
2015-11-07 18:53:42 -06:00
|
|
|
if err := hcl.DecodeObject(&config, item.Val); err != nil {
|
2014-09-14 16:43:54 -05:00
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Error reading config for %s: %s",
|
|
|
|
k,
|
|
|
|
err)
|
|
|
|
}
|
2014-09-11 17:58:30 -05:00
|
|
|
|
2014-09-14 16:43:54 -05:00
|
|
|
// Remove the fields we handle specially
|
|
|
|
delete(config, "source")
|
2014-09-11 17:58:30 -05:00
|
|
|
|
2014-09-14 16:43:54 -05:00
|
|
|
rawConfig, err := NewRawConfig(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Error reading config for %s: %s",
|
|
|
|
k,
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have a count, then figure it out
|
|
|
|
var source string
|
2015-11-07 18:53:42 -06:00
|
|
|
if o := listVal.Filter("source"); len(o.Items) > 0 {
|
|
|
|
err = hcl.DecodeObject(&source, o.Items[0].Val)
|
2014-09-11 17:58:30 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
2014-09-14 16:43:54 -05:00
|
|
|
"Error parsing source for %s: %s",
|
2014-09-11 17:58:30 -05:00
|
|
|
k,
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
}
|
2014-09-14 16:43:54 -05:00
|
|
|
|
|
|
|
result = append(result, &Module{
|
|
|
|
Name: k,
|
|
|
|
Source: source,
|
|
|
|
RawConfig: rawConfig,
|
|
|
|
})
|
2014-09-11 17:58:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2014-08-05 00:04:48 -05:00
|
|
|
// LoadOutputsHcl recurses into the given HCL object and turns
|
|
|
|
// it into a mapping of outputs.
|
2015-11-07 18:53:42 -06:00
|
|
|
func loadOutputsHcl(list *ast.ObjectList) ([]*Output, error) {
|
|
|
|
list = list.Children()
|
|
|
|
if len(list.Items) == 0 {
|
2016-11-02 18:56:20 -05:00
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"'output' must be followed by exactly one string: a name")
|
2014-08-05 00:04:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Go through each object and turn it into an actual result.
|
2015-11-07 18:53:42 -06:00
|
|
|
result := make([]*Output, 0, len(list.Items))
|
|
|
|
for _, item := range list.Items {
|
|
|
|
n := item.Keys[0].Token.Value().(string)
|
2014-08-05 00:04:48 -05:00
|
|
|
|
2016-11-11 19:46:34 -06:00
|
|
|
var listVal *ast.ObjectList
|
|
|
|
if ot, ok := item.Val.(*ast.ObjectType); ok {
|
|
|
|
listVal = ot.List
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("output '%s': should be an object", n)
|
|
|
|
}
|
|
|
|
|
2015-11-07 18:53:42 -06:00
|
|
|
var config map[string]interface{}
|
|
|
|
if err := hcl.DecodeObject(&config, item.Val); err != nil {
|
2014-08-05 00:04:48 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-11-11 19:46:34 -06:00
|
|
|
// Delete special keys
|
|
|
|
delete(config, "depends_on")
|
|
|
|
|
2014-08-05 00:04:48 -05:00
|
|
|
rawConfig, err := NewRawConfig(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Error reading config for output %s: %s",
|
|
|
|
n,
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
2016-11-11 19:46:34 -06:00
|
|
|
// If we have depends fields, then add those in
|
|
|
|
var dependsOn []string
|
|
|
|
if o := listVal.Filter("depends_on"); len(o.Items) > 0 {
|
|
|
|
err := hcl.DecodeObject(&dependsOn, o.Items[0].Val)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Error reading depends_on for output %q: %s",
|
|
|
|
n,
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-05 00:04:48 -05:00
|
|
|
result = append(result, &Output{
|
|
|
|
Name: n,
|
|
|
|
RawConfig: rawConfig,
|
2016-11-11 19:46:34 -06:00
|
|
|
DependsOn: dependsOn,
|
2014-08-05 00:04:48 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2016-11-02 16:59:16 -05:00
|
|
|
// LoadVariablesHcl recurses into the given HCL object and turns
|
|
|
|
// it into a list of variables.
|
|
|
|
func loadVariablesHcl(list *ast.ObjectList) ([]*Variable, error) {
|
|
|
|
list = list.Children()
|
|
|
|
if len(list.Items) == 0 {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"'variable' must be followed by exactly one strings: a name")
|
|
|
|
}
|
|
|
|
|
|
|
|
// hclVariable is the structure each variable is decoded into
|
|
|
|
type hclVariable struct {
|
|
|
|
DeclaredType string `hcl:"type"`
|
|
|
|
Default interface{}
|
|
|
|
Description string
|
|
|
|
Fields []string `hcl:",decodedFields"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go through each object and turn it into an actual result.
|
|
|
|
result := make([]*Variable, 0, len(list.Items))
|
|
|
|
for _, item := range list.Items {
|
|
|
|
// Clean up items from JSON
|
|
|
|
unwrapHCLObjectKeysFromJSON(item, 1)
|
|
|
|
|
|
|
|
// Verify the keys
|
|
|
|
if len(item.Keys) != 1 {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"position %s: 'variable' must be followed by exactly one strings: a name",
|
|
|
|
item.Pos())
|
|
|
|
}
|
|
|
|
|
|
|
|
n := item.Keys[0].Token.Value().(string)
|
|
|
|
|
|
|
|
/*
|
|
|
|
// TODO: catch extra fields
|
|
|
|
// Decode into raw map[string]interface{} so we know ALL fields
|
|
|
|
var config map[string]interface{}
|
|
|
|
if err := hcl.DecodeObject(&config, item.Val); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Decode into hclVariable to get typed values
|
|
|
|
var hclVar hclVariable
|
|
|
|
if err := hcl.DecodeObject(&hclVar, item.Val); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Defaults turn into a slice of map[string]interface{} and
|
|
|
|
// we need to make sure to convert that down into the
|
|
|
|
// proper type for Config.
|
|
|
|
if ms, ok := hclVar.Default.([]map[string]interface{}); ok {
|
|
|
|
def := make(map[string]interface{})
|
|
|
|
for _, m := range ms {
|
|
|
|
for k, v := range m {
|
|
|
|
def[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hclVar.Default = def
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the new variable and do some basic validation
|
|
|
|
newVar := &Variable{
|
|
|
|
Name: n,
|
|
|
|
DeclaredType: hclVar.DeclaredType,
|
|
|
|
Default: hclVar.Default,
|
|
|
|
Description: hclVar.Description,
|
|
|
|
}
|
|
|
|
if err := newVar.ValidateTypeAndDefault(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result = append(result, newVar)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2014-08-05 00:04:48 -05:00
|
|
|
// LoadProvidersHcl recurses into the given HCL object and turns
|
|
|
|
// it into a mapping of provider configs.
|
2015-11-07 18:53:42 -06:00
|
|
|
func loadProvidersHcl(list *ast.ObjectList) ([]*ProviderConfig, error) {
|
|
|
|
list = list.Children()
|
|
|
|
if len(list.Items) == 0 {
|
2014-08-05 00:04:48 -05:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go through each object and turn it into an actual result.
|
2015-11-07 18:53:42 -06:00
|
|
|
result := make([]*ProviderConfig, 0, len(list.Items))
|
|
|
|
for _, item := range list.Items {
|
|
|
|
n := item.Keys[0].Token.Value().(string)
|
|
|
|
|
|
|
|
var listVal *ast.ObjectList
|
|
|
|
if ot, ok := item.Val.(*ast.ObjectType); ok {
|
|
|
|
listVal = ot.List
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("module '%s': should be an object", n)
|
|
|
|
}
|
2014-08-05 00:04:48 -05:00
|
|
|
|
2015-11-07 18:53:42 -06:00
|
|
|
var config map[string]interface{}
|
|
|
|
if err := hcl.DecodeObject(&config, item.Val); err != nil {
|
2014-08-05 00:04:48 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-03-23 17:36:53 -05:00
|
|
|
delete(config, "alias")
|
|
|
|
|
2014-08-05 00:04:48 -05:00
|
|
|
rawConfig, err := NewRawConfig(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Error reading config for provider config %s: %s",
|
2015-11-07 18:53:42 -06:00
|
|
|
n,
|
2014-08-05 00:04:48 -05:00
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
2015-03-23 17:36:53 -05:00
|
|
|
// If we have an alias field, then add those in
|
|
|
|
var alias string
|
2015-11-07 18:53:42 -06:00
|
|
|
if a := listVal.Filter("alias"); len(a.Items) > 0 {
|
|
|
|
err := hcl.DecodeObject(&alias, a.Items[0].Val)
|
2015-03-23 17:36:53 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Error reading alias for provider[%s]: %s",
|
2015-11-07 18:53:42 -06:00
|
|
|
n,
|
2015-03-23 17:36:53 -05:00
|
|
|
err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-05 00:04:48 -05:00
|
|
|
result = append(result, &ProviderConfig{
|
2015-11-07 18:53:42 -06:00
|
|
|
Name: n,
|
2015-03-23 17:36:53 -05:00
|
|
|
Alias: alias,
|
2014-08-05 00:04:48 -05:00
|
|
|
RawConfig: rawConfig,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Given a handle to a HCL object, this recurses into the structure
|
2016-01-16 18:20:00 -06:00
|
|
|
// and pulls out a list of data sources.
|
|
|
|
//
|
|
|
|
// The resulting data sources may not be unique, but each one
|
|
|
|
// represents exactly one data definition in the HCL configuration.
|
|
|
|
// We leave it up to another pass to merge them together.
|
|
|
|
func loadDataResourcesHcl(list *ast.ObjectList) ([]*Resource, error) {
|
|
|
|
list = list.Children()
|
|
|
|
if len(list.Items) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Where all the results will go
|
|
|
|
var result []*Resource
|
|
|
|
|
|
|
|
// Now go over all the types and their children in order to get
|
|
|
|
// all of the actual resources.
|
|
|
|
for _, item := range list.Items {
|
|
|
|
if len(item.Keys) != 2 {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"position %s: 'data' must be followed by exactly two strings: a type and a name",
|
|
|
|
item.Pos())
|
|
|
|
}
|
|
|
|
|
|
|
|
t := item.Keys[0].Token.Value().(string)
|
|
|
|
k := item.Keys[1].Token.Value().(string)
|
|
|
|
|
|
|
|
var listVal *ast.ObjectList
|
|
|
|
if ot, ok := item.Val.(*ast.ObjectType); ok {
|
|
|
|
listVal = ot.List
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("data sources %s[%s]: should be an object", t, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
var config map[string]interface{}
|
|
|
|
if err := hcl.DecodeObject(&config, item.Val); err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Error reading config for %s[%s]: %s",
|
|
|
|
t,
|
|
|
|
k,
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the fields we handle specially
|
|
|
|
delete(config, "depends_on")
|
|
|
|
delete(config, "provider")
|
2016-09-02 13:43:14 -05:00
|
|
|
delete(config, "count")
|
2016-01-16 18:20:00 -06:00
|
|
|
|
|
|
|
rawConfig, err := NewRawConfig(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Error reading config for %s[%s]: %s",
|
|
|
|
t,
|
|
|
|
k,
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have a count, then figure it out
|
|
|
|
var count string = "1"
|
|
|
|
if o := listVal.Filter("count"); len(o.Items) > 0 {
|
|
|
|
err = hcl.DecodeObject(&count, o.Items[0].Val)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Error parsing count for %s[%s]: %s",
|
|
|
|
t,
|
|
|
|
k,
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
countConfig, err := NewRawConfig(map[string]interface{}{
|
|
|
|
"count": count,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
countConfig.Key = "count"
|
|
|
|
|
|
|
|
// If we have depends fields, then add those in
|
|
|
|
var dependsOn []string
|
|
|
|
if o := listVal.Filter("depends_on"); len(o.Items) > 0 {
|
|
|
|
err := hcl.DecodeObject(&dependsOn, o.Items[0].Val)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Error reading depends_on for %s[%s]: %s",
|
|
|
|
t,
|
|
|
|
k,
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have a provider, then parse it out
|
|
|
|
var provider string
|
|
|
|
if o := listVal.Filter("provider"); len(o.Items) > 0 {
|
|
|
|
err := hcl.DecodeObject(&provider, o.Items[0].Val)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Error reading provider for %s[%s]: %s",
|
|
|
|
t,
|
|
|
|
k,
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result = append(result, &Resource{
|
|
|
|
Mode: DataResourceMode,
|
|
|
|
Name: k,
|
|
|
|
Type: t,
|
|
|
|
RawCount: countConfig,
|
|
|
|
RawConfig: rawConfig,
|
|
|
|
Provider: provider,
|
|
|
|
Provisioners: []*Provisioner{},
|
|
|
|
DependsOn: dependsOn,
|
|
|
|
Lifecycle: ResourceLifecycle{},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Given a handle to a HCL object, this recurses into the structure
|
|
|
|
// and pulls out a list of managed resources.
|
2014-08-05 00:04:48 -05:00
|
|
|
//
|
|
|
|
// The resulting resources may not be unique, but each resource
|
2016-01-16 18:20:00 -06:00
|
|
|
// represents exactly one "resource" block in the HCL configuration.
|
2014-08-05 00:04:48 -05:00
|
|
|
// We leave it up to another pass to merge them together.
|
2016-01-16 18:20:00 -06:00
|
|
|
func loadManagedResourcesHcl(list *ast.ObjectList) ([]*Resource, error) {
|
2015-11-07 18:53:42 -06:00
|
|
|
list = list.Children()
|
|
|
|
if len(list.Items) == 0 {
|
|
|
|
return nil, nil
|
2014-08-11 23:51:24 -05:00
|
|
|
}
|
2014-08-05 00:04:48 -05:00
|
|
|
|
|
|
|
// Where all the results will go
|
|
|
|
var result []*Resource
|
|
|
|
|
|
|
|
// Now go over all the types and their children in order to get
|
|
|
|
// all of the actual resources.
|
2015-11-07 18:53:42 -06:00
|
|
|
for _, item := range list.Items {
|
2016-01-19 14:19:32 -06:00
|
|
|
// GH-4385: We detect a pure provisioner resource and give the user
|
|
|
|
// an error about how to do it cleanly.
|
|
|
|
if len(item.Keys) == 4 && item.Keys[2].Token.Value().(string) == "provisioner" {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"position %s: provisioners in a resource should be wrapped in a list\n\n"+
|
|
|
|
"Example: \"provisioner\": [ { \"local-exec\": ... } ]",
|
|
|
|
item.Pos())
|
|
|
|
}
|
|
|
|
|
2016-11-02 16:59:16 -05:00
|
|
|
// Fix up JSON input
|
|
|
|
unwrapHCLObjectKeysFromJSON(item, 2)
|
2016-08-25 19:18:18 -05:00
|
|
|
|
2015-11-07 18:53:42 -06:00
|
|
|
if len(item.Keys) != 2 {
|
2015-12-09 18:05:49 -06:00
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"position %s: resource must be followed by exactly two strings, a type and a name",
|
|
|
|
item.Pos())
|
2015-11-07 18:53:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
t := item.Keys[0].Token.Value().(string)
|
|
|
|
k := item.Keys[1].Token.Value().(string)
|
|
|
|
|
|
|
|
var listVal *ast.ObjectList
|
|
|
|
if ot, ok := item.Val.(*ast.ObjectType); ok {
|
|
|
|
listVal = ot.List
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("resources %s[%s]: should be an object", t, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
var config map[string]interface{}
|
|
|
|
if err := hcl.DecodeObject(&config, item.Val); err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Error reading config for %s[%s]: %s",
|
|
|
|
t,
|
|
|
|
k,
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the fields we handle specially
|
|
|
|
delete(config, "connection")
|
|
|
|
delete(config, "count")
|
|
|
|
delete(config, "depends_on")
|
|
|
|
delete(config, "provisioner")
|
|
|
|
delete(config, "provider")
|
|
|
|
delete(config, "lifecycle")
|
|
|
|
|
|
|
|
rawConfig, err := NewRawConfig(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Error reading config for %s[%s]: %s",
|
|
|
|
t,
|
|
|
|
k,
|
|
|
|
err)
|
|
|
|
}
|
2014-08-11 23:51:24 -05:00
|
|
|
|
2015-11-07 18:53:42 -06:00
|
|
|
// If we have a count, then figure it out
|
|
|
|
var count string = "1"
|
|
|
|
if o := listVal.Filter("count"); len(o.Items) > 0 {
|
|
|
|
err = hcl.DecodeObject(&count, o.Items[0].Val)
|
|
|
|
if err != nil {
|
2014-08-11 23:51:24 -05:00
|
|
|
return nil, fmt.Errorf(
|
2015-11-07 18:53:42 -06:00
|
|
|
"Error parsing count for %s[%s]: %s",
|
|
|
|
t,
|
2014-08-11 23:51:24 -05:00
|
|
|
k,
|
|
|
|
err)
|
|
|
|
}
|
2015-11-07 18:53:42 -06:00
|
|
|
}
|
|
|
|
countConfig, err := NewRawConfig(map[string]interface{}{
|
|
|
|
"count": count,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
countConfig.Key = "count"
|
2014-08-11 23:51:24 -05:00
|
|
|
|
2015-11-07 18:53:42 -06:00
|
|
|
// If we have depends fields, then add those in
|
|
|
|
var dependsOn []string
|
|
|
|
if o := listVal.Filter("depends_on"); len(o.Items) > 0 {
|
|
|
|
err := hcl.DecodeObject(&dependsOn, o.Items[0].Val)
|
2014-08-11 23:51:24 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
2015-11-07 18:53:42 -06:00
|
|
|
"Error reading depends_on for %s[%s]: %s",
|
|
|
|
t,
|
2014-08-11 23:51:24 -05:00
|
|
|
k,
|
|
|
|
err)
|
|
|
|
}
|
2015-11-07 18:53:42 -06:00
|
|
|
}
|
2014-08-05 00:04:48 -05:00
|
|
|
|
2015-11-07 18:53:42 -06:00
|
|
|
// If we have connection info, then parse those out
|
|
|
|
var connInfo map[string]interface{}
|
|
|
|
if o := listVal.Filter("connection"); len(o.Items) > 0 {
|
|
|
|
err := hcl.DecodeObject(&connInfo, o.Items[0].Val)
|
2014-10-02 13:14:50 -05:00
|
|
|
if err != nil {
|
2015-11-07 18:53:42 -06:00
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Error reading connection info for %s[%s]: %s",
|
|
|
|
t,
|
|
|
|
k,
|
|
|
|
err)
|
2014-08-11 23:51:24 -05:00
|
|
|
}
|
2015-11-07 18:53:42 -06:00
|
|
|
}
|
2014-08-05 00:04:48 -05:00
|
|
|
|
2015-11-07 18:53:42 -06:00
|
|
|
// If we have provisioners, then parse those out
|
|
|
|
var provisioners []*Provisioner
|
|
|
|
if os := listVal.Filter("provisioner"); len(os.Items) > 0 {
|
|
|
|
var err error
|
|
|
|
provisioners, err = loadProvisionersHcl(os, connInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Error reading provisioners for %s[%s]: %s",
|
|
|
|
t,
|
|
|
|
k,
|
|
|
|
err)
|
2014-08-11 23:51:24 -05:00
|
|
|
}
|
2015-11-07 18:53:42 -06:00
|
|
|
}
|
2014-08-05 00:04:48 -05:00
|
|
|
|
2015-11-07 18:53:42 -06:00
|
|
|
// If we have a provider, then parse it out
|
|
|
|
var provider string
|
|
|
|
if o := listVal.Filter("provider"); len(o.Items) > 0 {
|
|
|
|
err := hcl.DecodeObject(&provider, o.Items[0].Val)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Error reading provider for %s[%s]: %s",
|
|
|
|
t,
|
|
|
|
k,
|
|
|
|
err)
|
2014-08-05 00:04:48 -05:00
|
|
|
}
|
2015-11-07 18:53:42 -06:00
|
|
|
}
|
2014-08-11 23:51:24 -05:00
|
|
|
|
2015-11-07 18:53:42 -06:00
|
|
|
// Check if the resource should be re-created before
|
|
|
|
// destroying the existing instance
|
|
|
|
var lifecycle ResourceLifecycle
|
|
|
|
if o := listVal.Filter("lifecycle"); len(o.Items) > 0 {
|
2016-01-19 13:28:45 -06:00
|
|
|
// Check for invalid keys
|
|
|
|
valid := []string{"create_before_destroy", "ignore_changes", "prevent_destroy"}
|
|
|
|
if err := checkHCLKeys(o.Items[0].Val, valid); err != nil {
|
|
|
|
return nil, multierror.Prefix(err, fmt.Sprintf(
|
|
|
|
"%s[%s]:", t, k))
|
|
|
|
}
|
|
|
|
|
2015-11-07 18:53:42 -06:00
|
|
|
var raw map[string]interface{}
|
|
|
|
if err = hcl.DecodeObject(&raw, o.Items[0].Val); err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Error parsing lifecycle for %s[%s]: %s",
|
|
|
|
t,
|
|
|
|
k,
|
|
|
|
err)
|
2015-03-23 17:36:53 -05:00
|
|
|
}
|
|
|
|
|
2015-11-07 18:53:42 -06:00
|
|
|
if err := mapstructure.WeakDecode(raw, &lifecycle); err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Error parsing lifecycle for %s[%s]: %s",
|
|
|
|
t,
|
|
|
|
k,
|
|
|
|
err)
|
2014-09-22 15:31:20 -05:00
|
|
|
}
|
2014-08-05 00:04:48 -05:00
|
|
|
}
|
2015-11-07 18:53:42 -06:00
|
|
|
|
|
|
|
result = append(result, &Resource{
|
2016-05-01 16:43:05 -05:00
|
|
|
Mode: ManagedResourceMode,
|
2015-11-07 18:53:42 -06:00
|
|
|
Name: k,
|
|
|
|
Type: t,
|
|
|
|
RawCount: countConfig,
|
|
|
|
RawConfig: rawConfig,
|
|
|
|
Provisioners: provisioners,
|
|
|
|
Provider: provider,
|
|
|
|
DependsOn: dependsOn,
|
|
|
|
Lifecycle: lifecycle,
|
|
|
|
})
|
2014-08-05 00:04:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2015-11-07 18:53:42 -06:00
|
|
|
func loadProvisionersHcl(list *ast.ObjectList, connInfo map[string]interface{}) ([]*Provisioner, error) {
|
|
|
|
list = list.Children()
|
|
|
|
if len(list.Items) == 0 {
|
2014-08-05 00:04:48 -05:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-11-07 18:53:42 -06:00
|
|
|
// Go through each object and turn it into an actual result.
|
|
|
|
result := make([]*Provisioner, 0, len(list.Items))
|
|
|
|
for _, item := range list.Items {
|
|
|
|
n := item.Keys[0].Token.Value().(string)
|
|
|
|
|
|
|
|
var listVal *ast.ObjectList
|
|
|
|
if ot, ok := item.Val.(*ast.ObjectType); ok {
|
|
|
|
listVal = ot.List
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("provisioner '%s': should be an object", n)
|
|
|
|
}
|
|
|
|
|
2014-08-05 00:04:48 -05:00
|
|
|
var config map[string]interface{}
|
2015-11-07 18:53:42 -06:00
|
|
|
if err := hcl.DecodeObject(&config, item.Val); err != nil {
|
2014-08-05 00:04:48 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-09-11 13:56:20 -05:00
|
|
|
// Delete the "connection" section, handle separately
|
2014-08-05 00:04:48 -05:00
|
|
|
delete(config, "connection")
|
|
|
|
|
|
|
|
rawConfig, err := NewRawConfig(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we have a provisioner-level connection
|
|
|
|
// block that overrides the resource-level
|
|
|
|
var subConnInfo map[string]interface{}
|
2015-11-07 18:53:42 -06:00
|
|
|
if o := listVal.Filter("connection"); len(o.Items) > 0 {
|
|
|
|
err := hcl.DecodeObject(&subConnInfo, o.Items[0].Val)
|
2014-08-11 23:51:24 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-08-05 00:04:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inherit from the resource connInfo any keys
|
|
|
|
// that are not explicitly overriden.
|
|
|
|
if connInfo != nil && subConnInfo != nil {
|
|
|
|
for k, v := range connInfo {
|
|
|
|
if _, ok := subConnInfo[k]; !ok {
|
|
|
|
subConnInfo[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if subConnInfo == nil {
|
|
|
|
subConnInfo = connInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the connInfo
|
|
|
|
connRaw, err := NewRawConfig(subConnInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result = append(result, &Provisioner{
|
2015-11-07 18:53:42 -06:00
|
|
|
Type: n,
|
2014-08-05 00:04:48 -05:00
|
|
|
RawConfig: rawConfig,
|
|
|
|
ConnInfo: connRaw,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2014-08-11 23:51:24 -05:00
|
|
|
/*
|
|
|
|
func hclObjectMap(os *hclobj.Object) map[string]ast.ListNode {
|
|
|
|
objects := make(map[string][]*hclobj.Object)
|
2014-08-05 00:04:48 -05:00
|
|
|
|
2014-08-11 23:51:24 -05:00
|
|
|
for _, o := range os.Elem(false) {
|
|
|
|
for _, elem := range o.Elem(true) {
|
|
|
|
val, ok := objects[elem.Key]
|
2014-08-05 00:04:48 -05:00
|
|
|
if !ok {
|
2014-08-11 23:51:24 -05:00
|
|
|
val = make([]*hclobj.Object, 0, 1)
|
2014-08-05 00:04:48 -05:00
|
|
|
}
|
|
|
|
|
2014-08-11 23:51:24 -05:00
|
|
|
val = append(val, elem)
|
|
|
|
objects[elem.Key] = val
|
2014-08-05 00:04:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return objects
|
|
|
|
}
|
2014-08-11 23:51:24 -05:00
|
|
|
*/
|
2016-01-19 13:28:45 -06:00
|
|
|
|
|
|
|
func checkHCLKeys(node ast.Node, valid []string) error {
|
|
|
|
var list *ast.ObjectList
|
|
|
|
switch n := node.(type) {
|
|
|
|
case *ast.ObjectList:
|
|
|
|
list = n
|
|
|
|
case *ast.ObjectType:
|
|
|
|
list = n.List
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("cannot check HCL keys of type %T", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
validMap := make(map[string]struct{}, len(valid))
|
|
|
|
for _, v := range valid {
|
|
|
|
validMap[v] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
var result error
|
|
|
|
for _, item := range list.Items {
|
|
|
|
key := item.Keys[0].Token.Value().(string)
|
|
|
|
if _, ok := validMap[key]; !ok {
|
|
|
|
result = multierror.Append(result, fmt.Errorf(
|
|
|
|
"invalid key: %s", key))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
2016-11-02 16:59:16 -05:00
|
|
|
|
|
|
|
// unwrapHCLObjectKeysFromJSON cleans up an edge case that can occur when
|
|
|
|
// parsing JSON as input: if we're parsing JSON then directly nested
|
|
|
|
// items will show up as additional "keys".
|
|
|
|
//
|
|
|
|
// For objects that expect a fixed number of keys, this breaks the
|
|
|
|
// decoding process. This function unwraps the object into what it would've
|
|
|
|
// looked like if it came directly from HCL by specifying the number of keys
|
|
|
|
// you expect.
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
|
|
|
// { "foo": { "baz": {} } }
|
|
|
|
//
|
|
|
|
// Will show up with Keys being: []string{"foo", "baz"}
|
|
|
|
// when we really just want the first two. This function will fix this.
|
|
|
|
func unwrapHCLObjectKeysFromJSON(item *ast.ObjectItem, depth int) {
|
|
|
|
if len(item.Keys) > depth && item.Keys[0].Token.JSON {
|
|
|
|
for len(item.Keys) > depth {
|
|
|
|
// Pop off the last key
|
|
|
|
n := len(item.Keys)
|
|
|
|
key := item.Keys[n-1]
|
|
|
|
item.Keys[n-1] = nil
|
|
|
|
item.Keys = item.Keys[:n-1]
|
|
|
|
|
|
|
|
// Wrap our value in a list
|
|
|
|
item.Val = &ast.ObjectType{
|
|
|
|
List: &ast.ObjectList{
|
|
|
|
Items: []*ast.ObjectItem{
|
|
|
|
&ast.ObjectItem{
|
|
|
|
Keys: []*ast.ObjectKey{key},
|
|
|
|
Val: item.Val,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|