asdine / storm

Simple and powerful toolkit for BoltDB
MIT License
2.06k stars 138 forks source link

Range on user defined integer #253

Open mentalmap opened 5 years ago

mentalmap commented 5 years ago
package main

import (
    "fmt"
    "log"
    "time"

    "github.com/asdine/storm"
    bolt "go.etcd.io/bbolt"
)

type Sequence uint32

type Data struct {
    ID    Sequence `storm:"id"`
    Value string
}

func main() {
    db, err := storm.Open("my.db", storm.BoltOptions(0600, &bolt.Options{Timeout: 1 * time.Second}))
    if err != nil {
        log.Panicln(err)
    }
    defer db.Close()

    for i := 1; i <= 10; i++ {
        d := Data{ID: Sequence(i), Value: fmt.Sprintf("value_%d", i)}
        _ = db.Save(&d)
    }

    var result []Data
    _ = db.Range("ID", Sequence(0), Sequence(10), &result)

    log.Println(result)
}

The output is:

[{1 value_1} {10 value_10}]

not

[{1 value_1} {2 value_2} {3 value_3} {4 value_4} {5 value_5} {6 value_6} {7 value_7} {8 value_8} {9 value_9} {10 value_10}]