github.com/beauknowssoftware/makehcl@v0.0.0-20200322000747-1b9bb1e1c008/internal/parse2/string.go (about) 1 package parse2 2 3 import ( 4 "fmt" 5 6 "github.com/hashicorp/hcl/v2" 7 "github.com/zclconf/go-cty/cty" 8 ) 9 10 type String struct { 11 Value string 12 attribute *hcl.Attribute 13 val cty.Value 14 ctx *hcl.EvalContext 15 } 16 17 func newStringAttribute(attr *hcl.Attribute, ctx *hcl.EvalContext) (sa *String, diag hcl.Diagnostics) { 18 sa = &String{ 19 attribute: attr, 20 } 21 _, diag = sa.fill(ctx) 22 23 return 24 } 25 26 func (a *String) fill(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) { 27 val, diag := a.attribute.Expr.Value(ctx) 28 if diag.HasErrors() { 29 return a.val, diag 30 } 31 32 t := val.Type() 33 if t != cty.String { 34 diag := hcl.Diagnostic{ 35 Summary: "invalid type", 36 Detail: fmt.Sprintf("expected string, got %v", t.FriendlyName()), 37 Severity: hcl.DiagError, 38 Subject: &a.attribute.Range, 39 Expression: a.attribute.Expr, 40 EvalContext: ctx, 41 } 42 43 return a.val, hcl.Diagnostics{&diag} 44 } 45 46 a.Value = val.AsString() 47 a.val = val 48 a.ctx = ctx 49 50 return a.val, nil 51 }