|
| 1 | +// Mgmt |
| 2 | +// Copyright (C) James Shubin and the project contributors |
| 3 | +// Written by James Shubin <james@shubin.ca> and the project contributors |
| 4 | +// |
| 5 | +// This program is free software: you can redistribute it and/or modify |
| 6 | +// it under the terms of the GNU General Public License as published by |
| 7 | +// the Free Software Foundation, either version 3 of the License, or |
| 8 | +// (at your option) any later version. |
| 9 | +// |
| 10 | +// This program is distributed in the hope that it will be useful, |
| 11 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +// GNU General Public License for more details. |
| 14 | +// |
| 15 | +// You should have received a copy of the GNU General Public License |
| 16 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | +// |
| 18 | +// Additional permission under GNU GPL version 3 section 7 |
| 19 | +// |
| 20 | +// If you modify this program, or any covered work, by linking or combining it |
| 21 | +// with embedded mcl code and modules (and that the embedded mcl code and |
| 22 | +// modules which link with this program, contain a copy of their source code in |
| 23 | +// the authoritative form) containing parts covered by the terms of any other |
| 24 | +// license, the licensors of this program grant you additional permission to |
| 25 | +// convey the resulting work. Furthermore, the licensors of this program grant |
| 26 | +// the original author, James Shubin, additional permission to update this |
| 27 | +// additional permission if he deems it necessary to achieve the goals of this |
| 28 | +// additional permission. |
| 29 | + |
| 30 | +package structs |
| 31 | + |
| 32 | +import ( |
| 33 | + "context" |
| 34 | + "fmt" |
| 35 | + "sync" |
| 36 | + |
| 37 | + "github.com/purpleidea/mgmt/lang/interfaces" |
| 38 | + "github.com/purpleidea/mgmt/lang/types" |
| 39 | + "github.com/purpleidea/mgmt/pgraph" |
| 40 | + "github.com/purpleidea/mgmt/util/errwrap" |
| 41 | +) |
| 42 | + |
| 43 | +const ( |
| 44 | + // ExprIfFuncName is the unique name identifier for this function. |
| 45 | + ExprIfFuncName = "exprif" |
| 46 | + |
| 47 | + // ExprIfFuncArgNameCondition is the name for the edge which connects |
| 48 | + // the input condition to ExprIfFunc. |
| 49 | + ExprIfFuncArgNameCondition = "condition" |
| 50 | +) |
| 51 | + |
| 52 | +// ExprIfFunc is a function that passes through the value of the correct branch |
| 53 | +// based on the conditional value it gets. |
| 54 | +type ExprIfFunc struct { |
| 55 | + interfaces.Textarea |
| 56 | + |
| 57 | + Type *types.Type // this is the type of the if expression output we hold |
| 58 | + |
| 59 | + EdgeName string // name of the edge used |
| 60 | + |
| 61 | + ThenGraph *pgraph.Graph |
| 62 | + ElseGraph *pgraph.Graph |
| 63 | + |
| 64 | + ThenFunc interfaces.Func |
| 65 | + ElseFunc interfaces.Func |
| 66 | + |
| 67 | + OutputVertex interfaces.Func |
| 68 | + |
| 69 | + init *interfaces.Init |
| 70 | + last *bool // last value received to use for diff |
| 71 | +} |
| 72 | + |
| 73 | +// String returns a simple name for this function. This is needed so this struct |
| 74 | +// can satisfy the pgraph.Vertex interface. |
| 75 | +func (obj *ExprIfFunc) String() string { |
| 76 | + return ExprIfFuncName |
| 77 | +} |
| 78 | + |
| 79 | +// Validate tells us if the input struct takes a valid form. |
| 80 | +func (obj *ExprIfFunc) Validate() error { |
| 81 | + if obj.Type == nil { |
| 82 | + return fmt.Errorf("must specify a type") |
| 83 | + } |
| 84 | + |
| 85 | + if obj.EdgeName == "" { |
| 86 | + return fmt.Errorf("must specify an edge name") |
| 87 | + } |
| 88 | + |
| 89 | + if obj.ThenFunc == nil { |
| 90 | + return fmt.Errorf("must specify a then func") |
| 91 | + } |
| 92 | + if obj.ElseFunc == nil { |
| 93 | + return fmt.Errorf("must specify an else func") |
| 94 | + } |
| 95 | + |
| 96 | + t1 := obj.ThenFunc.Info().Sig.Out |
| 97 | + t2 := obj.ElseFunc.Info().Sig.Out |
| 98 | + if err := t1.Cmp(t2); err != nil { |
| 99 | + return errwrap.Wrapf(err, "types of exprif branches must match") |
| 100 | + } |
| 101 | + |
| 102 | + if obj.OutputVertex == nil { |
| 103 | + return fmt.Errorf("the output vertex is missing") |
| 104 | + } |
| 105 | + |
| 106 | + return nil |
| 107 | +} |
| 108 | + |
| 109 | +// Info returns some static info about itself. |
| 110 | +func (obj *ExprIfFunc) Info() *interfaces.Info { |
| 111 | + var typ *types.Type |
| 112 | + if obj.Type != nil { // don't panic if called speculatively |
| 113 | + typ = &types.Type{ |
| 114 | + Kind: types.KindFunc, // function type |
| 115 | + Map: map[string]*types.Type{ |
| 116 | + ExprIfFuncArgNameCondition: types.TypeBool, // conditional must be a boolean |
| 117 | + }, |
| 118 | + Ord: []string{ExprIfFuncArgNameCondition}, // conditional |
| 119 | + Out: obj.Type, // result type must match |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return &interfaces.Info{ |
| 124 | + Pure: true, |
| 125 | + Memo: false, // TODO: ??? |
| 126 | + Sig: typ, |
| 127 | + Err: obj.Validate(), |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +// Init runs some startup code for this if expression function. |
| 132 | +func (obj *ExprIfFunc) Init(init *interfaces.Init) error { |
| 133 | + obj.init = init |
| 134 | + return nil |
| 135 | +} |
| 136 | + |
| 137 | +// Stream takes an input struct in the format as described in the Func and Graph |
| 138 | +// methods of the Expr, and returns the actual expected value as a stream based |
| 139 | +// on the changing inputs to that value. |
| 140 | +func (obj *ExprIfFunc) Stream(ctx context.Context) error { |
| 141 | + // XXX: is there a sync.Once sort of solution that would be more elegant here? |
| 142 | + mutex := &sync.Mutex{} |
| 143 | + done := false |
| 144 | + send := func(ctx context.Context, b bool) error { |
| 145 | + mutex.Lock() |
| 146 | + defer mutex.Unlock() |
| 147 | + if done { |
| 148 | + return nil |
| 149 | + } |
| 150 | + done = true |
| 151 | + defer close(obj.init.Output) // the sender closes |
| 152 | + |
| 153 | + if !b { |
| 154 | + return nil |
| 155 | + } |
| 156 | + |
| 157 | + // send dummy value to the output |
| 158 | + select { |
| 159 | + case obj.init.Output <- types.NewFloat(): // XXX: dummy value |
| 160 | + case <-ctx.Done(): |
| 161 | + return ctx.Err() |
| 162 | + } |
| 163 | + |
| 164 | + return nil |
| 165 | + } |
| 166 | + defer send(ctx, false) // just close |
| 167 | + |
| 168 | + defer func() { |
| 169 | + obj.init.Txn.Reverse() |
| 170 | + }() |
| 171 | + |
| 172 | + for { |
| 173 | + select { |
| 174 | + case input, ok := <-obj.init.Input: |
| 175 | + if !ok { |
| 176 | + obj.init.Input = nil // block looping back here |
| 177 | + if !done { |
| 178 | + return fmt.Errorf("input closed without ever sending anything") |
| 179 | + } |
| 180 | + return nil |
| 181 | + } |
| 182 | + |
| 183 | + value, exists := input.Struct()[obj.EdgeName] |
| 184 | + if !exists { |
| 185 | + return fmt.Errorf("programming error, can't find edge") |
| 186 | + } |
| 187 | + |
| 188 | + b := value.Bool() |
| 189 | + |
| 190 | + if obj.last != nil && *obj.last == b { |
| 191 | + continue // result didn't change |
| 192 | + } |
| 193 | + obj.last = &b // store new result |
| 194 | + |
| 195 | + if err := obj.replaceSubGraph(b); err != nil { |
| 196 | + return errwrap.Wrapf(err, "could not replace subgraph") |
| 197 | + } |
| 198 | + |
| 199 | + send(ctx, true) // send dummy and then close |
| 200 | + |
| 201 | + continue |
| 202 | + |
| 203 | + case <-ctx.Done(): |
| 204 | + return nil |
| 205 | + } |
| 206 | + } |
| 207 | +} |
| 208 | + |
| 209 | +func (obj *ExprIfFunc) replaceSubGraph(b bool) error { |
| 210 | + // delete the old subgraph |
| 211 | + if err := obj.init.Txn.Reverse(); err != nil { |
| 212 | + return errwrap.Wrapf(err, "could not Reverse") |
| 213 | + } |
| 214 | + |
| 215 | + var f interfaces.Func |
| 216 | + if b { |
| 217 | + obj.init.Txn.AddGraph(obj.ThenGraph) |
| 218 | + f = obj.ThenFunc |
| 219 | + } else { |
| 220 | + obj.init.Txn.AddGraph(obj.ElseGraph) |
| 221 | + f = obj.ElseFunc |
| 222 | + } |
| 223 | + |
| 224 | + // create the new subgraph |
| 225 | + edgeName := OutputFuncArgName |
| 226 | + edge := &interfaces.FuncEdge{Args: []string{edgeName}} |
| 227 | + obj.init.Txn.AddVertex(f) |
| 228 | + obj.init.Txn.AddEdge(f, obj.OutputVertex, edge) |
| 229 | + |
| 230 | + return obj.init.Txn.Commit() |
| 231 | +} |
0 commit comments