Skip to content

Commit 264b8e9

Browse files
committed
install iroh-go in the go examples folder and adjust tests
1 parent 7570b55 commit 264b8e9

File tree

13 files changed

+78
-49
lines changed

13 files changed

+78
-49
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,7 @@ yarn-error.log*
3535
# typescript
3636
*.tsbuildinfo
3737
next-env.d.ts
38+
39+
# go
40+
extern
41+
iroh_data_dir

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "api-code-examples/go/extern/iroh-ffi"]
2+
path = api-code-examples/go/extern/iroh-ffi
3+
url = https://github.com/n0-computer/iroh-ffi.git

api-code-examples/go/author-list.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ package main
33
import (
44
"fmt"
55

6-
"github.com/n0-computer/iroh-ffi/iroh"
6+
"github.com/n0-computer/iroh-ffi/iroh-go/iroh"
77
)
88

99
func main() {
10-
node, err := iroh.NewIrohNode()
10+
node, err := iroh.NewIrohNode("iroh_data_dir")
1111
if err != nil {
1212
panic(err)
1313
}
1414

15-
if _, err := node.AuthorNew(); err != nil {
15+
if _, err := node.AuthorCreate(); err != nil {
1616
panic(err)
1717
}
1818

api-code-examples/go/author-new.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ package main
33
import (
44
"fmt"
55

6-
"github.com/n0-computer/iroh-ffi/iroh"
6+
"github.com/n0-computer/iroh-ffi/iroh-go/iroh"
77
)
88

99
func main() {
10-
node, err := iroh.NewIrohNode()
10+
node, err := iroh.NewIrohNode("iroh_data_dir")
1111
if err != nil {
1212
panic(err)
1313
}
1414

15-
author, err := node.AuthorNew()
15+
author, err := node.AuthorCreate()
1616
if err != nil {
1717
panic(err)
1818
}

api-code-examples/go/doc-get.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package main
33
import (
44
"fmt"
55

6-
"github.com/n0-computer/iroh-ffi/iroh"
6+
"github.com/n0-computer/iroh-ffi/iroh-go/iroh"
77
)
88

99
func main() {
10-
node, err := iroh.NewIrohNode()
10+
node, err := iroh.NewIrohNode("iroh_data_dir")
1111
if err != nil {
1212
// real programs handle errors!
1313
panic(err)

api-code-examples/go/doc-join.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ package main
33
import (
44
"fmt"
55

6-
"github.com/n0-computer/iroh-ffi/iroh"
6+
"github.com/n0-computer/iroh-ffi/iroh-go/iroh"
77
)
88

99
// you'll need to get a ticket from somewhere, see the `doc share` command documentation
10-
// for details. This ticket will fail to join, but is a valid ticket.
11-
const ticketString = "sdx7wazju2rysqydgfpmhqkrluc5lbcuainravjipwmbl7r3k3uqcigncyopdcnooufnteu5vuatpzhoqrml35ifgyuozr6kwhjiz4jxyaaqcbiajbmsainmsmbqcjqaibavg5hiaaaaaaaaaaabad47sebqbqfiiqzkzeydadakqrckvsjqgajgabaecu3u5aaaaaaaaaaaaeagt6iqg"
10+
// for details.
11+
12+
const ticketString = "docaaqjjfgbzx2ry4zpaoujdppvqktgvfvpxgqubkghiialqovv7z4wosqbebpvjjp2tywajvg6unjza6dnugkalg4srmwkcucmhka7mgy4r3aa4aibayaeusjsjlcfoagavaa4xrcxaetag4aaq45mxvqaaaaaaaaadiu4kvybeybxaaehhlf5mdenfufmhk7nixcvoajganyabbz2zplgbno2vsnuvtkpyvlqcjqdoaaioowl22k3fc26qjx4ot6fk4"
1213

1314
func main() {
14-
node, err := iroh.NewIrohNode()
15+
node, err := iroh.NewIrohNode("iroh_data_dir")
1516
if err != nil {
1617
panic(err)
1718
}
@@ -26,5 +27,5 @@ func main() {
2627
panic(err)
2728
}
2829

29-
fmt.Println("Joined doc: %s", doc.Id())
30+
fmt.Println("Joined doc:", doc.Id().ToString())
3031
}

api-code-examples/go/doc-keys.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,48 @@ package main
33
import (
44
"fmt"
55

6-
"github.com/n0-computer/iroh-ffi/iroh"
6+
"github.com/n0-computer/iroh-ffi/iroh-go/iroh"
77
)
88

99
func main() {
10-
node, err := iroh.NewIrohNode()
10+
node, err := iroh.NewIrohNode("iroh_data_dir")
1111
if err != nil {
1212
panic(err)
1313
}
1414

15-
author, err := node.AuthorNew()
15+
author, err := node.AuthorCreate()
1616
if err != nil {
1717
panic(err)
1818
}
1919

20-
doc, err := node.DocNew()
20+
doc, err := node.DocCreate()
2121
if err != nil {
2222
panic(err)
2323
}
24-
fmt.Printf("Created document %s\n", doc.Id())
24+
fmt.Printf("Created document %s\n", doc.Id().ToString())
2525

2626
for i, key := range []string{"a", "b", "c"} {
2727
if _, err := doc.SetBytes(author, []byte(key), []byte(fmt.Sprintf("%d", i))); err != nil {
2828
panic(err)
2929
}
3030
}
3131

32-
keys, err := doc.Keys()
32+
// get all the entries with default filtering and sorting
33+
query := iroh.QueryAll(nil)
34+
entries, err := doc.GetMany(query)
3335
if err != nil {
3436
panic(err)
3537
}
3638

3739
fmt.Println("Keys:")
38-
for _, key := range keys {
39-
content, err := doc.GetContentBytes(key)
40+
for _, entry := range entries {
41+
key := entry.Key()
42+
hash := entry.ContentHash()
43+
content, err := doc.ReadToBytes(entry)
4044
if err != nil {
4145
panic(err)
4246
}
43-
fmt.Printf("%q : %q (hash: %s)\n", string(key.Key()), string(content), key.Hash().ToString())
47+
fmt.Printf("%q : %q (hash: %s)\n", string(key), string(content), hash.ToString())
4448
}
4549
// Output:
4650
// Created document 7hgonoxdjzlwtuicfyou24l5nhv3bmvzhyeq6v2er66ekrpvhotq

api-code-examples/go/doc-list.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,29 @@ package main
33
import (
44
"fmt"
55

6-
"github.com/n0-computer/iroh-ffi/iroh"
6+
"github.com/n0-computer/iroh-ffi/iroh-go/iroh"
77
)
88

99
func main() {
10-
node, err := iroh.NewIrohNode()
10+
node, err := iroh.NewIrohNode("iroh_data_dir")
1111
if err != nil {
1212
// real programs handle errors!
1313
panic(err)
1414
}
1515

1616
// create one document
17-
doc, err := node.DocNew()
17+
doc, err := node.DocCreate()
1818
if err != nil {
1919
panic(err)
2020
}
21-
fmt.Printf("Created document %s\n", doc.Id())
21+
fmt.Printf("Created document %s\n", doc.Id().ToString())
2222

2323
// create a second document
24-
doc, err := node.DocNew()
24+
doc, err = node.DocCreate()
2525
if err != nil {
2626
panic(err)
2727
}
28-
fmt.Printf("Created document %s\n", doc.Id())
28+
fmt.Printf("Created document %s\n", doc.Id().ToString())
2929

3030
// list all your documents
3131
docs, err := node.DocList()
@@ -34,7 +34,8 @@ func main() {
3434
}
3535

3636
fmt.Printf("Listing all %d documents:\n", len(docs))
37-
for _, doc_id := range docs {
38-
fmt.Printf("\t%s\n", doc_id.ToString())
37+
// doc ids are also called "NamespaceIds"
38+
for _, namespaceAndCapability := range docs {
39+
fmt.Printf("\t%s\n", namespaceAndCapability.Namespace.ToString())
3940
}
4041
}

api-code-examples/go/doc-new.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@ package main
33
import (
44
"fmt"
55

6-
"github.com/n0-computer/iroh-ffi/iroh"
6+
"github.com/n0-computer/iroh-ffi/iroh-go/iroh"
77
)
88

99
func main() {
10-
node, err := iroh.NewIrohNode()
10+
node, err := iroh.NewIrohNode("iroh_data_dir")
1111
if err != nil {
1212
// real programs handle errors!
1313
panic(err)
1414
}
1515

16-
doc, err := node.DocNew()
16+
doc, err := node.DocCreate()
1717
if err != nil {
1818
panic(err)
1919
}
20-
fmt.Printf("Created document %s\n", doc.Id())
20+
fmt.Printf("Created document %s\n", doc.Id().ToString())
2121
}

api-code-examples/go/doc-set.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,44 @@ package main
33
import (
44
"fmt"
55

6-
"github.com/n0-computer/iroh-ffi/iroh"
6+
"github.com/n0-computer/iroh-ffi/iroh-go/iroh"
77
)
88

99
func main() {
10-
node, err := iroh.NewIrohNode()
10+
node, err := iroh.NewIrohNode("iroh_data_dir")
1111
if err != nil {
1212
// real programs handle errors!
1313
panic(err)
1414
}
1515

16-
author, err := node.AuthorNew()
16+
author, err := node.AuthorCreate()
1717
if err != nil {
1818
panic(err)
1919
}
2020
fmt.Printf("Created author %s\n", author.ToString())
2121

22-
doc, err := node.DocNew()
22+
doc, err := node.DocCreate()
2323
if err != nil {
2424
panic(err)
2525
}
26-
fmt.Printf("Created document %s\n", doc.Id())
26+
fmt.Printf("Created document %s\n", doc.Id().ToString())
2727

28-
hash, err := doc.SetBytes(author, []byte("go"), []byte("says hello"))
28+
key := []byte("go")
29+
hash, err := doc.SetBytes(author, key, []byte("says hello"))
2930
if err != nil {
3031
panic(err)
3132
}
3233
fmt.Printf("Inserted %s\n", hash.ToString())
3334

34-
content, err := doc.GetContentBytes(hash)
35+
// returns a pointer to an entry
36+
entry, err := doc.GetExact(author, key, false)
37+
if err != nil {
38+
panic(err)
39+
}
40+
41+
// dereference the pointer to the entry once you know doc.GetExact did not
42+
// return an error
43+
content, err := doc.ReadToBytes(*entry)
3544
if err != nil {
3645
panic(err)
3746
}

api-code-examples/go/doc-share.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,33 @@ package main
33
import (
44
"fmt"
55

6-
"github.com/n0-computer/iroh-ffi/iroh"
6+
"github.com/n0-computer/iroh-ffi/iroh-go/iroh"
77
)
88

99
func main() {
10-
node, err := iroh.NewIrohNode()
10+
node, err := iroh.NewIrohNode("iroh_data_dir")
1111
if err != nil {
1212
panic(err)
1313
}
1414

15-
doc, err := node.DocNew()
15+
doc, err := node.DocCreate()
1616
if err != nil {
1717
panic(err)
1818
}
19-
fmt.Printf("Created document %s\n", doc.Id())
19+
fmt.Printf("Created document %s\n", doc.Id().ToString())
2020

2121
// TODO - sharing read-only tickets is not yet implemented. Coming soon!
22-
// readTicket, err := doc.ShareRead()
22+
// readTicket, err := doc.Share(iroh.ShareModeRead)
2323
// if err != nil {
2424
// panic(err)
2525
// }
2626
// fmt.Println(readTicket.ToString())
2727

28-
writeTicket, err := doc.ShareWrite()
28+
writeTicket, err := doc.Share(iroh.ShareModeWrite)
2929
if err != nil {
3030
panic(err)
3131
}
32-
fmt.Println("Write-Access Ticket: %s", writeTicket.ToString())
32+
fmt.Println("Write-Access Ticket:", writeTicket.ToString())
3333
// Output:
3434
// Created document 7hgonoxdjzlwtuicfyou24l5nhv3bmvzhyeq6v2er66ekrpvhotq
3535
// Write-Access Ticket: sdx7wazju2rysqydgfpmhqkrluc5lbcuainravjipwmbl7r3k3uqcigncyopdcnooufnteu5vuatpzhoqrml35ifgyuozr6kwhjiz4jxyaaqcbiajbmsainmsmbqcjqaibavg5hiaaaaaaaaaaabad47sebqbqfiiqzkzeydadakqrckvsjqgajgabaecu3u5aaaaaaaaaaaaeagt6iqg

api-code-examples/go/go.mod

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,9 @@ module github.com/n0-computer/iroh.computer
22

33
go 1.21.1
44

5-
require github.com/n0-computer/iroh-ffi v0.0.7-0.20230927174552-77938699b905 // indirect
5+
require (
6+
github.com/n0-computer/iroh-ffi v0.0.7-0.20230927174552-77938699b905 // indirect
7+
github.com/n0-computer/iroh-ffi/iroh-go v0.11.0-alpha // indirect
8+
)
9+
10+
replace github.com/n0-computer/iroh-ffi/iroh-go => ./extern/iroh-ffi/iroh-go

api-code-examples/go/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
github.com/n0-computer/iroh-ffi v0.0.7-0.20230927174552-77938699b905 h1:4djPw1iRtcsmA1qN7l7sd03U/pxkx/JFJ4yJO5RQofE=
22
github.com/n0-computer/iroh-ffi v0.0.7-0.20230927174552-77938699b905/go.mod h1:puyEestvwIzSzADHLSeUe/vM2CyljV5hhKz6hcu7t94=
3+
github.com/n0-computer/iroh-ffi/iroh-go v0.11.0-alpha h1:1rERAwZjRcmprZn2owABT8vC8szhWQJ5NzzuOxZ7VDk=
4+
github.com/n0-computer/iroh-ffi/iroh-go v0.11.0-alpha/go.mod h1:4YFeQsObpqZ6OndeRGbs0xUIeukOdzPT4ptn8UZu8ug=

0 commit comments

Comments
 (0)