generated from oracle/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
118 lines (98 loc) · 2.93 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* Copyright (c) 2023, 2024 Oracle and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at
* https://oss.oracle.com/licenses/upl.
*/
/*
Package main shows how to carry out basic operations against a NamedMap where a key and value are structs.
*/
package main
import (
"context"
"fmt"
"github.com/oracle/coherence-go-client/v2/coherence"
"github.com/oracle/coherence-go-client/v2/coherence/processors"
)
// AccountKey defines the key for an account.
type AccountKey struct {
AccountID int `json:"accountID"`
AccountType string `json:"accountType"`
}
func (ak AccountKey) String() string {
return fmt.Sprintf("AccountKey{accountID=%d, accountType=%s}", ak.AccountID, ak.AccountType)
}
// Account defines the attributes for an account.
type Account struct {
AccountID int `json:"accountID"`
AccountType string `json:"accountType"`
Name string `json:"name"`
Balance float32 `json:"balance"`
}
// GetKey returns the AccountKey for an Account.
func (a Account) GetKey() AccountKey {
return AccountKey{
AccountID: a.AccountID,
AccountType: a.AccountType,
}
}
func (a Account) String() string {
return fmt.Sprintf("Account{accountKey=%v, name=%s, balance=$%.2f}", a.GetKey(), a.Name, a.Balance)
}
func main() {
var (
account *Account
size int
ctx = context.Background()
)
// create a new Session
session, err := coherence.NewSession(ctx, coherence.WithPlainText())
if err != nil {
panic(err)
}
defer session.Close()
// create a new NamedMap of Account with key of AccountKey
namedMap, err := coherence.GetNamedMap[AccountKey, Account](session, "accounts")
if err != nil {
panic(err)
}
// clear the Map
if err = namedMap.Clear(ctx); err != nil {
panic(err)
}
newAccountKey := AccountKey{AccountID: 100, AccountType: "savings"}
newAccount := Account{AccountID: newAccountKey.AccountID, AccountType: newAccountKey.AccountType,
Name: "John Doe", Balance: 100_000}
fmt.Println("Add new Account", newAccount, "with key", newAccountKey)
if _, err = namedMap.Put(ctx, newAccount.GetKey(), newAccount); err != nil {
panic(err)
}
if size, err = namedMap.Size(ctx); err != nil {
panic(err)
}
fmt.Println("Cache size is", size)
// get the Account
if account, err = namedMap.Get(ctx, newAccountKey); err != nil {
panic(err)
}
fmt.Println("Account from Get() is", *account)
fmt.Println("Update account balance using processor")
// Update the balance by $1000
_, err = coherence.Invoke[AccountKey, Account, bool](ctx, namedMap, newAccountKey,
processors.Update("balance", account.Balance+1000))
if err != nil {
panic(err)
}
// get the Account
if account, err = namedMap.Get(ctx, newAccountKey); err != nil {
panic(err)
}
fmt.Println("Updated account is", *account)
_, err = namedMap.Remove(ctx, newAccountKey)
if err != nil {
panic(err)
}
if size, err = namedMap.Size(ctx); err != nil {
panic(err)
}
fmt.Println("Cache size is", size)
}