Compare commits
11 Commits
main
...
rewrite-fr
Author | SHA1 | Date | |
---|---|---|---|
5a85c195a7 | |||
741eac472a | |||
319a3b033f | |||
fe8f4d644a | |||
638cf4ded7 | |||
c64bda66f5 | |||
ac4d008f57 | |||
7c152f1273 | |||
de7d97d96b | |||
0e0dff859f | |||
0ac08f775e |
4
.gitignore
vendored
4
.gitignore
vendored
@ -19,6 +19,4 @@ _cgo_export.*
|
||||
|
||||
_testmain.go
|
||||
|
||||
*.exe
|
||||
|
||||
example/
|
||||
*.exe
|
12
be_decode.go
Normal file
12
be_decode.go
Normal file
@ -0,0 +1,12 @@
|
||||
package boltease
|
||||
|
||||
import "reflect"
|
||||
|
||||
func (db *DB) Load(path []string, dest any) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// A Number represents a Boltease number literal.
|
||||
type Number string
|
||||
|
||||
var numberType = reflect.TypeFor[Number]()
|
915
be_encode.go
Normal file
915
be_encode.go
Normal file
@ -0,0 +1,915 @@
|
||||
package boltease
|
||||
|
||||
import (
|
||||
"encoding"
|
||||
"fmt"
|
||||
"math"
|
||||
"reflect"
|
||||
"slices"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
func (db *DB) Save(path []string, k string, v any) error {
|
||||
e := newWriterState(db, path)
|
||||
defer writerStatePool.Put(e)
|
||||
|
||||
err := e.marshal(db, path, k, v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Marshaler is the interfacce implemented by types that
|
||||
// can marshal themselves into a db
|
||||
type Marshaler interface {
|
||||
MarshalBoltease(db *DB, path []string, key string) error
|
||||
}
|
||||
|
||||
// An UnsupportedTypeError is returned by [Marsha] when attempting
|
||||
// to write an unsupported value type.
|
||||
type UnsupportedTypeError struct {
|
||||
Type reflect.Type
|
||||
}
|
||||
|
||||
func (e *UnsupportedTypeError) Error() string {
|
||||
return "boltease: unsupported type: " + e.Type.String()
|
||||
}
|
||||
|
||||
// An UnsupportedValueError is returned by [Marshal] when attempting
|
||||
// to encode an unsupported value.
|
||||
type UnsupportedValueError struct {
|
||||
Value reflect.Value
|
||||
Str string
|
||||
}
|
||||
|
||||
func (e *UnsupportedValueError) Error() string {
|
||||
return "boltease: unsupported value: " + e.Str
|
||||
}
|
||||
|
||||
// A MarshalError represents an error from calling a
|
||||
// [Marshaler.MarshelBoltease] or [encoding.TextMarshaler.MarshalText] method.
|
||||
type MarshalerError struct {
|
||||
Type reflect.Type
|
||||
Err error
|
||||
sourceFunc string
|
||||
}
|
||||
|
||||
func (e *MarshalerError) Error() string {
|
||||
srcFunc := e.sourceFunc
|
||||
if srcFunc == "" {
|
||||
srcFunc = "MarshalBoltease"
|
||||
}
|
||||
return "boltease: error calling " + srcFunc +
|
||||
" for type " + e.Type.String() +
|
||||
": " + e.Err.Error()
|
||||
}
|
||||
|
||||
// Unwrap returns the underlying error.
|
||||
func (e *MarshalerError) Unwrap() error { return e.Err }
|
||||
|
||||
type writerState struct {
|
||||
db *DB
|
||||
path []string
|
||||
ptrLevel uint
|
||||
ptrSeen map[any]struct{}
|
||||
}
|
||||
|
||||
func (es *writerState) WriteString(key, val string) error {
|
||||
return es.Write([]byte(key), []byte(val))
|
||||
}
|
||||
|
||||
func (es *writerState) Write(key []byte, val []byte) error {
|
||||
return es.db.SetBBytes(es.path, key, val)
|
||||
}
|
||||
|
||||
const startDetectingCyclesAfter = 1000
|
||||
|
||||
var writerStatePool sync.Pool
|
||||
|
||||
func newWriterState(db *DB, path []string) *writerState {
|
||||
if v := writerStatePool.Get(); v != nil {
|
||||
e := v.(*writerState)
|
||||
if len(e.ptrSeen) > 0 {
|
||||
panic("ptrWriter.write should have emptied ptrSeen via defers")
|
||||
}
|
||||
e.ptrLevel = 0
|
||||
return e
|
||||
}
|
||||
return &writerState{
|
||||
db: db,
|
||||
path: path,
|
||||
ptrSeen: make(map[any]struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
// bolteaseError is an error wrapper type for internal use only.
|
||||
// Panics with errors are wrapped in bolteaseError so that the top-level recover
|
||||
// can distinguish intentional panics from this package.
|
||||
type bolteaseError struct{ error }
|
||||
|
||||
func (e *writerState) marshal(db *DB, path []string, k string, v any) (err error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
if be, ok := r.(bolteaseError); ok {
|
||||
err = be.error
|
||||
} else {
|
||||
panic(r)
|
||||
}
|
||||
}
|
||||
}()
|
||||
e.reflectValue(k, reflect.ValueOf(v))
|
||||
return nil
|
||||
}
|
||||
|
||||
// error aborts the encoding by panicking with err wrapped in bolteaseError.
|
||||
func (e *writerState) error(err error) {
|
||||
panic(bolteaseError{err})
|
||||
}
|
||||
|
||||
func isEmptyValue(v reflect.Value) bool {
|
||||
switch v.Kind() {
|
||||
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
|
||||
return v.Len() == 0
|
||||
case reflect.Bool,
|
||||
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
|
||||
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr,
|
||||
reflect.Float32, reflect.Float64,
|
||||
reflect.Interface, reflect.Pointer:
|
||||
return v.IsZero()
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (e *writerState) reflectValue(k string, v reflect.Value) {
|
||||
valueWriter(v)(e, k, v)
|
||||
}
|
||||
|
||||
type writerFunc func(e *writerState, k string, v reflect.Value)
|
||||
|
||||
var writerCache sync.Map // map[reflect.Type]writerFunc
|
||||
|
||||
func valueWriter(v reflect.Value) writerFunc {
|
||||
if !v.IsValid() {
|
||||
return invalidValueWriter
|
||||
}
|
||||
return typeWriter(v.Type())
|
||||
}
|
||||
|
||||
func typeWriter(t reflect.Type) writerFunc {
|
||||
if fi, ok := writerCache.Load(t); ok {
|
||||
return fi.(writerFunc)
|
||||
}
|
||||
|
||||
// To deal with recursive types, populate the map with an
|
||||
// indirect func before we build it. This type waits on the
|
||||
// real func (f) to be ready and then calls it. This indirect
|
||||
// func is only used for recursive types.
|
||||
var (
|
||||
wg sync.WaitGroup
|
||||
f writerFunc
|
||||
)
|
||||
wg.Add(1)
|
||||
fi, loaded := writerCache.LoadOrStore(t, writerFunc(func(e *writerState, k string, v reflect.Value) {
|
||||
wg.Wait()
|
||||
f(e, k, v)
|
||||
}))
|
||||
if loaded {
|
||||
return fi.(writerFunc)
|
||||
}
|
||||
|
||||
// Compute the real writer and replace the indirect func with it.
|
||||
f = newTypeWriter(t, true)
|
||||
wg.Done()
|
||||
writerCache.Store(t, f)
|
||||
return f
|
||||
}
|
||||
|
||||
var (
|
||||
marshalerType = reflect.TypeFor[Marshaler]()
|
||||
textMarshalerType = reflect.TypeFor[encoding.TextMarshaler]()
|
||||
)
|
||||
|
||||
// newTypeWriter constructs an writerFunc for a type.
|
||||
// The returned writer only checks CanAddr when allowAddr is true.
|
||||
func newTypeWriter(t reflect.Type, allowAddr bool) writerFunc {
|
||||
// if we have a non-pointer value whose type implements
|
||||
// Marshaler with a value receiver, then we're better off taking
|
||||
// the address of the value - otherwise we end up with an
|
||||
// allocation as we cast the value to an interface.
|
||||
if t.Kind() != reflect.Pointer && allowAddr && reflect.PointerTo(t).Implements(marshalerType) {
|
||||
return newCondAddrWriter(addrMarshalerWriter, newTypeWriter(t, false))
|
||||
}
|
||||
if t.Implements(marshalerType) {
|
||||
return marshalerWriter
|
||||
}
|
||||
if t.Kind() != reflect.Pointer && allowAddr && reflect.PointerTo(t).Implements(textMarshalerType) {
|
||||
return newCondAddrWriter(addrTextMarshalerWriter, newTypeWriter(t, false))
|
||||
}
|
||||
if t.Implements(textMarshalerType) {
|
||||
return textMarshalerWriter
|
||||
}
|
||||
|
||||
switch t.Kind() {
|
||||
case reflect.Bool:
|
||||
return boolWriter
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
return intWriter
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
||||
return uintWriter
|
||||
case reflect.Float32:
|
||||
return float32Writer
|
||||
case reflect.Float64:
|
||||
return float64Writer
|
||||
case reflect.String:
|
||||
return stringWriter
|
||||
case reflect.Interface:
|
||||
return interfaceWriter
|
||||
case reflect.Struct:
|
||||
return newStructWriter(t)
|
||||
case reflect.Map:
|
||||
return newMapWriter(t)
|
||||
case reflect.Slice:
|
||||
return newSliceWriter(t)
|
||||
case reflect.Array:
|
||||
return newArrayWriter(t)
|
||||
case reflect.Pointer:
|
||||
return newPtrWriter(t)
|
||||
default:
|
||||
return unsupportedTypeWriter
|
||||
}
|
||||
}
|
||||
|
||||
func invalidValueWriter(e *writerState, k string, v reflect.Value) {
|
||||
e.WriteString(k, "null")
|
||||
}
|
||||
|
||||
func marshalerWriter(e *writerState, k string, v reflect.Value) {
|
||||
if v.Kind() == reflect.Pointer && v.IsNil() {
|
||||
e.WriteString(k, "null")
|
||||
return
|
||||
}
|
||||
m, ok := v.Interface().(Marshaler)
|
||||
if !ok {
|
||||
e.WriteString(k, "null")
|
||||
return
|
||||
}
|
||||
err := m.MarshalBoltease(e.db, e.path, k)
|
||||
if err != nil {
|
||||
e.error(&MarshalerError{v.Type(), err, "MarshalBoltease"})
|
||||
}
|
||||
}
|
||||
|
||||
func addrMarshalerWriter(e *writerState, k string, v reflect.Value) {
|
||||
va := v.Addr()
|
||||
if va.IsNil() {
|
||||
e.WriteString(k, "null")
|
||||
return
|
||||
}
|
||||
m := va.Interface().(Marshaler)
|
||||
err := m.MarshalBoltease(e.db, e.path, k)
|
||||
if err != nil {
|
||||
e.error(&MarshalerError{v.Type(), err, "MarshalBoltease"})
|
||||
}
|
||||
}
|
||||
|
||||
func textMarshalerWriter(e *writerState, k string, v reflect.Value) {
|
||||
if v.Kind() == reflect.Pointer && v.IsNil() {
|
||||
e.WriteString(k, "null")
|
||||
return
|
||||
}
|
||||
m, ok := v.Interface().(encoding.TextMarshaler)
|
||||
if !ok {
|
||||
e.WriteString(k, "null")
|
||||
return
|
||||
}
|
||||
b, err := m.MarshalText()
|
||||
if err != nil {
|
||||
e.error(&MarshalerError{v.Type(), err, "MarshalText"})
|
||||
}
|
||||
e.Write([]byte(k), b)
|
||||
}
|
||||
|
||||
func addrTextMarshalerWriter(e *writerState, k string, v reflect.Value) {
|
||||
va := v.Addr()
|
||||
if va.IsNil() {
|
||||
e.WriteString(k, "null")
|
||||
return
|
||||
}
|
||||
m := va.Interface().(encoding.TextMarshaler)
|
||||
b, err := m.MarshalText()
|
||||
if err != nil {
|
||||
e.error(&MarshalerError{v.Type(), err, "MarshalText"})
|
||||
}
|
||||
e.Write([]byte(k), b)
|
||||
}
|
||||
|
||||
func boolWriter(e *writerState, k string, v reflect.Value) {
|
||||
e.WriteString(k, strconv.FormatBool(v.Bool()))
|
||||
}
|
||||
|
||||
func intWriter(e *writerState, k string, v reflect.Value) {
|
||||
e.WriteString(k, strconv.FormatInt(v.Int(), 10))
|
||||
}
|
||||
|
||||
func uintWriter(e *writerState, k string, v reflect.Value) {
|
||||
e.WriteString(k, strconv.FormatUint(v.Uint(), 10))
|
||||
}
|
||||
|
||||
type floatWriter int // number of bits
|
||||
|
||||
func (bits floatWriter) write(e *writerState, k string, v reflect.Value) {
|
||||
f := v.Float()
|
||||
if math.IsInf(f, 0) || math.IsNaN(f) {
|
||||
e.error(&UnsupportedValueError{v, strconv.FormatFloat(f, 'g', -1, int(bits))})
|
||||
}
|
||||
|
||||
// Connvert as if by ES6 number to string conversion.
|
||||
// This matches most other JSON generators.
|
||||
// See golang.org/issue/6384 and golang.org/issue/14135.
|
||||
// Like fmt %g, but the exponent cutoffs are different
|
||||
// and exponents themselves are not padded to two digits.
|
||||
b := []byte{}
|
||||
abs := math.Abs(f)
|
||||
fmt := byte('f')
|
||||
// Note: Must use float32 comparisons for underlying float32 value to get precise cutoffs right.
|
||||
if abs != 0 {
|
||||
if bits == 64 && (abs < 1e-6 || abs >= 1e21) || bits == 32 && (float32(abs) < 1e-6 || float32(abs) >= 1e21) {
|
||||
fmt = 'e'
|
||||
}
|
||||
}
|
||||
b = strconv.AppendFloat(b, f, fmt, -1, int(bits))
|
||||
if fmt == 'e' {
|
||||
// clean up e-09 to e-9
|
||||
n := len(b)
|
||||
if n >= 4 && b[n-4] == 'e' && b[n-3] == '-' && b[n-2] == '0' {
|
||||
b[n-2] = b[n-1]
|
||||
b = b[:n-1]
|
||||
}
|
||||
}
|
||||
e.Write([]byte(k), b)
|
||||
}
|
||||
|
||||
var (
|
||||
float32Writer = (floatWriter(32)).write
|
||||
float64Writer = (floatWriter(64)).write
|
||||
)
|
||||
|
||||
func stringWriter(e *writerState, k string, v reflect.Value) {
|
||||
if v.Type() == numberType {
|
||||
numStr := v.String()
|
||||
// In Go1.5 the empty string writes to "0", while this is not a valid number literal
|
||||
// we keep compatibility so check validity after this.
|
||||
if numStr == "" {
|
||||
numStr = "0" // Number's zero-val
|
||||
}
|
||||
if !isValidNumber(numStr) {
|
||||
e.error(fmt.Errorf("boltease: invalid number literal %q", numStr))
|
||||
}
|
||||
b := []byte{}
|
||||
b = append(b, numStr...)
|
||||
e.Write([]byte(k), b)
|
||||
return
|
||||
}
|
||||
e.Write([]byte(k), []byte(v.String()))
|
||||
}
|
||||
|
||||
func isValidNumber(s string) bool {
|
||||
// This function implements the JSON numbers grammar.
|
||||
// See https://tools.ietf.org/html/rfc7159#section-6
|
||||
// and https://www.json.org/img/number.png
|
||||
|
||||
if s == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
// Optional -
|
||||
if s[0] == '-' {
|
||||
s = s[1:]
|
||||
if s == "" {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// Digits
|
||||
switch {
|
||||
default:
|
||||
return false
|
||||
|
||||
case s[0] == '0':
|
||||
s = s[1:]
|
||||
|
||||
case '1' <= s[0] && s[0] <= '9':
|
||||
s = s[1:]
|
||||
for len(s) > 0 && '0' <= s[0] && s[0] <= '9' {
|
||||
s = s[1:]
|
||||
}
|
||||
}
|
||||
|
||||
// . followed by 1 or more digits.
|
||||
if len(s) >= 2 && s[0] == '.' && '0' <= s[1] && s[1] <= '9' {
|
||||
s = s[2:]
|
||||
for len(s) > 0 && '0' <= s[0] && s[0] <= '9' {
|
||||
s = s[1:]
|
||||
}
|
||||
}
|
||||
|
||||
// e or E followed by an optional - or + and
|
||||
// 1 or more digits.
|
||||
if len(s) >= 2 && (s[0] == 'e' || s[0] == 'E') {
|
||||
s = s[1:]
|
||||
if s[0] == '+' || s[0] == '-' {
|
||||
s = s[1:]
|
||||
if s == "" {
|
||||
return false
|
||||
}
|
||||
}
|
||||
for len(s) > 0 && '0' <= s[0] && s[0] <= '9' {
|
||||
s = s[1:]
|
||||
}
|
||||
}
|
||||
|
||||
// Make sure we are at the end.
|
||||
return s == ""
|
||||
}
|
||||
|
||||
func interfaceWriter(e *writerState, k string, v reflect.Value) {
|
||||
if v.IsNil() {
|
||||
e.WriteString(k, "null")
|
||||
return
|
||||
}
|
||||
e.reflectValue(k, v.Elem())
|
||||
}
|
||||
|
||||
func unsupportedTypeWriter(e *writerState, k string, v reflect.Value) {
|
||||
e.error(&UnsupportedTypeError{v.Type()})
|
||||
}
|
||||
|
||||
type structWriter struct {
|
||||
fields structFields
|
||||
}
|
||||
|
||||
type structFields struct {
|
||||
list []field
|
||||
byExactName map[string]*field
|
||||
byFoldedName map[string]*field
|
||||
}
|
||||
|
||||
// Write a struct at e.path
|
||||
func (se structWriter) write(e *writerState, k string, v reflect.Value) {
|
||||
// Add the key for this struct to the writerState
|
||||
e.path = append(e.path, k)
|
||||
// Pop it when we're done.
|
||||
defer func() { e.path = e.path[:len(e.path)-1] }()
|
||||
|
||||
FieldLoop:
|
||||
for i := range se.fields.list {
|
||||
f := &se.fields.list[i]
|
||||
// Find the nested struct field by following f.index.
|
||||
fv := v
|
||||
for _, i := range f.index {
|
||||
if fv.Kind() == reflect.Pointer {
|
||||
if fv.IsNil() {
|
||||
continue FieldLoop
|
||||
}
|
||||
fv = fv.Elem()
|
||||
}
|
||||
fv = fv.Field(i)
|
||||
}
|
||||
if f.omitEmpty && isEmptyValue(fv) {
|
||||
continue
|
||||
}
|
||||
f.writer(e, f.name, fv)
|
||||
}
|
||||
}
|
||||
|
||||
func newStrucWriter(t reflect.Type) writerFunc {
|
||||
se := structWriter{fields: cachedTypeFields(t)}
|
||||
return se.write
|
||||
}
|
||||
|
||||
func newStructWriter(t reflect.Type) writerFunc {
|
||||
se := structWriter{fields: cachedTypeFields(t)}
|
||||
return se.write
|
||||
}
|
||||
|
||||
type mapWriter struct {
|
||||
elemEnc writerFunc
|
||||
}
|
||||
|
||||
func (me mapWriter) write(e *writerState, k string, v reflect.Value) {
|
||||
if v.IsNil() {
|
||||
e.WriteString(k, "null")
|
||||
return
|
||||
}
|
||||
if e.ptrLevel++; e.ptrLevel > startDetectingCyclesAfter {
|
||||
// We're a large number of nested ptrWriter.write calls deep;
|
||||
// start checking if we've run into a pointer cycle.
|
||||
ptr := v.UnsafePointer()
|
||||
if _, ok := e.ptrSeen[ptr]; ok {
|
||||
e.error(&UnsupportedValueError{v, fmt.Sprintf("encountered a cycle via %s", v.Type())})
|
||||
}
|
||||
e.ptrSeen[ptr] = struct{}{}
|
||||
defer delete(e.ptrSeen, ptr)
|
||||
}
|
||||
|
||||
// Extract and sort the keys.
|
||||
var (
|
||||
sv = make([]reflectWithString, v.Len())
|
||||
mi = v.MapRange()
|
||||
err error
|
||||
)
|
||||
for i := 0; mi.Next(); i++ {
|
||||
if sv[i].ks, err = resolveKeyName(mi.Key()); err != nil {
|
||||
e.error(fmt.Errorf("boltease: encoding error for type %q: %q", v.Type().String(), err.Error()))
|
||||
}
|
||||
sv[i].v = mi.Value()
|
||||
}
|
||||
slices.SortFunc(sv, func(i, j reflectWithString) int {
|
||||
return strings.Compare(i.ks, j.ks)
|
||||
})
|
||||
|
||||
for i, kv := range sv {
|
||||
me.elemEnc(e, sv[i].ks, kv.v)
|
||||
}
|
||||
e.ptrLevel--
|
||||
}
|
||||
|
||||
func newMapWriter(t reflect.Type) writerFunc {
|
||||
switch t.Key().Kind() {
|
||||
case reflect.String,
|
||||
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
|
||||
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
||||
default:
|
||||
if !t.Key().Implements(textMarshalerType) {
|
||||
return unsupportedTypeWriter
|
||||
}
|
||||
}
|
||||
me := mapWriter{typeWriter(t.Elem())}
|
||||
return me.write
|
||||
}
|
||||
|
||||
func writeByteSlice(e *writerState, k string, v reflect.Value) {
|
||||
if v.IsNil() {
|
||||
e.WriteString(k, "null")
|
||||
return
|
||||
}
|
||||
e.Write([]byte(k), v.Bytes())
|
||||
}
|
||||
|
||||
// sliceWriter just wraps an arrayWriter, checking to make sure the value isn't nil.
|
||||
type sliceWriter struct {
|
||||
arrayWriter writerFunc
|
||||
}
|
||||
|
||||
func (se sliceWriter) write(e *writerState, k string, v reflect.Value) {
|
||||
if v.IsNil() {
|
||||
e.WriteString(k, "null")
|
||||
return
|
||||
}
|
||||
if e.ptrLevel++; e.ptrLevel > startDetectingCyclesAfter {
|
||||
// We're a large number of nested ptrWriter.write calls deep;
|
||||
// start checking if we've run into a pointer cycle.
|
||||
// Here we use a struct to memorize the pointer to the first element of the slice
|
||||
// and its length.
|
||||
ptr := struct {
|
||||
ptr interface{} // always an unsafe.Pointer, but avoids a dependency on package unsafe
|
||||
len int
|
||||
}{v.UnsafePointer(), v.Len()}
|
||||
if _, ok := e.ptrSeen[ptr]; ok {
|
||||
e.error(&UnsupportedValueError{v, fmt.Sprintf("encountered a cycle via %s", v.Type())})
|
||||
}
|
||||
e.ptrSeen[ptr] = struct{}{}
|
||||
defer delete(e.ptrSeen, ptr)
|
||||
}
|
||||
se.arrayWriter(e, k, v)
|
||||
e.ptrLevel--
|
||||
}
|
||||
|
||||
func newSliceWriter(t reflect.Type) writerFunc {
|
||||
// Byte slices get special treatment; arrays don't.
|
||||
if t.Elem().Kind() == reflect.Uint8 {
|
||||
p := reflect.PointerTo(t.Elem())
|
||||
if !p.Implements(marshalerType) && !p.Implements(textMarshalerType) {
|
||||
return writeByteSlice
|
||||
}
|
||||
}
|
||||
enc := sliceWriter{newArrayWriter(t)}
|
||||
return enc.write
|
||||
}
|
||||
|
||||
type arrayWriter struct {
|
||||
elemWrite writerFunc
|
||||
}
|
||||
|
||||
func (ae arrayWriter) write(e *writerState, k string, v reflect.Value) {
|
||||
e.path = append(e.path, k)
|
||||
defer func() { e.path = e.path[:len(e.path)-1] }()
|
||||
n := v.Len()
|
||||
for i := 0; i < n; i++ {
|
||||
ae.elemWrite(e, strconv.Itoa(i), v.Index(i))
|
||||
}
|
||||
}
|
||||
|
||||
func newArrayWriter(t reflect.Type) writerFunc {
|
||||
w := arrayWriter{typeWriter(t.Elem())}
|
||||
return w.write
|
||||
}
|
||||
|
||||
type ptrWriter struct {
|
||||
elemWrite writerFunc
|
||||
}
|
||||
|
||||
func (pe ptrWriter) write(e *writerState, k string, v reflect.Value) {
|
||||
if v.IsNil() {
|
||||
e.WriteString(k, "null")
|
||||
return
|
||||
}
|
||||
if e.ptrLevel++; e.ptrLevel > startDetectingCyclesAfter {
|
||||
// We're a large number of nested ptrWriter.write calls deep;
|
||||
// start checking if we've run into a pointer cycle.
|
||||
ptr := v.Interface()
|
||||
if _, ok := e.ptrSeen[ptr]; ok {
|
||||
e.error(&UnsupportedValueError{v, fmt.Sprintf("encountered a cycle via %s", v.Type())})
|
||||
}
|
||||
e.ptrSeen[ptr] = struct{}{}
|
||||
defer delete(e.ptrSeen, ptr)
|
||||
}
|
||||
pe.elemWrite(e, k, v.Elem())
|
||||
e.ptrLevel--
|
||||
}
|
||||
|
||||
func newPtrWriter(t reflect.Type) writerFunc {
|
||||
w := ptrWriter{typeWriter(t.Elem())}
|
||||
return w.write
|
||||
}
|
||||
|
||||
type condAddrWriter struct {
|
||||
canAddrWrite, elseWrite writerFunc
|
||||
}
|
||||
|
||||
func (ce condAddrWriter) write(e *writerState, k string, v reflect.Value) {
|
||||
if v.CanAddr() {
|
||||
ce.canAddrWrite(e, k, v)
|
||||
} else {
|
||||
ce.elseWrite(e, k, v)
|
||||
}
|
||||
}
|
||||
|
||||
// newCondAddrWriter returns a writer that checks whether its value
|
||||
// CanAddr and delegates to canAddrWrite if so, else to elseWrite.
|
||||
func newCondAddrWriter(canAddrWrite, elseWrite writerFunc) writerFunc {
|
||||
w := condAddrWriter{canAddrWrite: canAddrWrite, elseWrite: elseWrite}
|
||||
return w.write
|
||||
}
|
||||
|
||||
func typeByIndex(t reflect.Type, index []int) reflect.Type {
|
||||
for _, i := range index {
|
||||
if t.Kind() == reflect.Pointer {
|
||||
t = t.Elem()
|
||||
}
|
||||
t = t.Field(i).Type
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
type reflectWithString struct {
|
||||
v reflect.Value
|
||||
ks string
|
||||
}
|
||||
|
||||
func resolveKeyName(k reflect.Value) (string, error) {
|
||||
if k.Kind() == reflect.String {
|
||||
return k.String(), nil
|
||||
}
|
||||
if tm, ok := k.Interface().(encoding.TextMarshaler); ok {
|
||||
if k.Kind() == reflect.Pointer && k.IsNil() {
|
||||
return "", nil
|
||||
}
|
||||
buf, err := tm.MarshalText()
|
||||
return string(buf), err
|
||||
}
|
||||
switch k.Kind() {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
return strconv.FormatInt(k.Int(), 10), nil
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
||||
return strconv.FormatUint(k.Uint(), 10), nil
|
||||
}
|
||||
panic("unexpected map key type")
|
||||
}
|
||||
|
||||
// A field represents a single field found in a struct.
|
||||
type field struct {
|
||||
name string
|
||||
nameBytes []byte
|
||||
|
||||
tag bool
|
||||
index []int
|
||||
typ reflect.Type
|
||||
omitEmpty bool
|
||||
|
||||
writer writerFunc
|
||||
}
|
||||
|
||||
// byIndex sorts field by index sequence.
|
||||
type byIndex []field
|
||||
|
||||
func (x byIndex) Len() int { return len(x) }
|
||||
|
||||
func (x byIndex) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
|
||||
|
||||
func (x byIndex) Less(i, j int) bool {
|
||||
for k, xik := range x[i].index {
|
||||
if k >= len(x[j].index) {
|
||||
return false
|
||||
}
|
||||
if xik != x[j].index[k] {
|
||||
return xik < x[j].index[k]
|
||||
}
|
||||
}
|
||||
return len(x[i].index) < len(x[j].index)
|
||||
}
|
||||
|
||||
// typeFields returns a list of fields that boltease should recognize for the given type.
|
||||
// The algorithm is breadth-first search over the set of structs to include - the top struct
|
||||
// and then any reachable anonymous structs.
|
||||
func typeFields(t reflect.Type) structFields {
|
||||
// Anonymous fields to explore at the current level and the next
|
||||
current := []field{}
|
||||
next := []field{{typ: t}}
|
||||
|
||||
// Count of queued names for current level and the next.
|
||||
var count, nextCount map[reflect.Type]int
|
||||
|
||||
// Types already visited at an earlier level.
|
||||
visited := map[reflect.Type]bool{}
|
||||
|
||||
// Fields found.
|
||||
var fields []field
|
||||
|
||||
for len(next) > 0 {
|
||||
current, next = next, current[:0]
|
||||
count, nextCount = nextCount, map[reflect.Type]int{}
|
||||
|
||||
for _, f := range current {
|
||||
if visited[f.typ] {
|
||||
continue
|
||||
}
|
||||
visited[f.typ] = true
|
||||
// Scan f.typ for fields to include.
|
||||
for i := 0; i < f.typ.NumField(); i++ {
|
||||
sf := f.typ.Field(i)
|
||||
if sf.Anonymous {
|
||||
t := sf.Type
|
||||
if t.Kind() == reflect.Pointer {
|
||||
t = t.Elem()
|
||||
}
|
||||
if !sf.IsExported() && t.Kind() != reflect.Struct {
|
||||
// Ignore embedded fields of unexported non-struct types.
|
||||
continue
|
||||
}
|
||||
// Do not ignore embedded fields of unexported struct types
|
||||
// /since they may have exported fields.
|
||||
} else if !sf.IsExported() {
|
||||
// Ignore unexported non-embedded fields.
|
||||
continue
|
||||
}
|
||||
tag := sf.Tag.Get("boltease")
|
||||
if tag == "-" {
|
||||
continue
|
||||
}
|
||||
name, opts := parseTag(tag)
|
||||
if !isValidTag(name) {
|
||||
name = ""
|
||||
}
|
||||
index := make([]int, len(f.index)+1)
|
||||
copy(index, f.index)
|
||||
index[len(f.index)] = i
|
||||
|
||||
ft := sf.Type
|
||||
if ft.Name() == "" && ft.Kind() == reflect.Pointer {
|
||||
// Follow pointer.
|
||||
ft = ft.Elem()
|
||||
}
|
||||
|
||||
// Record found field and index sequence.
|
||||
if name != "" || !sf.Anonymous || ft.Kind() != reflect.Struct {
|
||||
tagged := name != ""
|
||||
if name == "" {
|
||||
name = sf.Name
|
||||
}
|
||||
field := field{
|
||||
name: name,
|
||||
tag: tagged,
|
||||
index: index,
|
||||
typ: ft,
|
||||
omitEmpty: opts.Contains("omitempty"),
|
||||
}
|
||||
field.nameBytes = []byte(field.name)
|
||||
fields = append(fields, field)
|
||||
if count[f.typ] > 1 {
|
||||
// If there were multiple instances, add a second,
|
||||
// so thta the annihilation code will see a duplicate.
|
||||
// it only cares about the distinction between 1 or 2,
|
||||
// so don't bother generating any more copies.
|
||||
fields = append(fields, fields[len(fields)-1])
|
||||
}
|
||||
continue
|
||||
}
|
||||
// Record new anonymous struct to explore in next round.
|
||||
nextCount[ft]++
|
||||
if nextCount[ft] == 1 {
|
||||
next = append(next, field{name: ft.Name(), index: index, typ: ft})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sort.Slice(fields, func(i, j int) bool {
|
||||
x := fields
|
||||
// sort field by name, breaking ties with depth, then
|
||||
// breaking ties with "name came from boltease tag", then
|
||||
// breaking ties with index sequence
|
||||
if x[i].name != x[j].name {
|
||||
return x[i].name < x[j].name
|
||||
}
|
||||
if len(x[i].index) != len(x[j].index) {
|
||||
return len(x[i].index) < len(x[j].index)
|
||||
}
|
||||
if x[i].tag != x[j].tag {
|
||||
return x[i].tag
|
||||
}
|
||||
return byIndex(x).Less(i, j)
|
||||
})
|
||||
|
||||
// Delete all fields that are hidden by the Go rules for embedded fields,
|
||||
// except that fields with boltease tags are promoted.
|
||||
//
|
||||
// The fields are sorted in primary order of name, secondary order
|
||||
// of field index length. Loop over names; for each name, delete
|
||||
// hidden fields by choosing the one dominant field that survives.
|
||||
out := fields[:0]
|
||||
for advance, i := 0, 0; i < len(fields); i += advance {
|
||||
// One iteration per name.
|
||||
// Find the sequence of fields with th ename of this first field.
|
||||
fi := fields[i]
|
||||
name := fi.name
|
||||
for advance = 1; i+advance < len(fields); advance++ {
|
||||
fj := fields[i+advance]
|
||||
if fj.name != name {
|
||||
break
|
||||
}
|
||||
}
|
||||
if advance == 1 { // Only one field with this name
|
||||
out = append(out, fi)
|
||||
continue
|
||||
}
|
||||
dominant, ok := dominantField(fields[i : i+advance])
|
||||
if ok {
|
||||
out = append(out, dominant)
|
||||
}
|
||||
}
|
||||
|
||||
fields = out
|
||||
sort.Sort(byIndex(fields))
|
||||
|
||||
exactNameIndex := make(map[string]*field, len(fields))
|
||||
foldedNameIndex := make(map[string]*field, len(fields))
|
||||
for i, field := range fields {
|
||||
exactNameIndex[field.name] = &fields[i]
|
||||
// For historical reasons, first folded match takes precedence.
|
||||
if _, ok := foldedNameIndex[string(foldName(field.nameBytes))]; !ok {
|
||||
foldedNameIndex[string(foldName(field.nameBytes))] = &fields[i]
|
||||
}
|
||||
}
|
||||
return structFields{fields, exactNameIndex, foldedNameIndex}
|
||||
}
|
||||
|
||||
// dominantField looks through the fields, all of which are known to have the
|
||||
// same name, to find the single field that dominates the others using Go's
|
||||
// embedding rules, modified by the presence of boltease tags. if there are
|
||||
// multiple top-level fields, the boolean will be false: This condition is an
|
||||
// error in Go and we skip all fields.
|
||||
func dominantField(fields []field) (field, bool) {
|
||||
// The fields are sorted in increasing index-length order, then by presence of tag.
|
||||
// That means that the first field is the dominant one. We need only check
|
||||
// for error cases: two fields at top level, either both tagged or neither tagged.
|
||||
if len(fields) > 1 && len(fields[0].index) == len(fields[1].index) && fields[0].tag == fields[1].tag {
|
||||
return field{}, false
|
||||
}
|
||||
return fields[0], true
|
||||
}
|
||||
|
||||
var fieldCache sync.Map // map[reflect.Type]structFields
|
||||
|
||||
// cachedTypeFields is like typeFields but uses a cache to avoid repeated work.
|
||||
func cachedTypeFields(t reflect.Type) structFields {
|
||||
if f, ok := fieldCache.Load(t); ok {
|
||||
return f.(structFields)
|
||||
}
|
||||
f, _ := fieldCache.LoadOrStore(t, typeFields(t))
|
||||
return f.(structFields)
|
||||
}
|
251
bolteasable.go
Normal file
251
bolteasable.go
Normal file
@ -0,0 +1,251 @@
|
||||
package boltease
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
type any = interface{}
|
||||
|
||||
/*
|
||||
func (b *DB) Save(path []string, key string, src any) error {
|
||||
t := reflect.TypeOf(src)
|
||||
if t.Kind() == reflect.Pointer {
|
||||
// Save the actual struct
|
||||
elem := reflect.ValueOf(src).Elem()
|
||||
return b.Save(path, key, elem.Interface())
|
||||
}
|
||||
|
||||
if t.Kind() == reflect.Struct {
|
||||
fields := reflect.VisibleFields(t)
|
||||
r := reflect.ValueOf(src)
|
||||
for _, fld := range fields {
|
||||
f := r.FieldByName(fld.Name)
|
||||
if (f.Kind() == reflect.Struct || f.Kind() == reflect.Pointer) && f != src {
|
||||
if f.CanInterface() {
|
||||
err := b.Save(append(path, FieldName(fld)), f.Interface())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
err := b.Save(append(path, FieldName(fld)), reflect.Indirect(f))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if err := b.Set(path, FieldName(fld), f); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return b.Set(path[:len(path)-1], path[len(path)-1], src)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
*/
|
||||
|
||||
func (b *DB) LoadOld(path []string, dest any) error {
|
||||
destValue := reflect.ValueOf(dest)
|
||||
fmt.Println("Loading:", path, destValue)
|
||||
if destValue.Kind() != reflect.Pointer {
|
||||
return errors.New("Destination must be a pointer")
|
||||
}
|
||||
d := reflect.Indirect(destValue)
|
||||
fmt.Println(">> d.Kind() ->", d.Kind())
|
||||
if d.Kind() != reflect.Struct {
|
||||
if b.CanGetForInterface(d) {
|
||||
fmt.Println(">> Kind != Struct; GetForInterface")
|
||||
path, name := path[:len(path)-1], path[len(path)-1]
|
||||
return b.GetForInterface(path, name, dest)
|
||||
} else {
|
||||
fmt.Println(">> Reflect Indirect(d) Elem")
|
||||
dest = reflect.Indirect(d).Elem()
|
||||
fmt.Println(">> Dest set")
|
||||
}
|
||||
}
|
||||
|
||||
entityType := reflect.TypeOf(dest).Elem()
|
||||
fmt.Println(">> entityType.Kind() ->", entityType.Kind())
|
||||
|
||||
var ret error
|
||||
for i := 0; i < entityType.NumField(); i++ {
|
||||
structFld := entityType.Field(i)
|
||||
fldName := FieldName(structFld)
|
||||
fmt.Println("Loading Field:", fldName, "->", structFld.Type.Kind())
|
||||
switch structFld.Type.Kind() {
|
||||
case reflect.Pointer, reflect.Struct:
|
||||
fmt.Println("Getting Pointer or Struct")
|
||||
var wrk interface{}
|
||||
var wrkV reflect.Value
|
||||
if structFld.Type.Kind() == reflect.Pointer {
|
||||
fmt.Println(" It's a pointer to something")
|
||||
wrkV = reflect.New(structFld.Type).Elem()
|
||||
} else {
|
||||
fmt.Println(" It's a struct itself")
|
||||
wrkV = reflect.New(reflect.TypeOf(structFld))
|
||||
}
|
||||
wrk = wrkV.Interface()
|
||||
fmt.Println("-> Recurse (struct)", reflect.TypeOf(wrk))
|
||||
return errors.New("Recursive Loading not Implemented")
|
||||
err := b.Load(append(path, fldName), &wrk)
|
||||
if err != nil {
|
||||
fmt.Println("-> -> Error loading.", err.Error())
|
||||
if ret == nil {
|
||||
ret = err
|
||||
}
|
||||
} else {
|
||||
// Set the value
|
||||
reflect.ValueOf(dest).Elem().FieldByName(structFld.Name).Set(reflect.ValueOf(wrk))
|
||||
}
|
||||
|
||||
case reflect.String:
|
||||
fmt.Println("Getting String")
|
||||
var wrk string
|
||||
err := b.GetForInterface(path, fldName, &wrk)
|
||||
if err != nil {
|
||||
if ret == nil {
|
||||
ret = err
|
||||
}
|
||||
} else {
|
||||
// Set the value
|
||||
reflect.ValueOf(dest).Elem().FieldByName(structFld.Name).Set(reflect.ValueOf(wrk))
|
||||
}
|
||||
|
||||
case reflect.Bool:
|
||||
fmt.Println("Getting Boolean")
|
||||
var wrk bool
|
||||
err := b.GetForInterface(path, fldName, &wrk)
|
||||
if err != nil {
|
||||
if ret == nil {
|
||||
ret = err
|
||||
}
|
||||
} else {
|
||||
// Set the value
|
||||
reflect.ValueOf(dest).Elem().FieldByName(structFld.Name).Set(reflect.ValueOf(wrk))
|
||||
}
|
||||
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
fmt.Println("Getting Integer")
|
||||
var wrk int
|
||||
err := b.GetForInterface(path, fldName, &wrk)
|
||||
if err != nil {
|
||||
if ret == nil {
|
||||
ret = err
|
||||
}
|
||||
} else {
|
||||
// Set the value
|
||||
reflect.ValueOf(dest).Elem().FieldByName(structFld.Name).Set(reflect.ValueOf(wrk))
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
setField := reflect.ValueOf(dest).Elem().Field(i)
|
||||
value := entityType.Field(i)
|
||||
fldName := FieldName(value)
|
||||
if value.Type.Kind() == reflect.Struct {
|
||||
b.LoadStruct(append(path, fldName), setField.Interface())
|
||||
} else {
|
||||
err := b.GetForInterface(path, fldName, &value)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
fmt.Println("value:", value)
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
if value.Type.Kind() == reflect.Struct {
|
||||
b.LoadStruct(append(path, fldName), setField.Interface())
|
||||
} else {
|
||||
v, err := b.GetForType(path, fldName, value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
setField.Set(reflect.ValueOf(v))
|
||||
}
|
||||
*/
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func (b *DB) loadObject(path []string, v reflect.Value) error {
|
||||
//var fields structFields
|
||||
//switch v.Kind() {
|
||||
//case reflect.Struct:
|
||||
// fields = cachedTypeFields(t)
|
||||
//}
|
||||
//return nil
|
||||
return errors.New("Load Object not Implemented")
|
||||
}
|
||||
|
||||
func FieldName(fld reflect.StructField) string {
|
||||
nm := fld.Name
|
||||
tag := fld.Tag.Get("boltease")
|
||||
if tag != "" {
|
||||
nm = tag
|
||||
}
|
||||
return nm
|
||||
}
|
||||
|
||||
func FieldIgnored(fld reflect.StructField) bool {
|
||||
return fld.Tag.Get("boltease") == "-"
|
||||
}
|
||||
|
||||
func ReflectValueToInterface(val reflect.Value) interface{} {
|
||||
switch val.Kind() {
|
||||
case reflect.Pointer:
|
||||
return ReflectValueToInterface(reflect.Indirect(val))
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
return val.Int()
|
||||
case reflect.Bool:
|
||||
return val.Bool()
|
||||
case reflect.String:
|
||||
return val.String()
|
||||
default:
|
||||
return val.Bytes()
|
||||
}
|
||||
}
|
||||
|
||||
// foldName returns a folded string such that foldName(x) == foldName(y)
|
||||
// is identical to bytes.EqualFold(x, y).
|
||||
func foldName(in []byte) []byte {
|
||||
// This is inlinable to take advantage of "function outlining".
|
||||
var arr [32768]byte // The max size of a bolt key
|
||||
return appendFoldedName(arr[:0], in)
|
||||
}
|
||||
|
||||
func appendFoldedName(out, in []byte) []byte {
|
||||
for i := 0; i < len(in); {
|
||||
// Handle single-byte ASCII.
|
||||
if c := in[i]; c < utf8.RuneSelf {
|
||||
if 'a' <= c && c <= 'z' {
|
||||
c -= 'a' - 'A'
|
||||
}
|
||||
out = append(out, c)
|
||||
i++
|
||||
continue
|
||||
}
|
||||
// Handle multi-byt eUnicode.
|
||||
r, n := utf8.DecodeRune(in[i:])
|
||||
out = utf8.AppendRune(out, foldRune(r))
|
||||
i += n
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// foldRune returns the smallest rune for all runes in the same fold set.
|
||||
func foldRune(r rune) rune {
|
||||
for {
|
||||
r2 := unicode.SimpleFold(r)
|
||||
if r2 <= r {
|
||||
return r2
|
||||
}
|
||||
r = r2
|
||||
}
|
||||
}
|
282
boltease.go
282
boltease.go
@ -2,9 +2,9 @@ package boltease
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@ -35,92 +35,6 @@ func Create(fn string, m os.FileMode, opts *bolt.Options) (*DB, error) {
|
||||
return &b, nil
|
||||
}
|
||||
|
||||
// SetStruct takes a database, a path, a struct, and a function that takes a
|
||||
// path and an element and processes it.
|
||||
func SetStruct[T any](b *DB, path []string, value T, fn func([]string, T) error) error {
|
||||
var err error
|
||||
if !b.dbIsOpen {
|
||||
if err = b.OpenDB(); err != nil {
|
||||
return err
|
||||
}
|
||||
defer b.CloseDB()
|
||||
}
|
||||
|
||||
err = b.MkBucketPath(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = b.boltDB.Update(func(tx *bolt.Tx) error {
|
||||
bkt := tx.Bucket([]byte(path[0]))
|
||||
if bkt == nil {
|
||||
return fmt.Errorf("couldn't find bucket " + path[0])
|
||||
}
|
||||
for idx := 1; idx < len(path); idx++ {
|
||||
bkt, err = bkt.CreateBucketIfNotExists([]byte(path[idx]))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// bkt should have the last bucket in the path
|
||||
return fn(path, value)
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
// SetStructList takes a path, a slice of structs, and a function that takes
|
||||
// a bolt bucket and one element of the slice and processes it.
|
||||
func SetStructList[T any](b *DB, path []string, values []T, fn func(*bolt.Bucket, T) error) error {
|
||||
var err error
|
||||
if !b.dbIsOpen {
|
||||
if err = b.OpenDB(); err != nil {
|
||||
return err
|
||||
}
|
||||
defer b.CloseDB()
|
||||
}
|
||||
|
||||
err = b.MkBucketPath(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = b.boltDB.Update(func(tx *bolt.Tx) error {
|
||||
bkt := tx.Bucket([]byte(path[0]))
|
||||
if bkt == nil {
|
||||
return fmt.Errorf("couldn't find bucket " + path[0])
|
||||
}
|
||||
for idx := 1; idx < len(path); idx++ {
|
||||
bkt, err = bkt.CreateBucketIfNotExists([]byte(path[idx]))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// bkt should have the last bucket in the path
|
||||
// We need to get the sequence number, if it's greater than the length of
|
||||
// 'values', we're going to have to delete values.
|
||||
seq := bkt.Sequence()
|
||||
bkt.SetSequence(0)
|
||||
for _, v := range values {
|
||||
id, _ := bkt.NextSequence()
|
||||
bId := make([]byte, 8)
|
||||
binary.BigEndian.PutUint64(bId, id)
|
||||
structBkt, err := bkt.CreateBucketIfNotExists(bId)
|
||||
if err = fn(structBkt, v); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
currSeq := bkt.Sequence()
|
||||
for ; seq < currSeq; seq++ {
|
||||
bId := make([]byte, 8)
|
||||
binary.BigEndian.PutUint64(bId, seq)
|
||||
err = bkt.Delete(bId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (b *DB) OpenDB() error {
|
||||
if b.dbIsOpen {
|
||||
// DB is already open, that's fine.
|
||||
@ -189,16 +103,98 @@ func (b *DB) MkBucketPath(path []string) error {
|
||||
|
||||
func (b *DB) Set(path []string, key string, val interface{}) error {
|
||||
switch v := val.(type) {
|
||||
case reflect.Value:
|
||||
return b.Set(path, key, ReflectValueToInterface(v))
|
||||
case string:
|
||||
return b.SetString(path, key, v)
|
||||
case int:
|
||||
return b.SetInt(path, key, v)
|
||||
case int8:
|
||||
return b.SetInt(path, key, int(v))
|
||||
case int16:
|
||||
return b.SetInt(path, key, int(v))
|
||||
case int32:
|
||||
return b.SetInt(path, key, int(v))
|
||||
case int64:
|
||||
return b.SetInt(path, key, int(v))
|
||||
case bool:
|
||||
return b.SetBool(path, key, v)
|
||||
case []byte:
|
||||
return b.SetBytes(path, key, v)
|
||||
default:
|
||||
return fmt.Errorf("Set: Unknown Data Type: %v", v)
|
||||
}
|
||||
return errors.New("unknown data type")
|
||||
}
|
||||
|
||||
func (b *DB) CanGetForInterface(val interface{}) bool {
|
||||
switch val.(type) {
|
||||
case *string, *int, *int8, *int16, *int32, *int64, *bool, *[]byte:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (b *DB) GetForInterface(path []string, key string, val interface{}) error {
|
||||
var err error
|
||||
switch v := val.(type) {
|
||||
case *string:
|
||||
*v, err = b.GetString(path, key)
|
||||
case *int:
|
||||
*v, err = b.GetInt(path, key)
|
||||
case *int8:
|
||||
var wrk int
|
||||
wrk, err = b.GetInt(path, key)
|
||||
*v = int8(wrk)
|
||||
case *int16:
|
||||
var wrk int
|
||||
wrk, err = b.GetInt(path, key)
|
||||
*v = int16(wrk)
|
||||
case *int32:
|
||||
var wrk int
|
||||
wrk, err = b.GetInt(path, key)
|
||||
*v = int32(wrk)
|
||||
case *int64:
|
||||
var wrk int
|
||||
wrk, err = b.GetInt(path, key)
|
||||
*v = int64(wrk)
|
||||
case *bool:
|
||||
*v, err = b.GetBool(path, key)
|
||||
case *[]byte:
|
||||
*v, err = b.GetBytes(path, key)
|
||||
default:
|
||||
return fmt.Errorf("GetForInterface: Unknown Data Type: %v", v)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (b *DB) LoadInto(path []string, key string, into interface{}) error {
|
||||
var err error
|
||||
switch v := into.(type) {
|
||||
case string:
|
||||
i := new(string)
|
||||
*i, err = b.GetString(path, key)
|
||||
fmt.Printf("Loading String: %v\n", i)
|
||||
into = i
|
||||
case int, int8, int16, int32, int64:
|
||||
i := new(int)
|
||||
*i, err = b.GetInt(path, key)
|
||||
fmt.Printf("Loading Int: %v\n", i)
|
||||
into = i
|
||||
case bool:
|
||||
i := new(bool)
|
||||
*i, err = b.GetBool(path, key)
|
||||
fmt.Printf("Loading Bool: %v\n", i)
|
||||
into = i
|
||||
case []byte:
|
||||
i := new([]byte)
|
||||
*i, err = b.GetBytes(path, key)
|
||||
fmt.Printf("Loading []byte: %v\n", i)
|
||||
into = i
|
||||
default:
|
||||
return fmt.Errorf("Unknown Data Type: %v", v)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (b *DB) GetBytes(path []string, key string) ([]byte, error) {
|
||||
@ -213,18 +209,18 @@ func (b *DB) GetBytes(path []string, key string) ([]byte, error) {
|
||||
err = b.boltDB.View(func(tx *bolt.Tx) error {
|
||||
bkt := tx.Bucket([]byte(path[0]))
|
||||
if bkt == nil {
|
||||
return fmt.Errorf("couldn't find bucket " + path[0])
|
||||
return fmt.Errorf("Couldn't find bucket " + path[0])
|
||||
}
|
||||
for idx := 1; idx < len(path); idx++ {
|
||||
bkt = bkt.Bucket([]byte(path[idx]))
|
||||
if bkt == nil {
|
||||
return fmt.Errorf("couldn't find bucket " + strings.Join(path[:idx+1], "/"))
|
||||
return fmt.Errorf("Couldn't find bucket " + strings.Join(path[:idx+1], "/"))
|
||||
}
|
||||
}
|
||||
// newBkt should have the last bucket in the path
|
||||
val := bkt.Get([]byte(key))
|
||||
if val == nil {
|
||||
return fmt.Errorf("couldn't find value")
|
||||
return fmt.Errorf("Couldn't find value")
|
||||
}
|
||||
ret = make([]byte, len(val))
|
||||
copy(ret, val)
|
||||
@ -236,7 +232,7 @@ func (b *DB) GetBytes(path []string, key string) ([]byte, error) {
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (b *DB) SetBytes(path []string, key string, val []byte) error {
|
||||
func (b *DB) SetBBytes(path []string, key []byte, val []byte) error {
|
||||
var err error
|
||||
if !b.dbIsOpen {
|
||||
if err = b.OpenDB(); err != nil {
|
||||
@ -252,7 +248,7 @@ func (b *DB) SetBytes(path []string, key string, val []byte) error {
|
||||
err = b.boltDB.Update(func(tx *bolt.Tx) error {
|
||||
bkt := tx.Bucket([]byte(path[0]))
|
||||
if bkt == nil {
|
||||
return fmt.Errorf("couldn't find bucket " + path[0])
|
||||
return fmt.Errorf("Couldn't find bucket " + path[0])
|
||||
}
|
||||
for idx := 1; idx < len(path); idx++ {
|
||||
bkt, err = bkt.CreateBucketIfNotExists([]byte(path[idx]))
|
||||
@ -261,11 +257,15 @@ func (b *DB) SetBytes(path []string, key string, val []byte) error {
|
||||
}
|
||||
}
|
||||
// bkt should have the last bucket in the path
|
||||
return bkt.Put([]byte(key), val)
|
||||
return bkt.Put(key, val)
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (b *DB) SetBytes(path []string, key string, val []byte) error {
|
||||
return b.SetBBytes(path, []byte(key), val)
|
||||
}
|
||||
|
||||
// GetString returns the value at path
|
||||
// path is a slice of strings
|
||||
// key is the key to get
|
||||
@ -281,15 +281,15 @@ func (b *DB) GetString(path []string, key string) (string, error) {
|
||||
err = b.boltDB.View(func(tx *bolt.Tx) error {
|
||||
bkt := tx.Bucket([]byte(path[0]))
|
||||
if bkt == nil {
|
||||
return fmt.Errorf("couldn't find bucket " + path[0])
|
||||
return fmt.Errorf("Couldn't find bucket " + path[0])
|
||||
}
|
||||
for idx := 1; idx < len(path); idx++ {
|
||||
bkt = bkt.Bucket([]byte(path[idx]))
|
||||
if bkt == nil {
|
||||
return fmt.Errorf("couldn't find bucket " + strings.Join(path[:idx+1], "/"))
|
||||
return fmt.Errorf("Couldn't find bucket " + strings.Join(path[:idx+1], "/"))
|
||||
}
|
||||
}
|
||||
// bkt should have the last bucket in the path
|
||||
// newBkt should have the last bucket in the path
|
||||
ret = string(bkt.Get([]byte(key)))
|
||||
return nil
|
||||
})
|
||||
@ -314,7 +314,7 @@ func (b *DB) SetString(path []string, key, val string) error {
|
||||
err = b.boltDB.Update(func(tx *bolt.Tx) error {
|
||||
bkt := tx.Bucket([]byte(path[0]))
|
||||
if bkt == nil {
|
||||
return fmt.Errorf("couldn't find bucket " + path[0])
|
||||
return fmt.Errorf("Couldn't find bucket " + path[0])
|
||||
}
|
||||
for idx := 1; idx < len(path); idx++ {
|
||||
bkt, err = bkt.CreateBucketIfNotExists([]byte(path[idx]))
|
||||
@ -323,7 +323,7 @@ func (b *DB) SetString(path []string, key, val string) error {
|
||||
}
|
||||
}
|
||||
// bkt should have the last bucket in the path
|
||||
return SetString(bkt, key, val)
|
||||
return bkt.Put([]byte(key), []byte(val))
|
||||
})
|
||||
return err
|
||||
}
|
||||
@ -354,7 +354,7 @@ func (b *DB) GetBool(path []string, key string) (bool, error) {
|
||||
if r == "true" || r == "1" {
|
||||
ret = true
|
||||
} else if r != "false" && r != "0" {
|
||||
err = fmt.Errorf("cannot parse as a boolean")
|
||||
err = fmt.Errorf("Cannot parse as a boolean")
|
||||
}
|
||||
}
|
||||
return ret, err
|
||||
@ -397,14 +397,14 @@ func (b *DB) GetBucketList(path []string) ([]string, error) {
|
||||
err = b.boltDB.Update(func(tx *bolt.Tx) error {
|
||||
bkt := tx.Bucket([]byte(path[0]))
|
||||
if bkt == nil {
|
||||
return fmt.Errorf("couldn't find bucket " + path[0])
|
||||
return fmt.Errorf("Couldn't find bucket " + path[0])
|
||||
}
|
||||
var berr error
|
||||
if len(path) > 1 {
|
||||
for idx := 1; idx < len(path); idx++ {
|
||||
bkt = bkt.Bucket([]byte(path[idx]))
|
||||
if bkt == nil {
|
||||
return fmt.Errorf("couldn't find bucket " + strings.Join(path[:idx+1], " / "))
|
||||
return fmt.Errorf("Couldn't find bucket " + strings.Join(path[:idx+1], " / "))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -436,14 +436,14 @@ func (b *DB) GetKeyList(path []string) ([]string, error) {
|
||||
err = b.boltDB.Update(func(tx *bolt.Tx) error {
|
||||
bkt := tx.Bucket([]byte(path[0]))
|
||||
if bkt == nil {
|
||||
return fmt.Errorf("couldn't find bucket " + path[0])
|
||||
return fmt.Errorf("Couldn't find bucket " + path[0])
|
||||
}
|
||||
var berr error
|
||||
if len(path) > 1 {
|
||||
for idx := 1; idx < len(path); idx++ {
|
||||
bkt = bkt.Bucket([]byte(path[idx]))
|
||||
if bkt == nil {
|
||||
return fmt.Errorf("couldn't find bucket " + strings.Join(path[:idx], " / "))
|
||||
return fmt.Errorf("Couldn't find bucket " + strings.Join(path[:idx], " / "))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -474,14 +474,14 @@ func (b *DB) DeletePair(path []string, key string) error {
|
||||
err = b.boltDB.Update(func(tx *bolt.Tx) error {
|
||||
bkt := tx.Bucket([]byte(path[0]))
|
||||
if bkt == nil {
|
||||
return fmt.Errorf("couldn't find bucket " + path[0])
|
||||
return fmt.Errorf("Couldn't find bucket " + path[0])
|
||||
}
|
||||
if len(path) > 1 {
|
||||
var newBkt *bolt.Bucket
|
||||
for idx := 1; idx < len(path); idx++ {
|
||||
newBkt = bkt.Bucket([]byte(path[idx]))
|
||||
if newBkt == nil {
|
||||
return fmt.Errorf("couldn't find bucket " + strings.Join(path[:idx], "/"))
|
||||
return fmt.Errorf("Couldn't find bucket " + strings.Join(path[:idx], "/"))
|
||||
}
|
||||
}
|
||||
bkt = newBkt
|
||||
@ -509,12 +509,12 @@ func (b *DB) DeleteBucket(path []string, key string) error {
|
||||
err = b.boltDB.Update(func(tx *bolt.Tx) error {
|
||||
bkt := tx.Bucket([]byte(path[0]))
|
||||
if bkt == nil {
|
||||
return fmt.Errorf("couldn't find bucket " + path[0])
|
||||
return fmt.Errorf("Couldn't find bucket " + path[0])
|
||||
}
|
||||
for idx := 1; idx < len(path); idx++ {
|
||||
bkt = bkt.Bucket([]byte(path[idx]))
|
||||
if bkt == nil {
|
||||
return fmt.Errorf("couldn't find bucket " + strings.Join(path[:idx], "/"))
|
||||
return fmt.Errorf("Couldn't find bucket " + strings.Join(path[:idx], "/"))
|
||||
}
|
||||
}
|
||||
// bkt should have the last bucket in the path
|
||||
@ -541,14 +541,14 @@ func (b *DB) GetStringList(path []string) ([]string, error) {
|
||||
err = b.boltDB.Update(func(tx *bolt.Tx) error {
|
||||
bkt := tx.Bucket([]byte(path[0]))
|
||||
if bkt == nil {
|
||||
return fmt.Errorf("couldn't find bucket " + path[0])
|
||||
return fmt.Errorf("Couldn't find bucket " + path[0])
|
||||
}
|
||||
var berr error
|
||||
if len(path) > 1 {
|
||||
for idx := 1; idx < len(path); idx++ {
|
||||
bkt = bkt.Bucket([]byte(path[idx]))
|
||||
if bkt == nil {
|
||||
return fmt.Errorf("couldn't find bucket " + strings.Join(path[:idx+1], " / "))
|
||||
return fmt.Errorf("Couldn't find bucket " + strings.Join(path[:idx+1], " / "))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -583,61 +583,7 @@ func (b *DB) SetStringList(path, values []string) error {
|
||||
err = b.boltDB.Update(func(tx *bolt.Tx) error {
|
||||
bkt := tx.Bucket([]byte(path[0]))
|
||||
if bkt == nil {
|
||||
return fmt.Errorf("couldn't find bucket " + path[0])
|
||||
}
|
||||
for idx := 1; idx < len(path); idx++ {
|
||||
bkt, err = bkt.CreateBucketIfNotExists([]byte(path[idx]))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// bkt should have the last bucket in the path
|
||||
// We need to get the sequence number, if it's greater than the length
|
||||
// of 'values', we're going to have to delete values.
|
||||
seq := bkt.Sequence()
|
||||
bkt.SetSequence(0)
|
||||
for _, v := range values {
|
||||
id, _ := bkt.NextSequence()
|
||||
bId := make([]byte, 8)
|
||||
binary.BigEndian.PutUint64(bId, id)
|
||||
err = bkt.Put(bId, []byte(v))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
currSeq := bkt.Sequence()
|
||||
for ; seq < currSeq; seq++ {
|
||||
bId := make([]byte, 8)
|
||||
binary.BigEndian.PutUint64(bId, seq)
|
||||
err = bkt.Delete(bId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
// AddToStringList adds strings to a bucket of string slices
|
||||
func (b *DB) AddToStringList(path []string, values ...string) error {
|
||||
var err error
|
||||
if !b.dbIsOpen {
|
||||
if err = b.OpenDB(); err != nil {
|
||||
return err
|
||||
}
|
||||
defer b.CloseDB()
|
||||
}
|
||||
|
||||
err = b.MkBucketPath(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = b.boltDB.Update(func(tx *bolt.Tx) error {
|
||||
bkt := tx.Bucket([]byte(path[0]))
|
||||
if bkt == nil {
|
||||
return fmt.Errorf("couldn't find bucket " + path[0])
|
||||
return fmt.Errorf("Couldn't find bucket " + path[0])
|
||||
}
|
||||
for idx := 1; idx < len(path); idx++ {
|
||||
bkt, err = bkt.CreateBucketIfNotExists([]byte(path[idx]))
|
||||
@ -674,14 +620,14 @@ func (b *DB) GetKeyValueMap(path []string) (map[string][]byte, error) {
|
||||
err = b.boltDB.Update(func(tx *bolt.Tx) error {
|
||||
bkt := tx.Bucket([]byte(path[0]))
|
||||
if bkt == nil {
|
||||
return fmt.Errorf("couldn't find bucket " + path[0])
|
||||
return fmt.Errorf("Couldn't find bucket " + path[0])
|
||||
}
|
||||
var berr error
|
||||
if len(path) > 1 {
|
||||
for idx := 1; idx < len(path); idx++ {
|
||||
bkt = bkt.Bucket([]byte(path[idx]))
|
||||
if bkt == nil {
|
||||
return fmt.Errorf("couldn't find bucket " + strings.Join(path[:idx], " / "))
|
||||
return fmt.Errorf("Couldn't find bucket " + strings.Join(path[:idx], " / "))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -729,7 +675,7 @@ func (b *DB) SetKeyValueMap(path []string, kvMap map[string][]byte) error {
|
||||
err = b.boltDB.Update(func(tx *bolt.Tx) error {
|
||||
bkt := tx.Bucket([]byte(path[0]))
|
||||
if bkt == nil {
|
||||
return fmt.Errorf("couldn't find bucket " + path[0])
|
||||
return fmt.Errorf("Couldn't find bucket " + path[0])
|
||||
}
|
||||
for idx := 1; idx < len(path); idx++ {
|
||||
bkt, err = bkt.CreateBucketIfNotExists([]byte(path[idx]))
|
||||
|
323
bucket_funcs.go
323
bucket_funcs.go
@ -1,323 +0,0 @@
|
||||
package boltease
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/boltdb/bolt"
|
||||
)
|
||||
|
||||
func DoUpdate(b *DB, path []string, fn func(bkt *bolt.Bucket) error) error {
|
||||
var err error
|
||||
if !b.dbIsOpen {
|
||||
if err = b.OpenDB(); err != nil {
|
||||
return err
|
||||
}
|
||||
defer b.CloseDB()
|
||||
}
|
||||
|
||||
err = b.MkBucketPath(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = b.boltDB.Update(func(tx *bolt.Tx) error {
|
||||
bkt := tx.Bucket([]byte(path[0]))
|
||||
if bkt == nil {
|
||||
return fmt.Errorf("couldn't find bucket " + path[0])
|
||||
}
|
||||
for idx := 1; idx < len(path); idx++ {
|
||||
bkt, err = bkt.CreateBucketIfNotExists([]byte(path[idx]))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// bkt should have the last bucket in the path
|
||||
return fn(bkt)
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func DoRead(b *DB, path []string, fn func(bkt *bolt.Bucket) error) error {
|
||||
var err error
|
||||
if !b.dbIsOpen {
|
||||
if err = b.OpenDB(); err != nil {
|
||||
return err
|
||||
}
|
||||
defer b.CloseDB()
|
||||
}
|
||||
err = b.boltDB.View(func(tx *bolt.Tx) error {
|
||||
bkt := tx.Bucket([]byte(path[0]))
|
||||
if bkt == nil {
|
||||
return fmt.Errorf("couldn't find bucket " + path[0])
|
||||
}
|
||||
for idx := 1; idx < len(path); idx++ {
|
||||
bkt = bkt.Bucket([]byte(path[idx]))
|
||||
if bkt == nil {
|
||||
return fmt.Errorf("couldn't find bucket " + strings.Join(path[:idx+1], "/"))
|
||||
}
|
||||
}
|
||||
// bkt should have the last bucket in the path
|
||||
return fn(bkt)
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
// These bucket functions are meant to be run inside of a boltdb
|
||||
// transaction (Update/View)
|
||||
// GetBucket must be run inside of an Update transaction
|
||||
// Otherwise, any Get* function can be in either.
|
||||
// And any Set* function needs to be in an Update.
|
||||
func GetBucket(bkt *bolt.Bucket, path ...string) (*bolt.Bucket, error) {
|
||||
if bkt == nil {
|
||||
return nil, fmt.Errorf("bucket must not be nil")
|
||||
}
|
||||
var ret *bolt.Bucket
|
||||
var err error
|
||||
ret, err = bkt.CreateBucketIfNotExists([]byte(path[0]))
|
||||
if len(path) > 1 {
|
||||
for idx := 1; idx < len(path); idx++ {
|
||||
ret, err = bkt.CreateBucketIfNotExists([]byte(path[idx]))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func GetString(bkt *bolt.Bucket, key string) (string, error) {
|
||||
if bkt == nil {
|
||||
return "", fmt.Errorf("bucket must not be nil")
|
||||
}
|
||||
return string(bkt.Get([]byte(key))), nil
|
||||
}
|
||||
|
||||
func SetString(bkt *bolt.Bucket, key, val string) error {
|
||||
if bkt == nil {
|
||||
return fmt.Errorf("bucket must not be nil")
|
||||
}
|
||||
return bkt.Put([]byte(key), []byte(val))
|
||||
}
|
||||
|
||||
func GetInt(bkt *bolt.Bucket, key string) (int, error) {
|
||||
if bkt == nil {
|
||||
return 0, fmt.Errorf("bucket must not be nil")
|
||||
}
|
||||
r, err := GetString(bkt, key)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return strconv.Atoi(r)
|
||||
}
|
||||
|
||||
func SetInt(bkt *bolt.Bucket, key string, val int) error {
|
||||
if bkt == nil {
|
||||
return fmt.Errorf("bucket must not be nil")
|
||||
}
|
||||
return SetString(bkt, key, strconv.Itoa(val))
|
||||
}
|
||||
|
||||
func GetBool(bkt *bolt.Bucket, key string) (bool, error) {
|
||||
r, err := GetString(bkt, key)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if r == "true" || r == "1" {
|
||||
return true, nil
|
||||
} else if r == "false" || r == "0" {
|
||||
return false, nil
|
||||
} else {
|
||||
return false, fmt.Errorf("cannot parse as a boolean")
|
||||
}
|
||||
}
|
||||
|
||||
func SetBool(bkt *bolt.Bucket, key string, val bool) error {
|
||||
if bkt == nil {
|
||||
return fmt.Errorf("bucket must not be nil")
|
||||
}
|
||||
if val {
|
||||
return SetString(bkt, key, "true")
|
||||
}
|
||||
return SetString(bkt, key, "false")
|
||||
}
|
||||
|
||||
func GetTimestamp(bkt *bolt.Bucket, key string) (time.Time, error) {
|
||||
r, err := GetString(bkt, key)
|
||||
if err != nil {
|
||||
return time.Unix(0, 0), err
|
||||
}
|
||||
return time.Parse(time.RFC3339, r)
|
||||
}
|
||||
|
||||
func SetTimestamp(bkt *bolt.Bucket, key string, val time.Time) error {
|
||||
return SetString(bkt, key, val.Format(time.RFC3339))
|
||||
}
|
||||
|
||||
func GetBucketList(bkt *bolt.Bucket) ([]string, error) {
|
||||
var ret []string
|
||||
if bkt == nil {
|
||||
return ret, fmt.Errorf("bucket must not be nil")
|
||||
}
|
||||
bkt.ForEach(func(k, v []byte) error {
|
||||
if v == nil {
|
||||
// Must be a bucket
|
||||
ret = append(ret, string(k))
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func GetKeyList(bkt *bolt.Bucket) ([]string, error) {
|
||||
var ret []string
|
||||
if bkt == nil {
|
||||
return ret, fmt.Errorf("bucket must not be nil")
|
||||
}
|
||||
bkt.ForEach(func(k, v []byte) error {
|
||||
if v != nil {
|
||||
ret = append(ret, string(k))
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func DeletePair(bkt *bolt.Bucket, key string) error {
|
||||
if bkt == nil {
|
||||
return fmt.Errorf("bucket must not be nil")
|
||||
}
|
||||
// Make sure that the key belongs to a pair
|
||||
if tst := bkt.Bucket([]byte(key)); tst != nil {
|
||||
return fmt.Errorf("key is a bucket")
|
||||
}
|
||||
return bkt.Delete([]byte(key))
|
||||
}
|
||||
|
||||
func DeleteBucket(bkt *bolt.Bucket, key string) error {
|
||||
if bkt == nil {
|
||||
return fmt.Errorf("bucket must not be nil")
|
||||
}
|
||||
// Make sure that the key belongs to a pair
|
||||
if tst := bkt.Bucket([]byte(key)); tst == nil {
|
||||
return fmt.Errorf("key is not a bucket")
|
||||
}
|
||||
return bkt.DeleteBucket([]byte(key))
|
||||
}
|
||||
|
||||
func GetStringList(bkt *bolt.Bucket) ([]string, error) {
|
||||
var ret []string
|
||||
if bkt == nil {
|
||||
return ret, fmt.Errorf("bucket must not be nil")
|
||||
}
|
||||
bkt.ForEach(func(k, v []byte) error {
|
||||
if v != nil {
|
||||
// Must be a key
|
||||
ret = append(ret, string(v))
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func SetStringList(bkt *bolt.Bucket, val []string) error {
|
||||
if bkt == nil {
|
||||
return fmt.Errorf("bucket must not be nil")
|
||||
}
|
||||
genseq := func(id uint64) []byte {
|
||||
bId := make([]byte, 8)
|
||||
binary.BigEndian.PutUint64(bId, id)
|
||||
return bId
|
||||
}
|
||||
var err error
|
||||
// We need to get the sequence number, if it's greater than the length of
|
||||
// val, we're going to have to delete values
|
||||
seq := bkt.Sequence()
|
||||
bkt.SetSequence(0)
|
||||
for _, v := range val {
|
||||
id, _ := bkt.NextSequence()
|
||||
if err = bkt.Put(genseq(id), []byte(v)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
currSeq := bkt.Sequence()
|
||||
for ; seq < currSeq; seq++ {
|
||||
err = bkt.Delete(genseq(seq))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func AddToStringList(bkt *bolt.Bucket, key string, values ...string) error {
|
||||
if bkt == nil {
|
||||
return fmt.Errorf("bucket must not be nil")
|
||||
}
|
||||
genseq := func(id uint64) []byte {
|
||||
bId := make([]byte, 8)
|
||||
binary.BigEndian.PutUint64(bId, id)
|
||||
return bId
|
||||
}
|
||||
var err error
|
||||
for _, v := range values {
|
||||
id, _ := bkt.NextSequence()
|
||||
if err = bkt.Put(genseq(id), []byte(v)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetKeyValueMap(bkt *bolt.Bucket) (map[string][]byte, error) {
|
||||
ret := make(map[string][]byte)
|
||||
if bkt == nil {
|
||||
return ret, fmt.Errorf("bucket must not be nil")
|
||||
}
|
||||
bkt.ForEach(func(k, v []byte) error {
|
||||
if v != nil {
|
||||
ret[string(k)] = v
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func SetKeyValueMap(bkt *bolt.Bucket, kvMap map[string][]byte) error {
|
||||
if bkt == nil {
|
||||
return fmt.Errorf("bucket must not be nil")
|
||||
}
|
||||
var err error
|
||||
for k, v := range kvMap {
|
||||
err = bkt.Put([]byte(k), v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetKeyValueStringMap(bkt *bolt.Bucket) (map[string]string, error) {
|
||||
ret := make(map[string]string)
|
||||
if bkt == nil {
|
||||
return ret, fmt.Errorf("bucket must not be nil")
|
||||
}
|
||||
btMap, err := GetKeyValueMap(bkt)
|
||||
if err != nil {
|
||||
return ret, err
|
||||
}
|
||||
for k, v := range btMap {
|
||||
ret[k] = string(v)
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func SetKeyValueStringMap(bkt *bolt.Bucket, kvMap map[string]string) error {
|
||||
chain := make(map[string][]byte)
|
||||
for k, v := range kvMap {
|
||||
chain[k] = []byte(v)
|
||||
}
|
||||
return SetKeyValueMap(bkt, chain)
|
||||
}
|
2
example/.gitignore
vendored
Normal file
2
example/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
example
|
||||
example.db
|
140
example/main.go
Normal file
140
example/main.go
Normal file
@ -0,0 +1,140 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"git.bullercodeworks.com/brian/boltease"
|
||||
)
|
||||
|
||||
func main() {
|
||||
example1()
|
||||
// fmt.Println()
|
||||
// example2()
|
||||
}
|
||||
|
||||
func example1() {
|
||||
fmt.Println("# Example 1")
|
||||
db, err := boltease.Create("example.db", 0o600, nil)
|
||||
if err != nil {
|
||||
fmt.Printf("Error Opening File: %s\n", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Println("## Saving Struct")
|
||||
err = db.Save(
|
||||
[]string{"examples"},
|
||||
"example1",
|
||||
ExampleType{
|
||||
Name: "Example 1",
|
||||
Age: 5,
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Printf("Error saving struct: %s\n", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
/*
|
||||
fmt.Println("## Example 1-1: Simple")
|
||||
var v string
|
||||
err = db.GetForInterface([]string{"examples", "example1"}, "name", &v)
|
||||
if err != nil {
|
||||
fmt.Println("Error:", err.Error())
|
||||
}
|
||||
fmt.Println("Name:", v)
|
||||
var age int
|
||||
err = db.GetForInterface([]string{"examples", "example1"}, "age", &age)
|
||||
if err != nil {
|
||||
fmt.Println("Error:", err.Error())
|
||||
}
|
||||
fmt.Println("Age:", age)
|
||||
fmt.Println("")
|
||||
|
||||
fmt.Println("## Example 1-2: LoadStruct, simple")
|
||||
var name string
|
||||
err = db.Load(
|
||||
[]string{"examples", "example1", "name"},
|
||||
&name,
|
||||
)
|
||||
fmt.Println("Name:", name)
|
||||
if err != nil {
|
||||
fmt.Println("Err:", err)
|
||||
}
|
||||
fmt.Println("")
|
||||
|
||||
fmt.Println("## Example 1-3: Struct")
|
||||
fmt.Println("Loading into Struct")
|
||||
newStruct := ExampleType{}
|
||||
err = db.Load(
|
||||
[]string{"examples", "example1"},
|
||||
&newStruct,
|
||||
)
|
||||
fmt.Println(newStruct)
|
||||
*/
|
||||
}
|
||||
|
||||
func example2() {
|
||||
fmt.Println("# Example 2")
|
||||
db, err := boltease.Create("example.db", 0o600, nil)
|
||||
if err != nil {
|
||||
fmt.Printf("Error Opening File: %s\n", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Println("## Saving Struct")
|
||||
num := 12345
|
||||
err = db.Save(
|
||||
[]string{"examples"},
|
||||
"example2",
|
||||
&ExampleType2{
|
||||
Name: "Example 2",
|
||||
Age: 20,
|
||||
SubTypePtr: &ExampleSubType{
|
||||
SubName: "Example SubType Pointer",
|
||||
},
|
||||
SubType: ExampleSubType{
|
||||
SubName: "Example SubType",
|
||||
},
|
||||
Number: &num,
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Printf("Error saving struct: %s\n", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
newStruct := ExampleType2{}
|
||||
err = db.Load([]string{"examples", "example2"}, &newStruct)
|
||||
fmt.Println(newStruct)
|
||||
}
|
||||
|
||||
type ExampleType struct {
|
||||
Name string `boltease:"name"`
|
||||
Age int `boltease:"age"`
|
||||
}
|
||||
|
||||
func (e ExampleType) String() string {
|
||||
r, _ := json.Marshal(e)
|
||||
return string(r)
|
||||
}
|
||||
|
||||
type ExampleType2 struct {
|
||||
Name string `boltease:"name"`
|
||||
Age int `boltease:"age"`
|
||||
SubTypePtr *ExampleSubType `boltease:"subtypeptr"`
|
||||
SubType ExampleSubType `boltease:"subtype"`
|
||||
Number *int `boltease:"number"`
|
||||
}
|
||||
|
||||
func (e ExampleType2) String() string {
|
||||
r, _ := json.Marshal(e)
|
||||
return string(r)
|
||||
}
|
||||
|
||||
type ExampleSubType struct {
|
||||
SubName string `boltease:"subname"`
|
||||
}
|
||||
|
||||
func (e ExampleSubType) String() string {
|
||||
r, _ := json.Marshal(e)
|
||||
return string(r)
|
||||
}
|
2
go.mod
2
go.mod
@ -1,6 +1,6 @@
|
||||
module git.bullercodeworks.com/brian/boltease
|
||||
|
||||
go 1.23.0
|
||||
go 1.22
|
||||
|
||||
require (
|
||||
github.com/boltdb/bolt v1.3.1
|
||||
|
52
tags.go
Normal file
52
tags.go
Normal file
@ -0,0 +1,52 @@
|
||||
package boltease
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
func isValidTag(s string) bool {
|
||||
if s == "" {
|
||||
return false
|
||||
}
|
||||
for _, c := range s {
|
||||
switch {
|
||||
case strings.ContainsRune("!#$%&()*+-./:;<=>?@[]^_{|}~ ", c):
|
||||
// Backslash and quote chars are reserved, but
|
||||
// otherwise any punctuation chars are allowed
|
||||
// in a tag name.
|
||||
case !unicode.IsLetter(c) && !unicode.IsDigit(c):
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// tagOptions is the string following a comma in a struct field's "boltease"
|
||||
// tag, or the empty string. It does not include the leading comma
|
||||
type tagOptions string
|
||||
|
||||
// parseTag splits a struct field's boltease tag into it's name and
|
||||
// comma-separated options.
|
||||
func parseTag(tag string) (string, tagOptions) {
|
||||
tag, opt, _ := strings.Cut(tag, ",")
|
||||
return tag, tagOptions(opt)
|
||||
}
|
||||
|
||||
// Contains reports whether a comma-separated list of options
|
||||
// contains a particular substr flag. Substr must be surrounded by a
|
||||
// string boundary or commas.
|
||||
func (o tagOptions) Contains(optionName string) bool {
|
||||
if len(o) == 0 {
|
||||
return false
|
||||
}
|
||||
s := string(o)
|
||||
for s != "" {
|
||||
var name string
|
||||
name, s, _ = strings.Cut(s, ",")
|
||||
if name == optionName {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
Loading…
Reference in New Issue
Block a user