Remove blueprint:"filter(*)" tag support

The filter tag is unused, replaced with FilterPropertyStruct to
generate a new type at runtime that only contains the filtered
fields.

Test: unpack_test.go
Change-Id: Id91cf99290832094d05426f3263279836f0fea73
This commit is contained in:
Colin Cross 2020-01-20 17:12:29 -08:00
parent b89d91c67c
commit 571f77a60d
3 changed files with 6 additions and 106 deletions

View file

@ -145,14 +145,6 @@ func assembleModuleTypeInfo(r *Reader, name string, factory reflect.Value,
return nil, fmt.Errorf("nesting point %q not found", nestedName)
}
key, value, err := proptools.HasFilter(nestPoint.Tag)
if err != nil {
return nil, err
}
if key != "" {
nested.IncludeByTag(key, value)
}
nestPoint.Nest(nested)
}
mt.PropertyStructs = append(mt.PropertyStructs, ps)

View file

@ -17,8 +17,6 @@ package proptools
import (
"fmt"
"reflect"
"strconv"
"strings"
"text/scanner"
"github.com/google/blueprint/parser"
@ -60,7 +58,7 @@ func UnpackProperties(propertyDefs []*parser.Property,
panic("properties must be a pointer to a struct")
}
newErrs := unpackStructValue("", propertiesValue, propertyMap, "", "")
newErrs := unpackStructValue("", propertiesValue, propertyMap)
errs = append(errs, newErrs...)
if len(errs) >= maxUnpackErrors {
@ -130,7 +128,7 @@ func buildPropertyMap(namePrefix string, propertyDefs []*parser.Property,
}
func unpackStructValue(namePrefix string, structValue reflect.Value,
propertyMap map[string]*packedProperty, filterKey, filterValue string) []error {
propertyMap map[string]*packedProperty) []error {
structType := structValue.Type()
@ -215,7 +213,7 @@ func unpackStructValue(namePrefix string, structValue reflect.Value,
}
if field.Anonymous && fieldValue.Kind() == reflect.Struct {
newErrs := unpackStructValue(namePrefix, fieldValue, propertyMap, filterKey, filterValue)
newErrs := unpackStructValue(namePrefix, fieldValue, propertyMap)
errs = append(errs, newErrs...)
continue
}
@ -239,40 +237,11 @@ func unpackStructValue(namePrefix string, structValue reflect.Value,
continue
}
if filterKey != "" && !HasTag(field, filterKey, filterValue) {
errs = append(errs,
&UnpackError{
Err: fmt.Errorf("filtered field %s cannot be set in a Blueprint file", propertyName),
Pos: packedProperty.property.ColonPos,
})
if len(errs) >= maxUnpackErrors {
return errs
}
continue
}
var newErrs []error
if fieldValue.Kind() == reflect.Struct {
localFilterKey, localFilterValue := filterKey, filterValue
if k, v, err := HasFilter(field.Tag); err != nil {
errs = append(errs, err)
if len(errs) >= maxUnpackErrors {
return errs
}
} else if k != "" {
if filterKey != "" {
errs = append(errs, fmt.Errorf("nested filter tag not supported on field %q",
field.Name))
if len(errs) >= maxUnpackErrors {
return errs
}
} else {
localFilterKey, localFilterValue = k, v
}
}
newErrs = unpackStruct(propertyName+".", fieldValue,
packedProperty.property, propertyMap, localFilterKey, localFilterValue)
packedProperty.property, propertyMap)
errs = append(errs, newErrs...)
if len(errs) >= maxUnpackErrors {
@ -365,8 +334,7 @@ func propertyToValue(typ reflect.Type, property *parser.Property) (reflect.Value
}
func unpackStruct(namePrefix string, structValue reflect.Value,
property *parser.Property, propertyMap map[string]*packedProperty,
filterKey, filterValue string) []error {
property *parser.Property, propertyMap map[string]*packedProperty) []error {
m, ok := property.Value.Eval().(*parser.Map)
if !ok {
@ -381,31 +349,5 @@ func unpackStruct(namePrefix string, structValue reflect.Value,
return errs
}
return unpackStructValue(namePrefix, structValue, propertyMap, filterKey, filterValue)
}
func HasFilter(field reflect.StructTag) (k, v string, err error) {
tag := field.Get("blueprint")
for _, entry := range strings.Split(tag, ",") {
if strings.HasPrefix(entry, "filter") {
if !strings.HasPrefix(entry, "filter(") || !strings.HasSuffix(entry, ")") {
return "", "", fmt.Errorf("unexpected format for filter %q: missing ()", entry)
}
entry = strings.TrimPrefix(entry, "filter(")
entry = strings.TrimSuffix(entry, ")")
s := strings.Split(entry, ":")
if len(s) != 2 {
return "", "", fmt.Errorf("unexpected format for filter %q: expected single ':'", entry)
}
k = s[0]
v, err = strconv.Unquote(s[1])
if err != nil {
return "", "", fmt.Errorf("unexpected format for filter %q: %s", entry, err.Error())
}
return k, v, nil
}
}
return "", "", nil
return unpackStructValue(namePrefix, structValue, propertyMap)
}

View file

@ -16,7 +16,6 @@ package proptools
import (
"bytes"
"fmt"
"reflect"
"testing"
"text/scanner"
@ -219,39 +218,6 @@ var validUnpackTestCases = []struct {
},
},
{
input: `
m {
nested: {
foo: "abc",
},
bar: false,
baz: ["def", "ghi"],
}
`,
output: []interface{}{
struct {
Nested struct {
Foo string
} `blueprint:"filter(allowNested:\"true\")"`
Bar bool
Baz []string
}{
Nested: struct{ Foo string }{
Foo: "",
},
Bar: false,
Baz: []string{"def", "ghi"},
},
},
errs: []error{
&UnpackError{
Err: fmt.Errorf("filtered field nested.foo cannot be set in a Blueprint file"),
Pos: mkpos(30, 4, 9),
},
},
},
// Anonymous struct
{
input: `