2017-07-28 01:15:56 +00:00
|
|
|
package include
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2017-09-11 23:40:37 +00:00
|
|
|
"github.com/hashicorp/hcl2/hcl"
|
2017-07-28 01:15:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// MapResolver returns a Resolver that consults the given map for preloaded
|
|
|
|
// bodies (the values) associated with static include paths (the keys).
|
|
|
|
//
|
|
|
|
// An error diagnostic is returned if a path is requested that does not appear
|
|
|
|
// as a key in the given map.
|
2017-09-11 23:40:37 +00:00
|
|
|
func MapResolver(m map[string]hcl.Body) Resolver {
|
|
|
|
return ResolverFunc(func(path string, refRange hcl.Range) (hcl.Body, hcl.Diagnostics) {
|
2017-07-28 01:15:56 +00:00
|
|
|
if body, ok := m[path]; ok {
|
|
|
|
return body, nil
|
|
|
|
}
|
|
|
|
|
2017-09-11 23:40:37 +00:00
|
|
|
return nil, hcl.Diagnostics{
|
2017-07-28 01:15:56 +00:00
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Severity: hcl.DiagError,
|
2017-07-28 01:15:56 +00:00
|
|
|
Summary: "Invalid include path",
|
|
|
|
Detail: fmt.Sprintf("The include path %q is not recognized.", path),
|
|
|
|
Subject: &refRange,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|