github.com/cilium/statedb@v0.3.2/reconciler/example/types.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package main 5 6 import ( 7 "github.com/spf13/pflag" 8 9 "github.com/cilium/statedb" 10 "github.com/cilium/statedb/index" 11 "github.com/cilium/statedb/reconciler" 12 ) 13 14 // Config defines the command-line configuration for the memos 15 // example application. 16 type Config struct { 17 Directory string // the directory in which memos are stored. 18 } 19 20 func (def Config) Flags(flags *pflag.FlagSet) { 21 flags.String("directory", "memos", "Memo directory") 22 } 23 24 // Memo is a brief note stored in the memos directory. A memo 25 // can be created with the /memos API. 26 type Memo struct { 27 Name string // filename of the memo. Stored in <directory>/<name>. 28 Content string // contents of the memo. 29 Status reconciler.Status // reconciliation status 30 } 31 32 // GetStatus returns the reconciliation status. Used to provide the 33 // reconciler access to it. 34 func (memo *Memo) GetStatus() reconciler.Status { 35 return memo.Status 36 } 37 38 // SetStatus sets the reconciliation status. 39 // Used by the reconciler to update the reconciliation status of the memo. 40 func (memo *Memo) SetStatus(newStatus reconciler.Status) *Memo { 41 memo.Status = newStatus 42 return memo 43 } 44 45 // Clone returns a shallow copy of the memo. 46 func (memo *Memo) Clone() *Memo { 47 m := *memo 48 return &m 49 } 50 51 // MemoNameIndex allows looking up the memo by its name, e.g. 52 // memos.First(txn, MemoNameIndex.Query("my-memo")) 53 var MemoNameIndex = statedb.Index[*Memo, string]{ 54 Name: "name", 55 FromObject: func(memo *Memo) index.KeySet { 56 return index.NewKeySet(index.String(memo.Name)) 57 }, 58 FromKey: index.String, 59 Unique: true, 60 } 61 62 // MemoStatusIndex indexes memos by their reconciliation status. 63 // This is mainly used by the reconciler to implement WaitForReconciliation. 64 var MemoStatusIndex = reconciler.NewStatusIndex((*Memo).GetStatus) 65 66 // NewMemoTable creates and registers the memos table. 67 func NewMemoTable(db *statedb.DB) (statedb.RWTable[*Memo], statedb.Index[*Memo, reconciler.StatusKind], error) { 68 tbl, err := statedb.NewTable( 69 "memos", 70 MemoNameIndex, 71 MemoStatusIndex, 72 ) 73 if err == nil { 74 err = db.RegisterTable(tbl) 75 } 76 return tbl, MemoStatusIndex, err 77 }