Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions csup/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,26 +342,29 @@ func metadataValue(cctx *Context, sctx *super.Context, b *scode.Builder, id ID,
return sctx.MustLookupTypeRecord(fields)
case *Union:
cmp := expr.NewValueCompareFn(order.Asc, order.NullsLast)
max, min := super.Null, super.Null
var bb scode.Builder
var min, max *super.Value
for _, id := range m.Values {
val := newMetadataValue(cctx, sctx, &bb, id, projection)
if val.IsNull() {
continue
}
min2, max2 := val.Deref("min"), val.Deref("max")
if min == nil || (min2 != nil && cmp(*min, *min2) > 0) {
min = new(min2.Copy())
if min2 == nil {
continue
}
if min.IsNull() || cmp(min, *min2) > 0 {
min = min2.Copy()
}
if max == nil || (max2 != nil && cmp(*max, *max2) < 0) {
max = new(max2.Copy())
if max.IsNull() || cmp(max, *max2) < 0 {
max = max2.Copy()
}
}
if min == nil {
if min.IsNull() {
b.Append(nil)
return super.TypeNull
}
return metadataLeaf(sctx, b, *min, *max)
return metadataLeaf(sctx, b, min, max)
case *Int:
return metadataLeaf(sctx, b, super.NewInt(m.Typ, m.Min), super.NewInt(m.Typ, m.Max))
case *Uint:
Expand Down
8 changes: 4 additions & 4 deletions csup/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func TestObjectProjectMetadata(t *testing.T) {
func TestObjectProjectMetadataForUnion(t *testing.T) {
sctx := super.NewContext()
supValues := []string{
"{a:1::(int64|string),b?:2,c:3::(int64|null)}",
`{a:"s"::(int64|string),b?:_::int64,c:null::(int64|null)}`,
"{a:1::(int64|string),b?:2,c:3::(int64|null),d?:{e:4}}",
`{a:"s"::(int64|string),b?:_::int64,c:null::(int64|null),d?:_::{e:int64}}`,
}
builder := vector.NewDynamicValueBuilder()
for _, s := range supValues {
Expand All @@ -56,8 +56,8 @@ func TestObjectProjectMetadataForUnion(t *testing.T) {

o, err := csup.NewObject(bytes.NewReader(csupBytes))
require.NoError(t, err)
p := field.NewProjection(field.DottedList("a,b,c"))
p := field.NewProjection(field.DottedList("a,b,c,d"))
values := o.ProjectMetadata(super.NewContext(), p)
require.Len(t, values, 1)
require.Equal(t, `{a:{min:1,max:"s"},b:{min:2,max:2},c:{min:3,max:3}}`, sup.FormatValue(values[0]))
require.Equal(t, `{a:{min:1,max:"s"},b:{min:2,max:2},c:{min:3,max:3},d:null}`, sup.FormatValue(values[0]))
}
Loading