MCPcopy Create free account
hub / github.com/chronoxor/FastBinaryEncoding / main

Function main

projects/Go/examples/create.go:7–51  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

5import "../proto/proto"
6
7func main() {
8 // Create a new account using FBE model
9 var account = proto.NewAccountModel(fbe.NewEmptyBuffer())
10 modelBegin := account.CreateBegin()
11 accountBegin, _ := account.Model().SetBegin()
12 _ = account.Model().Id.Set(1)
13 _ = account.Model().Name.Set("Test")
14 _ = account.Model().State.SetValue(proto.State_good)
15 walletBegin, _ := account.Model().Wallet.SetBegin()
16 _ = account.Model().Wallet.Currency.Set("USD")
17 _ = account.Model().Wallet.Amount.Set(1000.0)
18 account.Model().Wallet.SetEnd(walletBegin)
19 account.Model().SetEnd(accountBegin)
20 account.CreateEnd(modelBegin)
21 if ok := account.Verify(); !ok {
22 panic("verify error")
23 }
24
25 // Show the serialized FBE size
26 fmt.Printf("FBE size: %d\n", account.Buffer().Size())
27
28 // Access the account using the FBE model
29 access := proto.NewAccountModel(account.Buffer())
30 if ok := access.Verify(); !ok {
31 panic("verify error")
32 }
33
34 accountBegin, _ = access.Model().GetBegin()
35 id, _ := access.Model().Id.Get()
36 name, _ := access.Model().Name.Get()
37 state, _ := access.Model().State.Get()
38 walletBegin, _ = access.Model().Wallet.GetBegin()
39 walletCurrency, _ := access.Model().Wallet.Currency.Get()
40 walletAmount, _ := access.Model().Wallet.Amount.Get()
41 access.Model().Wallet.GetEnd(walletBegin)
42 access.Model().GetEnd(accountBegin)
43
44 // Show account content
45 fmt.Println()
46 fmt.Printf("account.id = %v\n", id)
47 fmt.Printf("account.name = %v\n", name)
48 fmt.Printf("account.state = %v\n", state)
49 fmt.Printf("account.wallet.currency = %v\n", walletCurrency)
50 fmt.Printf("account.wallet.amount = %v\n", walletAmount)
51}

Callers

nothing calls this directly

Calls 15

VerifyMethod · 0.95
ModelMethod · 0.95
CreateBeginMethod · 0.45
SetBeginMethod · 0.45
ModelMethod · 0.45
SetMethod · 0.45
SetValueMethod · 0.45
SetEndMethod · 0.45
CreateEndMethod · 0.45
VerifyMethod · 0.45
SizeMethod · 0.45
BufferMethod · 0.45

Tested by

no test coverage detected