Reverse allowlist
This commit is contained in:
@@ -3716,7 +3716,9 @@ func (r *valueResolver) resolveValue(row map[string]any, field string, value any
|
||||
switch typed := value.(type) {
|
||||
case map[string]any:
|
||||
if encoding, ok := r.valueEncodingForField(field); ok {
|
||||
if _, hasList := typed["list"]; hasList {
|
||||
_, hasList := typed["list"]
|
||||
_, hasAllExcept := typed["all_except"]
|
||||
if hasList || hasAllExcept {
|
||||
value, err := encodeConfiguredValueList(typed, encoding)
|
||||
if err != nil {
|
||||
return tlkCompiledValue{}, fmt.Errorf("field %s: %w", field, err)
|
||||
@@ -3792,11 +3794,23 @@ func encodeConfiguredValueList(obj map[string]any, encoding project.TopDataValue
|
||||
if strings.TrimSpace(encoding.Mode) != "packed_hex_list" {
|
||||
return "", fmt.Errorf("unsupported value encoding mode %q", encoding.Mode)
|
||||
}
|
||||
_, hasList := obj["list"]
|
||||
_, hasAllExcept := obj["all_except"]
|
||||
if hasList && hasAllExcept {
|
||||
return "", fmt.Errorf("list encoding object cannot contain both list and all_except")
|
||||
}
|
||||
for key := range obj {
|
||||
if key != "list" {
|
||||
if key != "list" && key != "all_except" {
|
||||
return "", fmt.Errorf("list encoding object contains unsupported key %q", key)
|
||||
}
|
||||
}
|
||||
if hasAllExcept {
|
||||
values, err := expandConfiguredValueAllExcept(obj["all_except"], encoding)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return formatConfiguredPackedHexList(values, encoding), nil
|
||||
}
|
||||
rawList, ok := obj["list"].([]any)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("list must be an array")
|
||||
@@ -3809,15 +3823,75 @@ func encodeConfiguredValueList(obj map[string]any, encoding project.TopDataValue
|
||||
}
|
||||
values = append(values, expanded...)
|
||||
}
|
||||
return formatConfiguredPackedHexList(values, encoding), nil
|
||||
}
|
||||
|
||||
func formatConfiguredPackedHexList(values []int, encoding project.TopDataValueEncodingConfig) string {
|
||||
if len(values) == 0 {
|
||||
return "", nil
|
||||
return ""
|
||||
}
|
||||
var builder strings.Builder
|
||||
builder.WriteString("0x")
|
||||
for _, value := range values {
|
||||
builder.WriteString(fmt.Sprintf("%0*X", encoding.HexWidth, value))
|
||||
}
|
||||
return builder.String(), nil
|
||||
return builder.String()
|
||||
}
|
||||
|
||||
func expandConfiguredValueAllExcept(raw any, encoding project.TopDataValueEncodingConfig) ([]int, error) {
|
||||
obj, ok := raw.(map[string]any)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("all_except must be an object")
|
||||
}
|
||||
for key := range obj {
|
||||
if key != "min" && key != "max" && key != "values" {
|
||||
return nil, fmt.Errorf("all_except contains unsupported key %q", key)
|
||||
}
|
||||
}
|
||||
min := encoding.Min
|
||||
if rawMin, ok := obj["min"]; ok {
|
||||
parsed, err := parseConfiguredListValue(rawMin, encoding)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("all_except min: %w", err)
|
||||
}
|
||||
min = parsed
|
||||
}
|
||||
max := encoding.Max
|
||||
if rawMax, ok := obj["max"]; ok {
|
||||
parsed, err := parseConfiguredListValue(rawMax, encoding)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("all_except max: %w", err)
|
||||
}
|
||||
max = parsed
|
||||
}
|
||||
if max < min {
|
||||
return nil, fmt.Errorf("all_except max %d is less than min %d", max, min)
|
||||
}
|
||||
rawValues, ok := obj["values"].([]any)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("all_except values must be an array")
|
||||
}
|
||||
excluded := map[int]struct{}{}
|
||||
for _, rawValue := range rawValues {
|
||||
expanded, err := expandConfiguredValueListItem(rawValue, encoding)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, value := range expanded {
|
||||
if value < min || value > max {
|
||||
return nil, fmt.Errorf("value %d outside all_except range %d-%d", value, min, max)
|
||||
}
|
||||
excluded[value] = struct{}{}
|
||||
}
|
||||
}
|
||||
values := make([]int, 0, max-min+1-len(excluded))
|
||||
for value := min; value <= max; value++ {
|
||||
if _, skip := excluded[value]; skip {
|
||||
continue
|
||||
}
|
||||
values = append(values, value)
|
||||
}
|
||||
return values, nil
|
||||
}
|
||||
|
||||
func expandConfiguredValueListItem(raw any, encoding project.TopDataValueEncodingConfig) ([]int, error) {
|
||||
|
||||
Reference in New Issue
Block a user