| 1 | // Copyright 2023 Google LLC. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | // Package lockedcallbacks provides locking per-key. |
| 16 | package lockedcallbacksscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/ |
| 17 | |
| 18 | import ( |
| 19 | "errorsscip-go gomod github.com/golang/go/src go1.21 errors/" |
| 20 | "ioscip-go gomod github.com/golang/go/src go1.21 io/" |
| 21 | "syncscip-go gomod github.com/golang/go/src go1.21 sync/" |
| 22 | "text/templatescip-go gomod github.com/golang/go/src go1.21 `text/template`/" |
| 23 | ) |
| 24 | |
| 25 | // ErrStateKeyNotFound indicates the requested key was not found in the map of states |
| 26 | var ErrStateKeyNotFoundscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/ErrStateKeyNotFound. error = errorsscip-go gomod github.com/golang/go/src go1.21 errors/.Newscip-go gomod github.com/golang/go/src go1.21 errors/New().("Unlock requested for an unknown state") |
| 27 | |
| 28 | // StatesMap wraps a map of Mstate. Each key locks separately. |
| 29 | type StatesMapscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/StatesMap# struct { |
| 30 | maplockscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/StatesMap#maplock. syncscip-go gomod github.com/golang/go/src go1.21 sync/.Mutexscip-go gomod github.com/golang/go/src go1.21 sync/Mutex# // locks the States map |
| 31 | Statesscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/StatesMap#States. map[any]*Mstatescip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/Mstate# // contains states for each template |
| 32 | } |
| 33 | |
| 34 | // Mstate is used to represent entries of StatesMap |
| 35 | // each entry (Mstate) holds a callback. A mutex is protecting changes to the callback |
| 36 | // The key and statesmap attributes are used to provide self-cleaning of the entries once a state |
| 37 | // does not need to be maintained. |
| 38 | type Mstatescip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/Mstate# struct { |
| 39 | statesmapscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/Mstate#statesmap. *StatesMapscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/StatesMap# // points back to StatesMap, so we can synchronize removing this entry when cnt==0 |
| 40 | statelockscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/Mstate#statelock. syncscip-go gomod github.com/golang/go/src go1.21 sync/.Mutexscip-go gomod github.com/golang/go/src go1.21 sync/Mutex# // is an entry-specific lock |
| 41 | cntscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/Mstate#cnt. int // references to the state |
| 42 | keyscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/Mstate#key. any // indicates the key in the parent states map |
| 43 | Callbackscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/Mstate#Callback. func(string) string // points to the callback to protect |
| 44 | AllowFlagsCallbackscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/Mstate#AllowFlagsCallback. func(string) string // points to the allowflags callback to protect |
| 45 | } |
| 46 | |
| 47 | // New returns an initialized StatesMap. |
| 48 | func Newscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/New().() *StatesMapscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/StatesMap# { |
| 49 | return &StatesMapscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/StatesMap#{Statesscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/StatesMap#States.: make(map[any]*Mstatescip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/Mstate#)} |
| 50 | } |
| 51 | |
| 52 | // Lock acquires a state corresponding to this key. |
| 53 | func (m *StatesMapscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/StatesMap#) lockscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/StatesMap#lock().(key any) *Mstatescip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/Mstate# { |
| 54 | |
| 55 | // read or create entry for this key atomically |
| 56 | m.maplockscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/StatesMap#maplock..Lockscip-go gomod github.com/golang/go/src go1.21 sync/Mutex#Lock().() |
| 57 | ent, ok := m.Statesscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/StatesMap#States.[key] |
| 58 | if !ok { |
| 59 | ent = &Mstatescip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/Mstate#{statesmapscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/Mstate#statesmap.: m, keyscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/Mstate#key.: key} |
| 60 | m.Statesscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/StatesMap#States.[key] = ent |
| 61 | } |
| 62 | ent.cntscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/Mstate#cnt.++ |
| 63 | m.maplockscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/StatesMap#maplock..Unlockscip-go gomod github.com/golang/go/src go1.21 sync/Mutex#Unlock().() |
| 64 | |
| 65 | ent.statelockscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/Mstate#statelock..Lockscip-go gomod github.com/golang/go/src go1.21 sync/Mutex#Lock().() |
| 66 | |
| 67 | return ent |
| 68 | } |
| 69 | |
| 70 | // Unlock releases the lock for this entry. |
| 71 | func (ms *Mstatescip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/Mstate#) unlockscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/Mstate#unlock().() error { |
| 72 | m := ms.statesmapscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/Mstate#statesmap. |
| 73 | |
| 74 | m.maplockscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/StatesMap#maplock..Lockscip-go gomod github.com/golang/go/src go1.21 sync/Mutex#Lock().() |
| 75 | defer m.maplockscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/StatesMap#maplock..Unlockscip-go gomod github.com/golang/go/src go1.21 sync/Mutex#Unlock().() |
| 76 | ent, ok := m.Statesscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/StatesMap#States.[ms.keyscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/Mstate#key.] |
| 77 | if !ok { |
| 78 | return ErrStateKeyNotFoundscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/ErrStateKeyNotFound. |
| 79 | } |
| 80 | ent.cntscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/Mstate#cnt.-- |
| 81 | if ent.cntscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/Mstate#cnt. < 1 { |
| 82 | ent.statesmapscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/Mstate#statesmap. = nil |
| 83 | delete(m.Statesscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/StatesMap#States., ms.keyscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/Mstate#key.) |
| 84 | } |
| 85 | ent.statelockscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/Mstate#statelock..Unlockscip-go gomod github.com/golang/go/src go1.21 sync/Mutex#Unlock().() |
| 86 | return nil |
| 87 | } |
| 88 | |
| 89 | // BuildTextTemplateRemediationFunc creates a virtual method calling the template's callback. |
| 90 | func (m *StatesMapscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/StatesMap#) BuildTextTemplateRemediationFuncscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/StatesMap#BuildTextTemplateRemediationFunc().(safeTmplUUID string, wrapperFunc func(any, func(string) string) any) func(any) any { |
| 91 | remediationFunc := func(data any) any { |
| 92 | m.maplockscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/StatesMap#maplock..Lockscip-go gomod github.com/golang/go/src go1.21 sync/Mutex#Lock().() |
| 93 | defer m.maplockscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/StatesMap#maplock..Unlockscip-go gomod github.com/golang/go/src go1.21 sync/Mutex#Unlock().() |
| 94 | tmplState, ok := m.Statesscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/StatesMap#States.[safeTmplUUID] |
| 95 | if !ok { |
| 96 | return nil |
| 97 | } |
| 98 | cb := tmplState.Callbackscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/Mstate#Callback. |
| 99 | return wrapperFunc(data, cb) |
| 100 | } |
| 101 | return remediationFunc |
| 102 | } |
| 103 | |
| 104 | // BuildAllowFlagsCallbackFunc creates a virtual method calling the template's allowFlags callback. |
| 105 | func (m *StatesMapscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/StatesMap#) BuildAllowFlagsCallbackFuncscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/StatesMap#BuildAllowFlagsCallbackFunc().(safeTmplUUID string, wrapperFunc func(any, func(string) string) any) func(data any) any { |
| 106 | allowFlagsCallbackFunc := func(data any) any { |
| 107 | m.maplockscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/StatesMap#maplock..Lockscip-go gomod github.com/golang/go/src go1.21 sync/Mutex#Lock().() |
| 108 | defer m.maplockscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/StatesMap#maplock..Unlockscip-go gomod github.com/golang/go/src go1.21 sync/Mutex#Unlock().() |
| 109 | tmplState, ok := m.Statesscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/StatesMap#States.[safeTmplUUID] |
| 110 | if !ok { |
| 111 | return nil |
| 112 | } |
| 113 | allowFlagsCb := tmplState.AllowFlagsCallbackscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/Mstate#AllowFlagsCallback. |
| 114 | return wrapperFunc(data, allowFlagsCb) |
| 115 | } |
| 116 | return allowFlagsCallbackFunc |
| 117 | } |
| 118 | |
| 119 | // SetAndExecuteWithCallback hides implementation details of the StatesMap by taking a template and |
| 120 | // a callback to set the proper callback and execute the template. |
| 121 | func (m *StatesMapscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/StatesMap#) SetAndExecuteWithCallbackscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/StatesMap#SetAndExecuteWithCallback().(tmpl *templatescip-go gomod github.com/golang/go/src go1.21 `text/template`/.Templatescip-go gomod github.com/golang/go/src go1.21 `text/template`/Template#, safeTmplUUID string, cb func(string) string, result ioscip-go gomod github.com/golang/go/src go1.21 io/.Writerscip-go gomod github.com/golang/go/src go1.21 io/Writer#, data any) error { |
| 122 | tmplState := m.lockscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/StatesMap#lock().(safeTmplUUID) |
| 123 | tmplState.Callbackscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/Mstate#Callback. = cb |
| 124 | |
| 125 | err := tmpl.Executescip-go gomod github.com/golang/go/src go1.21 `text/template`/Template#Execute().(result, data) |
| 126 | if err != nil { |
| 127 | return err |
| 128 | } |
| 129 | return tmplState.unlockscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/Mstate#unlock().() |
| 130 | } |
| 131 | |
| 132 | // SetAndExecuteWithShCallback hides implementation details of the StatesMap by taking a template |
| 133 | // and both callbacks to set the proper callback and execute the template. |
| 134 | func (m *StatesMapscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/StatesMap#) SetAndExecuteWithShCallbackscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/StatesMap#SetAndExecuteWithShCallback().(tmpl *templatescip-go gomod github.com/golang/go/src go1.21 `text/template`/.Templatescip-go gomod github.com/golang/go/src go1.21 `text/template`/Template#, safeTmplUUID string, cb func(string) string, allowFlagsCb func(string) string, result ioscip-go gomod github.com/golang/go/src go1.21 io/.Writerscip-go gomod github.com/golang/go/src go1.21 io/Writer#, data any) error { |
| 135 | tmplState := m.lockscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/StatesMap#lock().(safeTmplUUID) |
| 136 | tmplState.Callbackscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/Mstate#Callback. = cb |
| 137 | tmplState.AllowFlagsCallbackscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/Mstate#AllowFlagsCallback. = allowFlagsCb |
| 138 | |
| 139 | err := tmpl.Executescip-go gomod github.com/golang/go/src go1.21 `text/template`/Template#Execute().(result, data) |
| 140 | if err != nil { |
| 141 | return err |
| 142 | } |
| 143 | return tmplState.unlockscip-go gomod github.com/google/safetext . `github.com/google/safetext/lockedcallbacks`/Mstate#unlock().() |
| 144 | } |