diff options
author | Ben Johnson <benbjohnson@yahoo.com> | 2014-04-11 15:11:55 -0600 |
---|---|---|
committer | Ben Johnson <benbjohnson@yahoo.com> | 2014-04-11 15:11:55 -0600 |
commit | 2c8020ec8e98e7b6c6c0fd3bd6e91d41caf7f25a (patch) | |
tree | 125c24e03c653417ce8bf5965b7fbcbeb2dedb04 /cmd/bolt/import.go | |
parent | Merge pull request #128 from benbjohnson/import-export (diff) | |
parent | Upgrade import/export to use nested buckets. (diff) | |
download | dedo-2c8020ec8e98e7b6c6c0fd3bd6e91d41caf7f25a.tar.gz dedo-2c8020ec8e98e7b6c6c0fd3bd6e91d41caf7f25a.tar.xz |
Merge pull request #127 from benbjohnson/nested-keys
Add nested buckets.
Diffstat (limited to 'cmd/bolt/import.go')
-rw-r--r-- | cmd/bolt/import.go | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/cmd/bolt/import.go b/cmd/bolt/import.go index ec8cee1..7554ae7 100644 --- a/cmd/bolt/import.go +++ b/cmd/bolt/import.go @@ -41,7 +41,7 @@ func Import(path string, input string) { } // Create the bucket if it doesn't exist. - if err := tx.CreateBucketIfNotExists(string(message.Key)); err != nil { + if err := tx.CreateBucketIfNotExists(message.Key); err != nil { return fmt.Errorf("create bucket: %s", err) } @@ -52,7 +52,7 @@ func Import(path string, input string) { } // Import all the values into the bucket. - b := tx.Bucket(string(message.Key)) + b := tx.Bucket(message.Key) if err := importBucket(b, children); err != nil { return fmt.Errorf("import bucket: %s", err) } @@ -67,7 +67,28 @@ func Import(path string, input string) { func importBucket(b *bolt.Bucket, children []*rawMessage) error { // Decode each message into a key/value pair. for _, child := range children { - // Decode the base64 value. + // Bucket messages are handled recursively. + if child.Type == "bucket" { + // Create the bucket if it doesn't exist. + if err := b.CreateBucketIfNotExists(child.Key); err != nil { + return fmt.Errorf("create bucket: %s", err) + } + + // Decode child messages. + var subchildren []*rawMessage + if err := json.Unmarshal(child.Value, &subchildren); err != nil { + return fmt.Errorf("decode children: %s", err) + } + + // Import subbucket. + subbucket := b.Bucket(child.Key) + if err := importBucket(subbucket, subchildren); err != nil { + return fmt.Errorf("import bucket: %s", err) + } + continue + } + + // Non-bucket values are decoded from base64. var value []byte if err := json.Unmarshal(child.Value, &value); err != nil { return fmt.Errorf("decode value: %s", err) |