Platform mapping-based product config

This allows us to set product variables as build settings instead
of loading them from a target's provider, which further allows us
to read product config variables in transitions.

Bug: 287539062
Bug: 269577299
Test: Presubmits
Change-Id: I8497703f706162572ceb3486240e1eb02a37f5f6
This commit is contained in:
Cole Faust
2023-04-21 17:37:11 -07:00
parent adb892c539
commit f8231dd0ea
12 changed files with 134 additions and 40 deletions

View File

@@ -25,12 +25,14 @@ import (
)
func Unmarshal[T any](value starlark.Value) (T, error) {
var zero T
x, err := UnmarshalReflect(value, reflect.TypeOf(zero))
x, err := UnmarshalReflect(value, reflect.TypeOf((*T)(nil)).Elem())
return x.Interface().(T), err
}
func UnmarshalReflect(value starlark.Value, ty reflect.Type) (reflect.Value, error) {
if ty == reflect.TypeOf((*starlark.Value)(nil)).Elem() {
return reflect.ValueOf(value), nil
}
zero := reflect.Zero(ty)
var result reflect.Value
if ty.Kind() == reflect.Interface {
@@ -286,3 +288,17 @@ func typeOfStarlarkValue(value starlark.Value) (reflect.Type, error) {
return nil, fmt.Errorf("unimplemented starlark type: %s", value.Type())
}
}
// NoneableString converts a starlark.Value to a string pointer. If the starlark.Value is NoneType,
// a nil pointer will be returned instead. All other types of starlark values are errors.
func NoneableString(value starlark.Value) (*string, error) {
switch v := value.(type) {
case starlark.String:
result := v.GoString()
return &result, nil
case starlark.NoneType:
return nil, nil
default:
return nil, fmt.Errorf("expected string or none, got %q", value.Type())
}
}