timshannon / badgerhold

BadgerHold is an embeddable NoSQL store for querying Go types built on Badger
MIT License
515 stars 52 forks source link

Add support for transformation tags #33

Closed ivanjaros closed 3 years ago

ivanjaros commented 3 years ago

I have some indices but I want them to be case-insensitive. So instead of having to define two fields - one for original value and the other for index value, it would be good if there would be tag support for something like 'lowercase' or 'uppercase' or something like that. This is especially useful for e-mail addresses where the case can vary but I always want to index it as lower-cased value. Same as things like handles/slugs like for twitter or github user names.

timshannon commented 3 years ago

There is a comparer interface that you can define on your type if you want it to compare values differently from Go standard.

There are examples in the tests (https://github.com/timshannon/badgerhold/blob/master/compare_test.go):

type CItemTest struct {
    Inner ItemTest
}

func (i *ItemTest) Compare(other interface{}) (int, error) {
    if other, ok := other.(ItemTest); ok {
        if i.ID == other.ID {
            return 0, nil
        }

        if i.ID < other.ID {
            return -1, nil
        }

        return 1, nil
    }

    return 0, &badgerhold.ErrTypeMismatch{Value: i, Other: other}
}
ivanjaros commented 3 years ago

i dont get it. dont you have indices stored as part of keys with values pointing to object ids? this example looks like you are loading entire objects and comparing them as a whole...where does the index come into this?

timshannon commented 3 years ago

Ah. Sorry I misunderstood. If you're talking about index values specifically, then you can implement the Storer interface.

https://github.com/timshannon/badgerhold/blob/master/store.go#L101