Esempio n. 1
0
// mustContainKeys checks that a bucket contains a given set of keys.
func mustContainKeys(b *bolt.Bucket, m map[string]string) {
	found := make(map[string]string)
	b.ForEach(func(k, _ []byte) error {
		found[string(k)] = ""
		return nil
	})

	// Check for keys found in bucket that shouldn't be there.
	var keys []string
	for k, _ := range found {
		if _, ok := m[string(k)]; !ok {
			keys = append(keys, k)
		}
	}
	if len(keys) > 0 {
		sort.Strings(keys)
		panic(fmt.Sprintf("keys found(%d): %s", len(keys), strings.Join(keys, ",")))
	}

	// Check for keys not found in bucket that should be there.
	for k, _ := range m {
		if _, ok := found[string(k)]; !ok {
			keys = append(keys, k)
		}
	}
	if len(keys) > 0 {
		sort.Strings(keys)
		panic(fmt.Sprintf("keys not found(%d): %s", len(keys), strings.Join(keys, ",")))
	}
}
Esempio n. 2
0
func exportBucket(b *bolt.Bucket) (*rawMessage, error) {
	// Encode individual key/value pairs into raw messages.
	var children = make([]*rawMessage, 0)
	err := b.ForEach(func(k, v []byte) error {
		var err error

		// If there is no value then it is a bucket.
		if v == nil {
			child, err := exportBucket(b.Bucket(k))
			if err != nil {
				return fmt.Errorf("bucket: %s", err)
			}
			child.Key = k
			children = append(children, child)
			return nil
		}

		// Otherwise it's a regular key.
		var child = &rawMessage{Key: k}
		if child.Value, err = json.Marshal(v); err != nil {
			return fmt.Errorf("value: %s", err)
		}
		children = append(children, child)
		return nil
	})
	if err != nil {
		return nil, err
	}

	// Encode bucket into a raw message.
	var root = rawMessage{Type: "bucket"}
	if root.Value, err = json.Marshal(children); err != nil {
		return nil, fmt.Errorf("children: %s", err)
	}

	return &root, nil
}