github.com/myhau/pulumi/pkg/v3@v3.70.2-0.20221116134521-f2775972e587/codegen/hcl2/model/diagnostics.go (about) 1 // Copyright 2016-2020, Pulumi Corporation. 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 model 16 17 import ( 18 "fmt" 19 20 "github.com/hashicorp/hcl/v2" 21 "github.com/zclconf/go-cty/cty" 22 ) 23 24 func errorf(subject hcl.Range, f string, args ...interface{}) *hcl.Diagnostic { 25 return diagf(hcl.DiagError, subject, f, args...) 26 } 27 28 func diagf(severity hcl.DiagnosticSeverity, subject hcl.Range, f string, args ...interface{}) *hcl.Diagnostic { 29 message := fmt.Sprintf(f, args...) 30 return &hcl.Diagnostic{ 31 Severity: severity, 32 Summary: message, 33 Subject: &subject, 34 } 35 } 36 37 func ExprNotConvertible(destType Type, expr Expression) *hcl.Diagnostic { 38 _, whyF := destType.conversionFrom(expr.Type(), false, map[Type]struct{}{}) 39 why := whyF() 40 if len(why) != 0 { 41 return errorf(expr.SyntaxNode().Range(), why[0].Summary) 42 } 43 return errorf(expr.SyntaxNode().Range(), "cannot assign expression of type %v to location of type %v: ", expr.Type(), 44 destType) 45 } 46 47 func typeNotConvertible(dest, src Type) *hcl.Diagnostic { 48 return &hcl.Diagnostic{Severity: hcl.DiagError, Summary: fmt.Sprintf("cannot assign value of type %v to type %v", 49 src, dest)} 50 } 51 52 func tuplesHaveDifferentLengths(dest, src *TupleType) *hcl.Diagnostic { 53 return &hcl.Diagnostic{Severity: hcl.DiagError, Summary: fmt.Sprintf("tuples %v and %v have different lengths", 54 dest, src)} 55 } 56 57 func invalidRecursiveType(t Type) *hcl.Diagnostic { 58 return errorf(t.SyntaxNode().Range(), "invalid recursive type") 59 } 60 61 func objectKeysMustBeStrings(expr Expression) *hcl.Diagnostic { 62 return errorf(expr.SyntaxNode().Range(), 63 "object keys must be strings: cannot assign expression of type %v to location of type string", expr.Type()) 64 } 65 66 func unsupportedLiteralValue(val cty.Value, valRange hcl.Range) *hcl.Diagnostic { 67 return errorf(valRange, "unsupported literal value of type %v", val.Type()) 68 } 69 70 func unknownFunction(name string, nameRange hcl.Range) *hcl.Diagnostic { 71 return errorf(nameRange, "unknown function '%s'", name) 72 } 73 74 func missingRequiredArgument(param Parameter, callRange hcl.Range) *hcl.Diagnostic { 75 return errorf(callRange, "missing required parameter '%s'", param.Name) 76 } 77 78 func extraArguments(expected, actual int, callRange hcl.Range) *hcl.Diagnostic { 79 return errorf(callRange, "too many arguments to call: expected %v, got %v", expected, actual) 80 } 81 82 func unsupportedMapKey(keyRange hcl.Range) *hcl.Diagnostic { 83 return errorf(keyRange, "map keys must be strings") 84 } 85 86 func unsupportedListIndex(indexRange hcl.Range) *hcl.Diagnostic { 87 return errorf(indexRange, "list indices must be numbers") 88 } 89 90 func unsupportedTupleIndex(indexRange hcl.Range) *hcl.Diagnostic { 91 return errorf(indexRange, "tuple indices must be integers") 92 } 93 94 func unsupportedObjectProperty(indexRange hcl.Range) *hcl.Diagnostic { 95 return errorf(indexRange, "object properties must be strings") 96 } 97 98 func tupleIndexOutOfRange(tupleLen int, indexRange hcl.Range) *hcl.Diagnostic { 99 return errorf(indexRange, "tuple index must be between 0 and %d", tupleLen) 100 } 101 102 func unknownObjectProperty(name string, indexRange hcl.Range, props []string) *hcl.Diagnostic { 103 return errorf(indexRange, "unknown property '%s' among %v", name, props) 104 } 105 106 func unsupportedReceiverType(receiver Type, indexRange hcl.Range) *hcl.Diagnostic { 107 return errorf(indexRange, "cannot traverse value of type %v", receiver) 108 } 109 110 func unsupportedCollectionType(collectionType Type, iteratorRange hcl.Range) *hcl.Diagnostic { 111 return errorf(iteratorRange, "cannot iterate over a value of type %v", collectionType) 112 } 113 114 func undefinedVariable(variableName string, variableRange hcl.Range) *hcl.Diagnostic { 115 return errorf(variableRange, fmt.Sprintf("undefined variable %v", variableName)) 116 } 117 118 func internalError(rng hcl.Range, fmt string, args ...interface{}) *hcl.Diagnostic { 119 return errorf(rng, "Internal error: "+fmt, args...) 120 } 121 122 func nameAlreadyDefined(name string, rng hcl.Range) *hcl.Diagnostic { 123 return errorf(rng, "name %v already defined", name) 124 } 125 126 func cannotTraverseKeyword(name string, rng hcl.Range) *hcl.Diagnostic { 127 return errorf(rng, "'%s' is a keyword and cannot be traversed", name) 128 } 129 130 func cannotTraverseFunction(rng hcl.Range) *hcl.Diagnostic { 131 return errorf(rng, "functions cannot be traversed") 132 } 133 134 func cannotEvaluateAnonymousFunctionExpressions() *hcl.Diagnostic { 135 return &hcl.Diagnostic{ 136 Severity: hcl.DiagError, 137 Summary: "cannot evaluate anonymous function expressions", 138 } 139 }