Last active
September 13, 2019 09:52
-
-
Save drillbits/ad34e6a75cf5d381cd68eef162f9f271 to your computer and use it in GitHub Desktop.
memdocstore: slice field breaks localfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"io" | |
"io/ioutil" | |
"log" | |
"path/filepath" | |
"time" | |
"gocloud.dev/docstore" | |
"gocloud.dev/docstore/memdocstore" | |
) | |
type Doc struct { | |
Key string `docstore:"keyField"` | |
Array []int `docstore:"array,omitempty"` | |
DocstoreRevision interface{} | |
} | |
func main() { | |
ctx := context.Background() | |
tmpdir, err := ioutil.TempDir("", "docstoretest") | |
if err != nil { | |
log.Fatalf("failed to create tmpdir: %s\n", err) | |
} | |
tmpfile := filepath.Join(tmpdir, "coll") | |
log.Printf("tmpfile: %s\n", tmpfile) | |
log.Println("") | |
log.Println("create without array") | |
log.Println("====================") | |
{ | |
d := &Doc{Key: fmt.Sprintf("%s", time.Now())} | |
coll, err := open(tmpfile) | |
if err != nil { | |
log.Fatalf("failed to open collection before create: %s\n", err) | |
} | |
log.Println("open") | |
err = coll.Create(ctx, d) | |
if err != nil { | |
log.Fatalf("failed to create doc: %s\n", err) | |
} | |
log.Println("create") | |
coll.Close() | |
log.Println("close") | |
coll, err = open(tmpfile) | |
if err != nil { | |
log.Fatalf("failed to open collection after create: %s\n", err) | |
} | |
log.Println("open") | |
log.Println("listing") | |
list(ctx, coll) | |
coll.Close() | |
log.Println("close") | |
log.Println("") | |
} | |
log.Println("create with array") | |
log.Println("=================") | |
{ | |
d := &Doc{Key: fmt.Sprintf("%s", time.Now()), Array: []int{1}} | |
coll, err := open(tmpfile) | |
if err != nil { | |
log.Fatalf("failed to open collection before create: %s\n", err) | |
} | |
log.Println("open") | |
err = coll.Create(ctx, d) | |
if err != nil { | |
log.Fatalf("failed to create doc: %s\n", err) | |
} | |
log.Println("create") | |
coll.Close() | |
log.Println("close") | |
coll, err = open(tmpfile) | |
if err != nil { | |
log.Fatalf("failed to open collection after create: %s\n", err) | |
} | |
log.Println("open") | |
log.Println("listing") | |
list(ctx, coll) | |
coll.Close() | |
log.Println("close") | |
log.Println("") | |
} | |
} | |
func open(f string) (*docstore.Collection, error) { | |
return memdocstore.OpenCollection("keyField", &memdocstore.Options{ | |
Filename: f, | |
}) | |
} | |
func list(ctx context.Context, coll *docstore.Collection) { | |
iter := coll.Query().Get(ctx) | |
defer iter.Stop() | |
for { | |
var d Doc | |
err := iter.Next(ctx, &d) | |
if err == io.EOF { | |
break | |
} else if err != nil { | |
log.Println(err) | |
return | |
} else { | |
log.Printf("- %#v\n", d) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2019/09/13 18:52:16 tmpfile: /var/folders/_j/zj04k0f94f73l_bxhjwtr07c0000gn/T/docstoretest913445769/coll | |
2019/09/13 18:52:16 | |
2019/09/13 18:52:16 create without array | |
2019/09/13 18:52:16 ==================== | |
2019/09/13 18:52:16 open | |
2019/09/13 18:52:16 create | |
2019/09/13 18:52:16 close | |
2019/09/13 18:52:16 open | |
2019/09/13 18:52:16 listing | |
2019/09/13 18:52:16 - main.Doc{Key:"2019-09-13 18:52:16.537114 +0900 JST m=+0.004645387", Array:[]int(nil), DocstoreRevision:1} | |
2019/09/13 18:52:16 close | |
2019/09/13 18:52:16 | |
2019/09/13 18:52:16 create with array | |
2019/09/13 18:52:16 ================= | |
2019/09/13 18:52:16 open | |
2019/09/13 18:52:16 create | |
2019/09/13 18:52:16 close | |
2019/09/13 18:52:16 failed to open collection after create: EOF | |
exit status 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment