V3 is off to a great start!

This commit is contained in:
2022-04-27 17:13:05 -05:00
parent 7443259b23
commit c2d2c04b1a
10 changed files with 838 additions and 142 deletions

View File

@@ -1,13 +1,17 @@
package util
import (
"encoding/binary"
"fmt"
"os"
"unicode/utf8"
bolt "go.etcd.io/bbolt"
)
func ToJsonString(b *bolt.Bucket) string {
// TODO: Escape any double quotes inside
// TODO: Deal with empty buckets
ret := "{"
b.ForEach(func(k, v []byte) error {
ret = fmt.Sprintf("%s\"%s\":", ret, string(k))
@@ -44,3 +48,34 @@ func WriteToFile(fn, s string, mode int) error {
}
return nil
}
// stringify ensures that we can print only valid characters.
// It's wrong to assume that everything is a string, since BoltDB is typeless.
func Stringify(v []byte) string {
if utf8.Valid(v) {
ok := true
for _, r := range string(v) {
if r < 0x20 {
ok = false
break
} else if r >= 0x7f && r <= 0x9f {
ok = false
break
}
}
if ok {
return string(v)
}
}
if len(v) == 8 {
return fmt.Sprintf("%v", binary.BigEndian.Uint64(v))
}
return fmt.Sprintf("%x", v)
}
func StringifyPath(path []string) []string {
for k, v := range path {
path[k] = Stringify([]byte(v))
}
return path
}