mirror of
				https://gitea.com/Lydanne/buildx.git
				synced 2025-11-04 10:03:42 +08:00 
			
		
		
		
	Update the buildflags cty code to handle unknown values. When hcl decodes a value with an invalid variable name, it appends a diagnostic for the error and then returns an unknown value so it can continue processing the file and finding more errors. The iteration code has now been changed to use a rangefunc from go 1.23 and it skips empty or unknown values. Empty values are valid when they are skipped and unknown values will have a diagnostic for itself. Signed-off-by: Jonathan A. Sternberg <jonathan.sternberg@docker.com>
		
			
				
	
	
		
			100 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			100 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package buildflags
 | 
						|
 | 
						|
import (
 | 
						|
	"strconv"
 | 
						|
	"sync"
 | 
						|
 | 
						|
	"github.com/zclconf/go-cty/cty"
 | 
						|
	"github.com/zclconf/go-cty/cty/convert"
 | 
						|
)
 | 
						|
 | 
						|
var attestType = sync.OnceValue(func() cty.Type {
 | 
						|
	return cty.Map(cty.String)
 | 
						|
})
 | 
						|
 | 
						|
func (e *Attests) FromCtyValue(in cty.Value, p cty.Path) error {
 | 
						|
	got := in.Type()
 | 
						|
	if got.IsTupleType() || got.IsListType() {
 | 
						|
		return e.fromCtyValue(in, p)
 | 
						|
	}
 | 
						|
 | 
						|
	want := cty.List(attestType())
 | 
						|
	return p.NewErrorf("%s", convert.MismatchMessage(got, want))
 | 
						|
}
 | 
						|
 | 
						|
func (e *Attests) fromCtyValue(in cty.Value, p cty.Path) error {
 | 
						|
	*e = make([]*Attest, 0, in.LengthInt())
 | 
						|
	for value := range eachElement(in) {
 | 
						|
		entry := &Attest{}
 | 
						|
		if err := entry.FromCtyValue(value, p); err != nil {
 | 
						|
			return err
 | 
						|
		}
 | 
						|
		*e = append(*e, entry)
 | 
						|
	}
 | 
						|
	return nil
 | 
						|
}
 | 
						|
 | 
						|
func (e Attests) ToCtyValue() cty.Value {
 | 
						|
	if len(e) == 0 {
 | 
						|
		return cty.ListValEmpty(attestType())
 | 
						|
	}
 | 
						|
 | 
						|
	vals := make([]cty.Value, len(e))
 | 
						|
	for i, entry := range e {
 | 
						|
		vals[i] = entry.ToCtyValue()
 | 
						|
	}
 | 
						|
	return cty.ListVal(vals)
 | 
						|
}
 | 
						|
 | 
						|
func (e *Attest) FromCtyValue(in cty.Value, p cty.Path) error {
 | 
						|
	if in.Type() == cty.String {
 | 
						|
		if err := e.UnmarshalText([]byte(in.AsString())); err != nil {
 | 
						|
			return p.NewError(err)
 | 
						|
		}
 | 
						|
		return nil
 | 
						|
	}
 | 
						|
 | 
						|
	conv, err := convert.Convert(in, cty.Map(cty.String))
 | 
						|
	if err != nil {
 | 
						|
		return err
 | 
						|
	}
 | 
						|
 | 
						|
	e.Attrs = map[string]string{}
 | 
						|
	for it := conv.ElementIterator(); it.Next(); {
 | 
						|
		k, v := it.Element()
 | 
						|
		if !v.IsKnown() {
 | 
						|
			continue
 | 
						|
		}
 | 
						|
 | 
						|
		switch key := k.AsString(); key {
 | 
						|
		case "type":
 | 
						|
			e.Type = v.AsString()
 | 
						|
		case "disabled":
 | 
						|
			b, err := strconv.ParseBool(v.AsString())
 | 
						|
			if err != nil {
 | 
						|
				return err
 | 
						|
			}
 | 
						|
			e.Disabled = b
 | 
						|
		default:
 | 
						|
			e.Attrs[key] = v.AsString()
 | 
						|
		}
 | 
						|
	}
 | 
						|
	return nil
 | 
						|
}
 | 
						|
 | 
						|
func (e *Attest) ToCtyValue() cty.Value {
 | 
						|
	if e == nil {
 | 
						|
		return cty.NullVal(cty.Map(cty.String))
 | 
						|
	}
 | 
						|
 | 
						|
	vals := make(map[string]cty.Value, len(e.Attrs)+2)
 | 
						|
	for k, v := range e.Attrs {
 | 
						|
		vals[k] = cty.StringVal(v)
 | 
						|
	}
 | 
						|
	vals["type"] = cty.StringVal(e.Type)
 | 
						|
	if e.Disabled {
 | 
						|
		vals["disabled"] = cty.StringVal("true")
 | 
						|
	}
 | 
						|
	return cty.MapVal(vals)
 | 
						|
}
 |