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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
|
package gocty
import (
"math/big"
"reflect"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/convert"
"github.com/zclconf/go-cty/cty/set"
)
// ToCtyValue produces a cty.Value from a Go value. The result will conform
// to the given type, or an error will be returned if this is not possible.
//
// The target type serves as a hint to resolve ambiguities in the mapping.
// For example, the Go type set.Set tells us that the value is a set but
// does not describe the set's element type. This also allows for convenient
// conversions, such as populating a set from a slice rather than having to
// first explicitly instantiate a set.Set.
//
// The audience of this function is assumed to be the developers of Go code
// that is integrating with cty, and thus the error messages it returns are
// presented from Go's perspective. These messages are thus not appropriate
// for display to end-users. An error returned from ToCtyValue represents a
// bug in the calling program, not user error.
func ToCtyValue(val interface{}, ty cty.Type) (cty.Value, error) {
// 'path' starts off as empty but will grow for each level of recursive
// call we make, so by the time toCtyValue returns it is likely to have
// unused capacity on the end of it, depending on how deeply-recursive
// the given Type is.
path := make(cty.Path, 0)
return toCtyValue(reflect.ValueOf(val), ty, path)
}
func toCtyValue(val reflect.Value, ty cty.Type, path cty.Path) (cty.Value, error) {
if val != (reflect.Value{}) && val.Type().AssignableTo(valueType) {
// If the source value is a cty.Value then we'll try to just pass
// through to the target type directly.
return toCtyPassthrough(val, ty, path)
}
switch ty {
case cty.Bool:
return toCtyBool(val, path)
case cty.Number:
return toCtyNumber(val, path)
case cty.String:
return toCtyString(val, path)
case cty.DynamicPseudoType:
return toCtyDynamic(val, path)
}
switch {
case ty.IsListType():
return toCtyList(val, ty.ElementType(), path)
case ty.IsMapType():
return toCtyMap(val, ty.ElementType(), path)
case ty.IsSetType():
return toCtySet(val, ty.ElementType(), path)
case ty.IsObjectType():
return toCtyObject(val, ty.AttributeTypes(), path)
case ty.IsTupleType():
return toCtyTuple(val, ty.TupleElementTypes(), path)
case ty.IsCapsuleType():
return toCtyCapsule(val, ty, path)
}
// We should never fall out here
return cty.NilVal, path.NewErrorf("unsupported target type %#v", ty)
}
func toCtyBool(val reflect.Value, path cty.Path) (cty.Value, error) {
if val = toCtyUnwrapPointer(val); !val.IsValid() {
return cty.NullVal(cty.Bool), nil
}
switch val.Kind() {
case reflect.Bool:
return cty.BoolVal(val.Bool()), nil
default:
return cty.NilVal, path.NewErrorf("can't convert Go %s to bool", val.Kind())
}
}
func toCtyNumber(val reflect.Value, path cty.Path) (cty.Value, error) {
if val = toCtyUnwrapPointer(val); !val.IsValid() {
return cty.NullVal(cty.Number), nil
}
switch val.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return cty.NumberIntVal(val.Int()), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return cty.NumberUIntVal(val.Uint()), nil
case reflect.Float32, reflect.Float64:
return cty.NumberFloatVal(val.Float()), nil
case reflect.Struct:
if val.Type().AssignableTo(bigIntType) {
bigInt := val.Interface().(big.Int)
bigFloat := (&big.Float{}).SetInt(&bigInt)
val = reflect.ValueOf(*bigFloat)
}
if val.Type().AssignableTo(bigFloatType) {
bigFloat := val.Interface().(big.Float)
return cty.NumberVal(&bigFloat), nil
}
fallthrough
default:
return cty.NilVal, path.NewErrorf("can't convert Go %s to number", val.Kind())
}
}
func toCtyString(val reflect.Value, path cty.Path) (cty.Value, error) {
if val = toCtyUnwrapPointer(val); !val.IsValid() {
return cty.NullVal(cty.String), nil
}
switch val.Kind() {
case reflect.String:
return cty.StringVal(val.String()), nil
default:
return cty.NilVal, path.NewErrorf("can't convert Go %s to string", val.Kind())
}
}
func toCtyList(val reflect.Value, ety cty.Type, path cty.Path) (cty.Value, error) {
if val = toCtyUnwrapPointer(val); !val.IsValid() {
return cty.NullVal(cty.List(ety)), nil
}
switch val.Kind() {
case reflect.Slice:
if val.IsNil() {
return cty.NullVal(cty.List(ety)), nil
}
fallthrough
case reflect.Array:
if val.Len() == 0 {
return cty.ListValEmpty(ety), nil
}
// While we work on our elements we'll temporarily grow
// path to give us a place to put our index step.
path = append(path, cty.PathStep(nil))
vals := make([]cty.Value, val.Len())
for i := range vals {
var err error
path[len(path)-1] = cty.IndexStep{
Key: cty.NumberIntVal(int64(i)),
}
vals[i], err = toCtyValue(val.Index(i), ety, path)
if err != nil {
return cty.NilVal, err
}
}
// Discard our extra path segment, retaining it as extra capacity
// for future appending to the path.
path = path[:len(path)-1]
return cty.ListVal(vals), nil
default:
return cty.NilVal, path.NewErrorf("can't convert Go %s to %#v", val.Kind(), cty.List(ety))
}
}
func toCtyMap(val reflect.Value, ety cty.Type, path cty.Path) (cty.Value, error) {
if val = toCtyUnwrapPointer(val); !val.IsValid() {
return cty.NullVal(cty.Map(ety)), nil
}
switch val.Kind() {
case reflect.Map:
if val.IsNil() {
return cty.NullVal(cty.Map(ety)), nil
}
if val.Len() == 0 {
return cty.MapValEmpty(ety), nil
}
keyType := val.Type().Key()
if keyType.Kind() != reflect.String {
return cty.NilVal, path.NewErrorf("can't convert Go map with key type %s; key type must be string", keyType)
}
// While we work on our elements we'll temporarily grow
// path to give us a place to put our index step.
path = append(path, cty.PathStep(nil))
vals := make(map[string]cty.Value, val.Len())
for _, kv := range val.MapKeys() {
k := kv.String()
var err error
path[len(path)-1] = cty.IndexStep{
Key: cty.StringVal(k),
}
vals[k], err = toCtyValue(val.MapIndex(reflect.ValueOf(k)), ety, path)
if err != nil {
return cty.NilVal, err
}
}
// Discard our extra path segment, retaining it as extra capacity
// for future appending to the path.
path = path[:len(path)-1]
return cty.MapVal(vals), nil
default:
return cty.NilVal, path.NewErrorf("can't convert Go %s to %#v", val.Kind(), cty.Map(ety))
}
}
func toCtySet(val reflect.Value, ety cty.Type, path cty.Path) (cty.Value, error) {
if val = toCtyUnwrapPointer(val); !val.IsValid() {
return cty.NullVal(cty.Set(ety)), nil
}
var vals []cty.Value
switch val.Kind() {
case reflect.Slice:
if val.IsNil() {
return cty.NullVal(cty.Set(ety)), nil
}
fallthrough
case reflect.Array:
if val.Len() == 0 {
return cty.SetValEmpty(ety), nil
}
vals = make([]cty.Value, val.Len())
for i := range vals {
var err error
vals[i], err = toCtyValue(val.Index(i), ety, path)
if err != nil {
return cty.NilVal, err
}
}
case reflect.Struct:
if !val.Type().AssignableTo(setType) {
return cty.NilVal, path.NewErrorf("can't convert Go %s to %#v", val.Type(), cty.Set(ety))
}
rawSet := val.Interface().(set.Set[interface{}])
inVals := rawSet.Values()
if len(inVals) == 0 {
return cty.SetValEmpty(ety), nil
}
vals = make([]cty.Value, len(inVals))
for i := range inVals {
var err error
vals[i], err = toCtyValue(reflect.ValueOf(inVals[i]), ety, path)
if err != nil {
return cty.NilVal, err
}
}
default:
return cty.NilVal, path.NewErrorf("can't convert Go %s to %#v", val.Kind(), cty.Set(ety))
}
return cty.SetVal(vals), nil
}
func toCtyObject(val reflect.Value, attrTypes map[string]cty.Type, path cty.Path) (cty.Value, error) {
if val = toCtyUnwrapPointer(val); !val.IsValid() {
return cty.NullVal(cty.Object(attrTypes)), nil
}
switch val.Kind() {
case reflect.Map:
if val.IsNil() {
return cty.NullVal(cty.Object(attrTypes)), nil
}
keyType := val.Type().Key()
if keyType.Kind() != reflect.String {
return cty.NilVal, path.NewErrorf("can't convert Go map with key type %s; key type must be string", keyType)
}
if len(attrTypes) == 0 {
return cty.EmptyObjectVal, nil
}
// While we work on our elements we'll temporarily grow
// path to give us a place to put our GetAttr step.
path = append(path, cty.PathStep(nil))
haveKeys := make(map[string]struct{}, val.Len())
for _, kv := range val.MapKeys() {
haveKeys[kv.String()] = struct{}{}
}
vals := make(map[string]cty.Value, len(attrTypes))
for k, at := range attrTypes {
var err error
path[len(path)-1] = cty.GetAttrStep{
Name: k,
}
if _, have := haveKeys[k]; !have {
vals[k] = cty.NullVal(at)
continue
}
vals[k], err = toCtyValue(val.MapIndex(reflect.ValueOf(k)), at, path)
if err != nil {
return cty.NilVal, err
}
}
// Discard our extra path segment, retaining it as extra capacity
// for future appending to the path.
path = path[:len(path)-1]
return cty.ObjectVal(vals), nil
case reflect.Struct:
if len(attrTypes) == 0 {
return cty.EmptyObjectVal, nil
}
// While we work on our elements we'll temporarily grow
// path to give us a place to put our GetAttr step.
path = append(path, cty.PathStep(nil))
attrFields := structTagIndices(val.Type())
vals := make(map[string]cty.Value, len(attrTypes))
for k, at := range attrTypes {
path[len(path)-1] = cty.GetAttrStep{
Name: k,
}
if fieldIdx, have := attrFields[k]; have {
var err error
vals[k], err = toCtyValue(val.Field(fieldIdx), at, path)
if err != nil {
return cty.NilVal, err
}
} else {
vals[k] = cty.NullVal(at)
}
}
// Discard our extra path segment, retaining it as extra capacity
// for future appending to the path.
path = path[:len(path)-1]
return cty.ObjectVal(vals), nil
default:
return cty.NilVal, path.NewErrorf("can't convert Go %s to %#v", val.Kind(), cty.Object(attrTypes))
}
}
func toCtyTuple(val reflect.Value, elemTypes []cty.Type, path cty.Path) (cty.Value, error) {
if val = toCtyUnwrapPointer(val); !val.IsValid() {
return cty.NullVal(cty.Tuple(elemTypes)), nil
}
switch val.Kind() {
case reflect.Slice:
if val.IsNil() {
return cty.NullVal(cty.Tuple(elemTypes)), nil
}
if val.Len() != len(elemTypes) {
return cty.NilVal, path.NewErrorf("wrong number of elements %d; need %d", val.Len(), len(elemTypes))
}
if len(elemTypes) == 0 {
return cty.EmptyTupleVal, nil
}
// While we work on our elements we'll temporarily grow
// path to give us a place to put our Index step.
path = append(path, cty.PathStep(nil))
vals := make([]cty.Value, len(elemTypes))
for i, ety := range elemTypes {
var err error
path[len(path)-1] = cty.IndexStep{
Key: cty.NumberIntVal(int64(i)),
}
vals[i], err = toCtyValue(val.Index(i), ety, path)
if err != nil {
return cty.NilVal, err
}
}
// Discard our extra path segment, retaining it as extra capacity
// for future appending to the path.
path = path[:len(path)-1]
return cty.TupleVal(vals), nil
case reflect.Struct:
fieldCount := val.Type().NumField()
if fieldCount != len(elemTypes) {
return cty.NilVal, path.NewErrorf("wrong number of struct fields %d; need %d", fieldCount, len(elemTypes))
}
if len(elemTypes) == 0 {
return cty.EmptyTupleVal, nil
}
// While we work on our elements we'll temporarily grow
// path to give us a place to put our Index step.
path = append(path, cty.PathStep(nil))
vals := make([]cty.Value, len(elemTypes))
for i, ety := range elemTypes {
var err error
path[len(path)-1] = cty.IndexStep{
Key: cty.NumberIntVal(int64(i)),
}
vals[i], err = toCtyValue(val.Field(i), ety, path)
if err != nil {
return cty.NilVal, err
}
}
// Discard our extra path segment, retaining it as extra capacity
// for future appending to the path.
path = path[:len(path)-1]
return cty.TupleVal(vals), nil
default:
return cty.NilVal, path.NewErrorf("can't convert Go %s to %#v", val.Kind(), cty.Tuple(elemTypes))
}
}
func toCtyCapsule(val reflect.Value, capsuleType cty.Type, path cty.Path) (cty.Value, error) {
if val = toCtyUnwrapPointer(val); !val.IsValid() {
return cty.NullVal(capsuleType), nil
}
if val.Kind() != reflect.Ptr {
if !val.CanAddr() {
return cty.NilVal, path.NewErrorf("source value for capsule %#v must be addressable", capsuleType)
}
val = val.Addr()
}
if !val.Type().Elem().AssignableTo(capsuleType.EncapsulatedType()) {
return cty.NilVal, path.NewErrorf("value of type %T not compatible with capsule %#v", val.Interface(), capsuleType)
}
return cty.CapsuleVal(capsuleType, val.Interface()), nil
}
func toCtyDynamic(val reflect.Value, path cty.Path) (cty.Value, error) {
if val = toCtyUnwrapPointer(val); !val.IsValid() {
return cty.NullVal(cty.DynamicPseudoType), nil
}
switch val.Kind() {
case reflect.Struct:
if !val.Type().AssignableTo(valueType) {
return cty.NilVal, path.NewErrorf("can't convert Go %s dynamically; only cty.Value allowed", val.Type())
}
return val.Interface().(cty.Value), nil
default:
return cty.NilVal, path.NewErrorf("can't convert Go %s dynamically; only cty.Value allowed", val.Kind())
}
}
func toCtyPassthrough(wrappedVal reflect.Value, wantTy cty.Type, path cty.Path) (cty.Value, error) {
if wrappedVal = toCtyUnwrapPointer(wrappedVal); !wrappedVal.IsValid() {
return cty.NullVal(wantTy), nil
}
givenVal := wrappedVal.Interface().(cty.Value)
val, err := convert.Convert(givenVal, wantTy)
if err != nil {
return cty.NilVal, path.NewErrorf("unsuitable value: %s", err)
}
return val, nil
}
// toCtyUnwrapPointer is a helper for dealing with Go pointers. It has three
// possible outcomes:
//
// - Given value isn't a pointer, so it's just returned as-is.
// - Given value is a non-nil pointer, in which case it is dereferenced
// and the result returned.
// - Given value is a nil pointer, in which case an invalid value is returned.
//
// For nested pointer types, like **int, they are all dereferenced in turn
// until a non-pointer value is found, or until a nil pointer is encountered.
func toCtyUnwrapPointer(val reflect.Value) reflect.Value {
for val.Kind() == reflect.Ptr || val.Kind() == reflect.Interface {
if val.IsNil() {
return reflect.Value{}
}
val = val.Elem()
}
return val
}
|