/
diagnostics.go
147 lines (118 loc) 路 4.99 KB
/
diagnostics.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2016-2020, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package model
import (
"fmt"
"github.com/hashicorp/hcl/v2"
"github.com/zclconf/go-cty/cty"
)
func errorf(subject hcl.Range, f string, args ...interface{}) *hcl.Diagnostic {
return diagf(hcl.DiagError, subject, f, args...)
}
func warnf(subject hcl.Range, f string, args ...interface{}) *hcl.Diagnostic {
return diagf(hcl.DiagWarning, subject, f, args...)
}
func diagf(severity hcl.DiagnosticSeverity, subject hcl.Range, f string, args ...interface{}) *hcl.Diagnostic {
message := fmt.Sprintf(f, args...)
return &hcl.Diagnostic{
Severity: severity,
Summary: message,
Subject: &subject,
}
}
func ExprNotConvertible(destType Type, expr Expression) *hcl.Diagnostic {
_, whyF := destType.conversionFrom(expr.Type(), false, map[Type]struct{}{})
why := whyF()
if len(why) != 0 {
return errorf(expr.SyntaxNode().Range(), why[0].Summary)
}
return errorf(expr.SyntaxNode().Range(), "cannot assign expression of type %s to location of type %s: ",
expr.Type().Pretty(), destType.Pretty())
}
func typeNotConvertible(dest, src Type) *hcl.Diagnostic {
return &hcl.Diagnostic{Severity: hcl.DiagError, Summary: fmt.Sprintf("cannot assign value of type %s to type %s",
src.Pretty(), dest.Pretty())}
}
func tuplesHaveDifferentLengths(dest, src *TupleType) *hcl.Diagnostic {
return &hcl.Diagnostic{Severity: hcl.DiagError, Summary: fmt.Sprintf("tuples %v and %v have different lengths",
dest, src)}
}
func invalidRecursiveType(t Type) *hcl.Diagnostic {
return errorf(t.SyntaxNode().Range(), "invalid recursive type")
}
func objectKeysMustBeStrings(expr Expression) *hcl.Diagnostic {
return errorf(expr.SyntaxNode().Range(),
"object keys must be strings: cannot assign expression of type %v to location of type string", expr.Type())
}
func unsupportedLiteralValue(val cty.Value, valRange hcl.Range) *hcl.Diagnostic {
return errorf(valRange, "unsupported literal value of type %v", val.Type())
}
func unknownFunction(name string, nameRange hcl.Range) *hcl.Diagnostic {
return errorf(nameRange, "unknown function '%s'", name)
}
func missingRequiredArgument(param Parameter, callRange hcl.Range) *hcl.Diagnostic {
return errorf(callRange, "missing required parameter '%s'", param.Name)
}
func extraArguments(expected, actual int, callRange hcl.Range) *hcl.Diagnostic {
return errorf(callRange, "too many arguments to call: expected %v, got %v", expected, actual)
}
func unsupportedMapKey(keyRange hcl.Range) *hcl.Diagnostic {
return errorf(keyRange, "map keys must be strings")
}
func unsupportedListIndex(indexRange hcl.Range) *hcl.Diagnostic {
return errorf(indexRange, "list indices must be numbers")
}
func unsupportedTupleIndex(indexRange hcl.Range) *hcl.Diagnostic {
return errorf(indexRange, "tuple indices must be integers")
}
func unsupportedObjectProperty(indexRange hcl.Range) *hcl.Diagnostic {
return errorf(indexRange, "object properties must be strings")
}
func tupleIndexOutOfRange(tupleLen int, indexRange hcl.Range) *hcl.Diagnostic {
return errorf(indexRange, "tuple index must be between 0 and %d", tupleLen)
}
func unknownObjectProperty(name string, indexRange hcl.Range, props []string) *hcl.Diagnostic {
return errorf(indexRange, "unknown property '%s' among %v", name, props)
}
func unsupportedReceiverType(receiver Type, indexRange hcl.Range) *hcl.Diagnostic {
return errorf(indexRange, "cannot traverse value of type %v", receiver)
}
func unsupportedCollectionType(collectionType Type, iteratorRange hcl.Range) *hcl.Diagnostic {
return errorf(iteratorRange, "cannot iterate over a value of type %v", collectionType)
}
func undefinedVariable(variableName string, variableRange hcl.Range, warn bool) *hcl.Diagnostic {
f := errorf
if warn {
f = warnf
}
return f(variableRange, "undefined variable %v", variableName)
}
func internalError(rng hcl.Range, fmt string, args ...interface{}) *hcl.Diagnostic {
return errorf(rng, "Internal error: "+fmt, args...)
}
func nameAlreadyDefined(name string, rng hcl.Range) *hcl.Diagnostic {
return errorf(rng, "name %v already defined", name)
}
func cannotTraverseKeyword(name string, rng hcl.Range) *hcl.Diagnostic {
return errorf(rng, "'%s' is a keyword and cannot be traversed", name)
}
func cannotTraverseFunction(rng hcl.Range) *hcl.Diagnostic {
return errorf(rng, "functions cannot be traversed")
}
func cannotEvaluateAnonymousFunctionExpressions() *hcl.Diagnostic {
return &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "cannot evaluate anonymous function expressions",
}
}