-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresult.go
129 lines (118 loc) · 4.28 KB
/
result.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
119
120
121
122
123
124
125
126
127
128
129
package dynamo
//----------------------------------------------------------------------
// This file is part of Dynamo.
// Copyright (C) 2011-2020 Bernd Fix
//
// Dynamo is free software: you can redistribute it and/or modify it
// under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// Dynamo is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: AGPL3.0-or-later
//----------------------------------------------------------------------
import (
"fmt"
"strings"
)
// DYNAMO error messages
const (
ErrModelDependencyLoop = "Equations have cyclic dependencies"
ErrModelEqnBadTargetKind = "Wrong kind of equation target"
ErrModelEqnBadTargetStage = "Wrong stage for equation target"
ErrModelEqnBadDependClass = "Wrong class (kind/stage) of equation dependency"
ErrModelEqnBadMode = "Wrong mode for equation"
ErrModelEqnOverwrite = "Equation overwrite"
ErrModelEqnAmbigious = "Ambigious equation"
ErrModelUnknownEqn = "No defining equation for variable found"
ErrModelUnknownFunction = "Unknown function call"
ErrModelFunctionArg = "Invalid function argument"
ErrModelNoVariable = "No variable found"
ErrModelVariabeExists = "Variable already known"
ErrModelNoSuchTable = "No such table"
ErrModelWrongTableSize = "Tabe size mismatch"
ErrModelNoTime = "No TIME defined"
ErrModelMaxRetry = "Retry limit reached"
ErrModelMissingDef = "Missing definition of value"
ErrModelNoData = "No data available"
ErrModelFunction = "Error in function"
ErrModelNotAvailable = "Model equations not available"
ErrModelNoInitial = "No initial value"
ErrParseLineLength = "Line too long"
ErrParseInvalidSpace = "Space in equation"
ErrParseInvalidMode = "Line does not start with a valid mode"
ErrParseInvalidName = "Invalid variable name"
ErrParseInvalidIndex = "Invalid variable index"
ErrParseNameLength = "Variable name too long"
ErrParseSyntax = "Syntax error"
ErrParseInvalidOp = "Unknown operand"
ErrParseTableTooSmall = "Not enough table elements"
ErrParseUnknownFunction = "Unknown function"
ErrParseInvalidNumArgs = "Invalid number of arguments"
ErrParseMacroDepth = "Invalid nesting for macro function"
ErrParseNotANumber = "Not a number"
ErrPlotRange = "Range failure"
ErrPlotNoVar = "Not a plot variable"
ErrPlotMode = "No such plotter mode"
ErrPrintNoVar = "Not a print variable"
ErrPrintMode = "No such plotter mode"
)
// Result represents the response of a method call in the Dynamo framework.
// It allows to track failures with more information than 'error' alone
// provides.
type Result struct {
Ok bool // call returned without problems
Err error // error (if !Ok)
Line int // line number in input stream (0 if not parsing)
Ctx interface{} // Optional failure context
}
// Success is used if the call finishes without problems
func Success() *Result {
return &Result{
Ok: true,
Err: nil,
Line: 0,
Ctx: nil,
}
}
// Failure returns a result for a failed operation. The parameter can be
// of type 'string' or 'error'.
func Failure(err interface{}, args ...interface{}) *Result {
var e error = nil
switch x := err.(type) {
case error:
e = x
case string:
if len(args) > 0 {
e = fmt.Errorf(x, args...)
} else {
e = fmt.Errorf(x)
}
}
return &Result{
Ok: false,
Err: e,
Line: 0,
Ctx: nil,
}
}
// SetLine should be used by methods that are involved with parsing
// DYNAMO source code to report the problematic line in the input stream.
func (r *Result) SetLine(line int) *Result {
r.Line = line
return r
}
// IsA returns true if the given (failure) result is of given error
func (r *Result) IsA(err string) bool {
if r.Err == nil {
return false
}
return strings.HasPrefix(r.Err.Error(), err)
}