Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GO-4246 change value of bool relations in existing object #1838

29 changes: 29 additions & 0 deletions pkg/lib/database/anystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,32 @@ func (s tagStatusSort) AppendKey(tuple anyenc.Tuple, v *anyenc.Value) anyenc.Tup
return tuple.Append(s.arena.NewString(sortKey))
}
}

type boolSort struct {
arena *anyenc.Arena
relationKey string
reverse bool
}

func (b boolSort) Fields() []query.SortField {
return []query.SortField{
{
Field: "",
},
}
}

func (b boolSort) AppendKey(tuple anyenc.Tuple, v *anyenc.Value) anyenc.Tuple {
defer func() {
b.arena.Reset()
}()
val := v.Get(b.relationKey)
if val == nil {
val = b.arena.NewFalse()
}
if b.reverse {
return tuple.AppendInverted(val)
} else {
return tuple.Append(val)
}
}
23 changes: 23 additions & 0 deletions pkg/lib/database/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func (ko *KeyOrder) Compare(a, b *domain.Details) int {
av, bv = ko.tryExtractSnippet(a, b, av, bv)
av, bv = ko.tryExtractDateTime(av, bv)
av, bv = ko.tryExtractTag(av, bv)
av, bv = ko.tryExtractBool(av, bv)

comp := ko.tryCompareStrings(av, bv)
if comp == 0 {
Expand Down Expand Up @@ -112,6 +113,8 @@ func (ko *KeyOrder) AnystoreSort() query.Sort {
return ko.basicSort(anyenc.TypeString)
case model.RelationFormat_tag, model.RelationFormat_status:
return ko.tagStatusSort()
case model.RelationFormat_checkbox:
return ko.boolSort()
default:
return ko.basicSort(anyenc.TypeString)
}
Expand Down Expand Up @@ -178,6 +181,14 @@ func (ko *KeyOrder) textSort() query.Sort {
}
}

func (ko *KeyOrder) boolSort() query.Sort {
return boolSort{
arena: ko.arena,
relationKey: ko.Key.String(),
reverse: ko.Type == model.BlockContentDataviewSort_Desc,
}
}

func (ko *KeyOrder) tryAdjustEmptyPositions(av domain.Value, bv domain.Value, comp int) int {
if ko.EmptyPlacement == model.BlockContentDataviewSort_NotSpecified {
return comp
Expand Down Expand Up @@ -232,6 +243,18 @@ func (ko *KeyOrder) isSpecialSortOfEmptyValuesNeed(av domain.Value, bv domain.Va
(aString || !av.Ok()) && (bString || !bv.Ok())
}

func (ko *KeyOrder) tryExtractBool(av domain.Value, bv domain.Value) (domain.Value, domain.Value) {
if ko.relationFormat == model.RelationFormat_checkbox {
if !av.Ok() {
av = domain.Bool(false)
}
if !bv.Ok() {
bv = domain.Bool(false)
}
}
return av, bv
}

func (ko *KeyOrder) tryExtractTag(av domain.Value, bv domain.Value) (domain.Value, domain.Value) {
if ko.relationFormat == model.RelationFormat_tag || ko.relationFormat == model.RelationFormat_status {
av = ko.GetOptionValue(av)
Expand Down
30 changes: 30 additions & 0 deletions pkg/lib/database/order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,36 @@ func TestKeyOrder_Compare(t *testing.T) {
ko := &KeyOrder{disableCollator: true, arena: arena, Key: bundle.RelationKeySpaceOrder, Type: model.BlockContentDataviewSort_Asc, relationFormat: model.RelationFormat_shorttext}
assertCompare(t, ko, a, b, -1)
})
t.Run("compare_bool_false_null", func(t *testing.T) {
a := domain.NewDetailsFromMap(map[domain.RelationKey]domain.Value{bundle.RelationKeyDone: domain.Bool(false)})
b := domain.NewDetailsFromMap(map[domain.RelationKey]domain.Value{})
ko := &KeyOrder{arena: arena, Key: bundle.RelationKeyDone, Type: model.BlockContentDataviewSort_Asc, relationFormat: model.RelationFormat_checkbox}
assertCompare(t, ko, a, b, 0)
})
t.Run("compare_bool_true_null", func(t *testing.T) {
a := domain.NewDetailsFromMap(map[domain.RelationKey]domain.Value{bundle.RelationKeyDone: domain.Bool(true)})
b := domain.NewDetailsFromMap(map[domain.RelationKey]domain.Value{})
ko := &KeyOrder{arena: arena, Key: bundle.RelationKeyDone, Type: model.BlockContentDataviewSort_Asc, relationFormat: model.RelationFormat_checkbox}
assertCompare(t, ko, a, b, 1)
})
t.Run("compare_bool_true_null_desc", func(t *testing.T) {
a := domain.NewDetailsFromMap(map[domain.RelationKey]domain.Value{bundle.RelationKeyDone: domain.Bool(true)})
b := domain.NewDetailsFromMap(map[domain.RelationKey]domain.Value{})
ko := &KeyOrder{arena: arena, Key: bundle.RelationKeyDone, Type: model.BlockContentDataviewSort_Desc, relationFormat: model.RelationFormat_checkbox}
assertCompare(t, ko, a, b, -1)
})
t.Run("compare_bool_true_false", func(t *testing.T) {
a := domain.NewDetailsFromMap(map[domain.RelationKey]domain.Value{bundle.RelationKeyDone: domain.Bool(true)})
b := domain.NewDetailsFromMap(map[domain.RelationKey]domain.Value{bundle.RelationKeyDone: domain.Bool(false)})
ko := &KeyOrder{arena: arena, Key: bundle.RelationKeyDone, Type: model.BlockContentDataviewSort_Asc, relationFormat: model.RelationFormat_checkbox}
assertCompare(t, ko, a, b, 1)
})
t.Run("compare_bool_true_true", func(t *testing.T) {
a := domain.NewDetailsFromMap(map[domain.RelationKey]domain.Value{bundle.RelationKeyDone: domain.Bool(true)})
b := domain.NewDetailsFromMap(map[domain.RelationKey]domain.Value{bundle.RelationKeyDone: domain.Bool(true)})
ko := &KeyOrder{arena: arena, Key: bundle.RelationKeyDone, Type: model.BlockContentDataviewSort_Asc, relationFormat: model.RelationFormat_checkbox}
assertCompare(t, ko, a, b, 0)
})
}

func TestKeyUnicodeOrder_Compare(t *testing.T) {
Expand Down
Loading