GetVariable traverses the stack (starting from the top) to find a variable with a matching name. Returns nil if no variable was found.
(name string)
| 108 | // GetVariable traverses the stack (starting from the top) to find a variable with a matching name. Returns nil if no |
| 109 | // variable was found. |
| 110 | func (is *InterpreterStack) GetVariable(name string) InterpreterVariableReference { |
| 111 | // TODO: handle nested record access |
| 112 | fieldName := "" |
| 113 | if strings.Count(name, ".") == 1 { |
| 114 | splitName := strings.Split(name, ".") |
| 115 | name = splitName[0] |
| 116 | fieldName = splitName[1] |
| 117 | } |
| 118 | for i := 0; i < is.stack.Len(); i++ { |
| 119 | if iv, ok := is.stack.PeekDepth(i).variables[name]; ok { |
| 120 | if len(fieldName) == 0 { |
| 121 | return InterpreterVariableReference{ |
| 122 | Type: iv.Type, |
| 123 | Value: &iv.Value, |
| 124 | } |
| 125 | } else if len(iv.Record) > 0 { |
| 126 | fieldIdx := iv.Record.IndexOf(fieldName, iv.Record[0].Source) |
| 127 | if fieldIdx == -1 { |
| 128 | // TODO: implement this as a proper error for missing record field rather than the generic "variable not found" |
| 129 | return InterpreterVariableReference{} |
| 130 | } |
| 131 | return InterpreterVariableReference{ |
| 132 | Type: iv.Record[fieldIdx].Type.(*pgtypes.DoltgresType), |
| 133 | Value: &(iv.Value.(sql.Row)[fieldIdx]), |
| 134 | } |
| 135 | } else if iv.Type.IsCompositeType() { |
| 136 | for fieldIdx := range iv.Type.CompositeAttrs { |
| 137 | if iv.Type.CompositeAttrs[fieldIdx].Name == fieldName { |
| 138 | vals := iv.Value.([]pgtypes.RecordValue) |
| 139 | return InterpreterVariableReference{ |
| 140 | Type: vals[fieldIdx].Type.(*pgtypes.DoltgresType), |
| 141 | Value: &(vals[fieldIdx].Value), |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | // The field could not be found |
| 146 | return InterpreterVariableReference{} |
| 147 | } else { |
| 148 | // Can't access fields on an empty record |
| 149 | return InterpreterVariableReference{} |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | return InterpreterVariableReference{} |
| 154 | } |
| 155 | |
| 156 | // ListVariables returns a map with the names of all variables. The attached slice represents field names for records. |
| 157 | // All names are lowercased. |
no test coverage detected