101 lines
2.9 KiB
Go
101 lines
2.9 KiB
Go
package model
|
|
|
|
import (
|
|
"github.com/hashicorp/hcl/v2"
|
|
"github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/syntax"
|
|
)
|
|
|
|
var typeBuiltins = map[string]Type{
|
|
"string": StringType,
|
|
"number": NumberType,
|
|
"int": IntType,
|
|
"bool": BoolType,
|
|
}
|
|
|
|
var typeFunctions = map[string]FunctionSignature{
|
|
"list": GenericFunctionSignature(func(args []Expression) (StaticFunctionSignature, hcl.Diagnostics) {
|
|
resultType := Type(DynamicType)
|
|
if len(args) == 1 {
|
|
resultType = NewListType(args[0].Type())
|
|
}
|
|
return StaticFunctionSignature{
|
|
Parameters: []Parameter{{Name: "elementType", Type: DynamicType}},
|
|
ReturnType: resultType,
|
|
}, nil
|
|
}),
|
|
"set": GenericFunctionSignature(func(args []Expression) (StaticFunctionSignature, hcl.Diagnostics) {
|
|
resultType := Type(DynamicType)
|
|
if len(args) == 1 {
|
|
resultType = NewSetType(args[0].Type())
|
|
}
|
|
return StaticFunctionSignature{
|
|
Parameters: []Parameter{{Name: "elementType", Type: DynamicType}},
|
|
ReturnType: resultType,
|
|
}, nil
|
|
}),
|
|
"map": GenericFunctionSignature(func(args []Expression) (StaticFunctionSignature, hcl.Diagnostics) {
|
|
resultType := Type(DynamicType)
|
|
if len(args) == 1 {
|
|
resultType = NewMapType(args[0].Type())
|
|
}
|
|
return StaticFunctionSignature{
|
|
Parameters: []Parameter{{Name: "elementType", Type: DynamicType}},
|
|
ReturnType: resultType,
|
|
}, nil
|
|
}),
|
|
"object": GenericFunctionSignature(func(args []Expression) (StaticFunctionSignature, hcl.Diagnostics) {
|
|
var diagnostics hcl.Diagnostics
|
|
resultType := Type(DynamicType)
|
|
if len(args) == 1 {
|
|
if _, isObjectType := args[0].Type().(*ObjectType); isObjectType {
|
|
resultType = args[0].Type()
|
|
} else {
|
|
rng := args[0].SyntaxNode().Range()
|
|
diagnostics = hcl.Diagnostics{{
|
|
Severity: hcl.DiagError,
|
|
Summary: "the argument to object() must be an object type",
|
|
Subject: &rng,
|
|
}}
|
|
}
|
|
}
|
|
return StaticFunctionSignature{
|
|
Parameters: []Parameter{{Name: "objectType", Type: DynamicType}},
|
|
ReturnType: resultType,
|
|
}, diagnostics
|
|
}),
|
|
"tuple": GenericFunctionSignature(func(args []Expression) (StaticFunctionSignature, hcl.Diagnostics) {
|
|
var diagnostics hcl.Diagnostics
|
|
resultType := Type(DynamicType)
|
|
if len(args) == 1 {
|
|
if _, isTupleType := args[0].Type().(*TupleType); isTupleType {
|
|
resultType = args[0].Type()
|
|
} else {
|
|
rng := args[0].SyntaxNode().Range()
|
|
diagnostics = hcl.Diagnostics{{
|
|
Severity: hcl.DiagError,
|
|
Summary: "the argument to tuple() must be an tuple type",
|
|
Subject: &rng,
|
|
}}
|
|
}
|
|
}
|
|
return StaticFunctionSignature{
|
|
Parameters: []Parameter{{Name: "tupleType", Type: DynamicType}},
|
|
ReturnType: resultType,
|
|
}, diagnostics
|
|
}),
|
|
}
|
|
|
|
var TypeScope *Scope
|
|
|
|
func init() {
|
|
TypeScope = NewRootScope(syntax.None)
|
|
for name, typ := range typeBuiltins {
|
|
TypeScope.Define(name, &Variable{
|
|
Name: name,
|
|
VariableType: typ,
|
|
})
|
|
}
|
|
for name, sig := range typeFunctions {
|
|
TypeScope.DefineFunction(name, NewFunction(sig))
|
|
}
|
|
}
|