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 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
|
package cty
import (
"fmt"
"math/big"
"github.com/zclconf/go-cty/cty/set"
)
// GoString is an implementation of fmt.GoStringer that produces concise
// source-like representations of values suitable for use in debug messages.
func (val Value) GoString() string {
if val.IsMarked() {
unVal, marks := val.Unmark()
if len(marks) == 1 {
var mark interface{}
for m := range marks {
mark = m
}
return fmt.Sprintf("%#v.Mark(%#v)", unVal, mark)
}
return fmt.Sprintf("%#v.WithMarks(%#v)", unVal, marks)
}
if val == NilVal {
return "cty.NilVal"
}
if val.IsNull() {
return fmt.Sprintf("cty.NullVal(%#v)", val.ty)
}
if val == DynamicVal { // is unknown, so must be before the IsKnown check below
return "cty.DynamicVal"
}
if !val.IsKnown() {
return fmt.Sprintf("cty.UnknownVal(%#v)", val.ty)
}
// By the time we reach here we've dealt with all of the exceptions around
// unknowns and nulls, so we're guaranteed that the values are the
// canonical internal representation of the given type.
switch val.ty {
case Bool:
if val.v.(bool) {
return "cty.True"
}
return "cty.False"
case Number:
if f, ok := val.v.(big.Float); ok {
panic(fmt.Sprintf("number value contains big.Float value %s, rather than pointer to big.Float", f.Text('g', -1)))
}
fv := val.v.(*big.Float)
// We'll try to use NumberIntVal or NumberFloatVal if we can, since
// the fully-general initializer call is pretty ugly-looking.
if fv.IsInt() {
return fmt.Sprintf("cty.NumberIntVal(%#v)", fv)
}
if rfv, accuracy := fv.Float64(); accuracy == big.Exact {
return fmt.Sprintf("cty.NumberFloatVal(%#v)", rfv)
}
return fmt.Sprintf("cty.MustParseNumberVal(%q)", fv.Text('f', -1))
case String:
return fmt.Sprintf("cty.StringVal(%#v)", val.v)
}
switch {
case val.ty.IsSetType():
vals := val.AsValueSlice()
if len(vals) == 0 {
return fmt.Sprintf("cty.SetValEmpty(%#v)", val.ty.ElementType())
}
return fmt.Sprintf("cty.SetVal(%#v)", vals)
case val.ty.IsListType():
vals := val.AsValueSlice()
if len(vals) == 0 {
return fmt.Sprintf("cty.ListValEmpty(%#v)", val.ty.ElementType())
}
return fmt.Sprintf("cty.ListVal(%#v)", vals)
case val.ty.IsMapType():
vals := val.AsValueMap()
if len(vals) == 0 {
return fmt.Sprintf("cty.MapValEmpty(%#v)", val.ty.ElementType())
}
return fmt.Sprintf("cty.MapVal(%#v)", vals)
case val.ty.IsTupleType():
if val.ty.Equals(EmptyTuple) {
return "cty.EmptyTupleVal"
}
vals := val.AsValueSlice()
return fmt.Sprintf("cty.TupleVal(%#v)", vals)
case val.ty.IsObjectType():
if val.ty.Equals(EmptyObject) {
return "cty.EmptyObjectVal"
}
vals := val.AsValueMap()
return fmt.Sprintf("cty.ObjectVal(%#v)", vals)
case val.ty.IsCapsuleType():
impl := val.ty.CapsuleOps().GoString
if impl == nil {
return fmt.Sprintf("cty.CapsuleVal(%#v, %#v)", val.ty, val.v)
}
return impl(val.EncapsulatedValue())
}
// Default exposes implementation details, so should actually cover
// all of the cases above for good caller UX.
return fmt.Sprintf("cty.Value{ty: %#v, v: %#v}", val.ty, val.v)
}
// Equals returns True if the receiver and the given other value have the
// same type and are exactly equal in value.
//
// As a special case, two null values are always equal regardless of type.
//
// The usual short-circuit rules apply, so the result will be unknown if
// either of the given values are.
//
// Use RawEquals to compare if two values are equal *ignoring* the
// short-circuit rules and the exception for null values.
func (val Value) Equals(other Value) Value {
if val.ContainsMarked() || other.ContainsMarked() {
val, valMarks := val.UnmarkDeep()
other, otherMarks := other.UnmarkDeep()
return val.Equals(other).WithMarks(valMarks, otherMarks)
}
// Start by handling Unknown values before considering types.
// This needs to be done since Null values are always equal regardless of
// type.
switch {
case !val.IsKnown() && !other.IsKnown():
// both unknown
return UnknownVal(Bool)
case val.IsKnown() && !other.IsKnown():
switch {
case val.IsNull(), other.ty.HasDynamicTypes():
// If known is Null, we need to wait for the unknown value since
// nulls of any type are equal.
// An unknown with a dynamic type compares as unknown, which we need
// to check before the type comparison below.
return UnknownVal(Bool)
case !val.ty.Equals(other.ty):
// There is no null comparison or dynamic types, so unequal types
// will never be equal.
return False
default:
return UnknownVal(Bool)
}
case other.IsKnown() && !val.IsKnown():
switch {
case other.IsNull(), val.ty.HasDynamicTypes():
// If known is Null, we need to wait for the unknown value since
// nulls of any type are equal.
// An unknown with a dynamic type compares as unknown, which we need
// to check before the type comparison below.
return UnknownVal(Bool)
case !other.ty.Equals(val.ty):
// There's no null comparison or dynamic types, so unequal types
// will never be equal.
return False
default:
return UnknownVal(Bool)
}
}
switch {
case val.IsNull() && other.IsNull():
// Nulls are always equal, regardless of type
return BoolVal(true)
case val.IsNull() || other.IsNull():
// If only one is null then the result must be false
return BoolVal(false)
}
// Check if there are any nested dynamic values making this comparison
// unknown.
if !val.HasWhollyKnownType() || !other.HasWhollyKnownType() {
// Even if we have dynamic values, we can still determine inequality if
// there is no way the types could later conform.
if val.ty.TestConformance(other.ty) != nil && other.ty.TestConformance(val.ty) != nil {
return BoolVal(false)
}
return UnknownVal(Bool)
}
if !val.ty.Equals(other.ty) {
return BoolVal(false)
}
ty := val.ty
result := false
switch {
case ty == Number:
result = rawNumberEqual(val.v.(*big.Float), other.v.(*big.Float))
case ty == Bool:
result = val.v.(bool) == other.v.(bool)
case ty == String:
// Simple equality is safe because we NFC-normalize strings as they
// enter our world from StringVal, and so we can assume strings are
// always in normal form.
result = val.v.(string) == other.v.(string)
case ty.IsObjectType():
oty := ty.typeImpl.(typeObject)
result = true
for attr, aty := range oty.AttrTypes {
lhs := Value{
ty: aty,
v: val.v.(map[string]interface{})[attr],
}
rhs := Value{
ty: aty,
v: other.v.(map[string]interface{})[attr],
}
eq := lhs.Equals(rhs)
if !eq.IsKnown() {
return UnknownVal(Bool)
}
if eq.False() {
result = false
break
}
}
case ty.IsTupleType():
tty := ty.typeImpl.(typeTuple)
result = true
for i, ety := range tty.ElemTypes {
lhs := Value{
ty: ety,
v: val.v.([]interface{})[i],
}
rhs := Value{
ty: ety,
v: other.v.([]interface{})[i],
}
eq := lhs.Equals(rhs)
if !eq.IsKnown() {
return UnknownVal(Bool)
}
if eq.False() {
result = false
break
}
}
case ty.IsListType():
ety := ty.typeImpl.(typeList).ElementTypeT
if len(val.v.([]interface{})) == len(other.v.([]interface{})) {
result = true
for i := range val.v.([]interface{}) {
lhs := Value{
ty: ety,
v: val.v.([]interface{})[i],
}
rhs := Value{
ty: ety,
v: other.v.([]interface{})[i],
}
eq := lhs.Equals(rhs)
if !eq.IsKnown() {
return UnknownVal(Bool)
}
if eq.False() {
result = false
break
}
}
}
case ty.IsSetType():
s1 := val.v.(set.Set[interface{}])
s2 := other.v.(set.Set[interface{}])
equal := true
// Two sets are equal if all of their values are known and all values
// in one are also in the other.
for it := s1.Iterator(); it.Next(); {
rv := it.Value()
if rv == unknown { // "unknown" is the internal representation of unknown-ness
return UnknownVal(Bool)
}
if !s2.Has(rv) {
equal = false
}
}
for it := s2.Iterator(); it.Next(); {
rv := it.Value()
if rv == unknown { // "unknown" is the internal representation of unknown-ness
return UnknownVal(Bool)
}
if !s1.Has(rv) {
equal = false
}
}
result = equal
case ty.IsMapType():
ety := ty.typeImpl.(typeMap).ElementTypeT
if len(val.v.(map[string]interface{})) == len(other.v.(map[string]interface{})) {
result = true
for k := range val.v.(map[string]interface{}) {
if _, ok := other.v.(map[string]interface{})[k]; !ok {
result = false
break
}
lhs := Value{
ty: ety,
v: val.v.(map[string]interface{})[k],
}
rhs := Value{
ty: ety,
v: other.v.(map[string]interface{})[k],
}
eq := lhs.Equals(rhs)
if !eq.IsKnown() {
return UnknownVal(Bool)
}
if eq.False() {
result = false
break
}
}
}
case ty.IsCapsuleType():
impl := val.ty.CapsuleOps().Equals
if impl == nil {
impl := val.ty.CapsuleOps().RawEquals
if impl == nil {
// A capsule type's encapsulated value is a pointer to a value of its
// native type, so we can just compare these to get the identity test
// we need.
return BoolVal(val.v == other.v)
}
return BoolVal(impl(val.v, other.v))
}
ret := impl(val.v, other.v)
if !ret.Type().Equals(Bool) {
panic(fmt.Sprintf("Equals for %#v returned %#v, not cty.Bool", ty, ret.Type()))
}
return ret
default:
// should never happen
panic(fmt.Errorf("unsupported value type %#v in Equals", ty))
}
return BoolVal(result)
}
// NotEqual is a shorthand for Equals followed by Not.
func (val Value) NotEqual(other Value) Value {
return val.Equals(other).Not()
}
// True returns true if the receiver is True, false if False, and panics if
// the receiver is not of type Bool.
//
// This is a helper function to help write application logic that works with
// values, rather than a first-class operation. It does not work with unknown
// or null values. For more robust handling with unknown value
// short-circuiting, use val.Equals(cty.True).
func (val Value) True() bool {
val.assertUnmarked()
if val.ty != Bool {
panic("not bool")
}
return val.Equals(True).v.(bool)
}
// False is the opposite of True.
func (val Value) False() bool {
return !val.True()
}
// RawEquals returns true if and only if the two given values have the same
// type and equal value, ignoring the usual short-circuit rules about
// unknowns and dynamic types.
//
// This method is more appropriate for testing than for real use, since it
// skips over usual semantics around unknowns but as a consequence allows
// testing the result of another operation that is expected to return unknown.
// It returns a primitive Go bool rather than a Value to remind us that it
// is not a first-class value operation.
func (val Value) RawEquals(other Value) bool {
if !val.ty.Equals(other.ty) {
return false
}
if !val.HasSameMarks(other) {
return false
}
// Since we've now checked the marks, we'll unmark for the rest of this...
val = val.unmarkForce()
other = other.unmarkForce()
if (!val.IsKnown()) && (!other.IsKnown()) {
return true
}
if (val.IsKnown() && !other.IsKnown()) || (other.IsKnown() && !val.IsKnown()) {
return false
}
if val.IsNull() && other.IsNull() {
return true
}
if (val.IsNull() && !other.IsNull()) || (other.IsNull() && !val.IsNull()) {
return false
}
if val.ty == DynamicPseudoType && other.ty == DynamicPseudoType {
return true
}
ty := val.ty
switch {
case ty == Number || ty == Bool || ty == String || ty == DynamicPseudoType:
return val.Equals(other).True()
case ty.IsObjectType():
oty := ty.typeImpl.(typeObject)
for attr, aty := range oty.AttrTypes {
lhs := Value{
ty: aty,
v: val.v.(map[string]interface{})[attr],
}
rhs := Value{
ty: aty,
v: other.v.(map[string]interface{})[attr],
}
eq := lhs.RawEquals(rhs)
if !eq {
return false
}
}
return true
case ty.IsTupleType():
tty := ty.typeImpl.(typeTuple)
for i, ety := range tty.ElemTypes {
lhs := Value{
ty: ety,
v: val.v.([]interface{})[i],
}
rhs := Value{
ty: ety,
v: other.v.([]interface{})[i],
}
eq := lhs.RawEquals(rhs)
if !eq {
return false
}
}
return true
case ty.IsListType():
ety := ty.typeImpl.(typeList).ElementTypeT
if len(val.v.([]interface{})) == len(other.v.([]interface{})) {
for i := range val.v.([]interface{}) {
lhs := Value{
ty: ety,
v: val.v.([]interface{})[i],
}
rhs := Value{
ty: ety,
v: other.v.([]interface{})[i],
}
eq := lhs.RawEquals(rhs)
if !eq {
return false
}
}
return true
}
return false
case ty.IsSetType():
// Convert the set values into a slice so that we can compare each
// value. This is safe because the underlying sets are ordered (see
// setRules in set_internals.go), and so the results are guaranteed to
// be in a consistent order for two equal sets
setList1 := val.AsValueSlice()
setList2 := other.AsValueSlice()
// If both physical sets have the same length and they have all of their
// _known_ values in common, we know that both sets also have the same
// number of unknown values.
if len(setList1) != len(setList2) {
return false
}
for i := range setList1 {
eq := setList1[i].RawEquals(setList2[i])
if !eq {
return false
}
}
// If we got here without returning false already then our sets are
// equal enough for RawEquals purposes.
return true
case ty.IsMapType():
ety := ty.typeImpl.(typeMap).ElementTypeT
if !val.HasSameMarks(other) {
return false
}
valUn, _ := val.Unmark()
otherUn, _ := other.Unmark()
if len(valUn.v.(map[string]interface{})) == len(otherUn.v.(map[string]interface{})) {
for k := range valUn.v.(map[string]interface{}) {
if _, ok := otherUn.v.(map[string]interface{})[k]; !ok {
return false
}
lhs := Value{
ty: ety,
v: valUn.v.(map[string]interface{})[k],
}
rhs := Value{
ty: ety,
v: otherUn.v.(map[string]interface{})[k],
}
eq := lhs.RawEquals(rhs)
if !eq {
return false
}
}
return true
}
return false
case ty.IsCapsuleType():
impl := val.ty.CapsuleOps().RawEquals
if impl == nil {
// A capsule type's encapsulated value is a pointer to a value of its
// native type, so we can just compare these to get the identity test
// we need.
return val.v == other.v
}
return impl(val.v, other.v)
default:
// should never happen
panic(fmt.Errorf("unsupported value type %#v in RawEquals", ty))
}
}
// Add returns the sum of the receiver and the given other value. Both values
// must be numbers; this method will panic if not.
func (val Value) Add(other Value) Value {
if val.IsMarked() || other.IsMarked() {
val, valMarks := val.Unmark()
other, otherMarks := other.Unmark()
return val.Add(other).WithMarks(valMarks, otherMarks)
}
if shortCircuit := mustTypeCheck(Number, Number, val, other); shortCircuit != nil {
shortCircuit = forceShortCircuitType(shortCircuit, Number)
return *shortCircuit
}
ret := new(big.Float)
ret.Add(val.v.(*big.Float), other.v.(*big.Float))
return NumberVal(ret)
}
// Subtract returns receiver minus the given other value. Both values must be
// numbers; this method will panic if not.
func (val Value) Subtract(other Value) Value {
if val.IsMarked() || other.IsMarked() {
val, valMarks := val.Unmark()
other, otherMarks := other.Unmark()
return val.Subtract(other).WithMarks(valMarks, otherMarks)
}
if shortCircuit := mustTypeCheck(Number, Number, val, other); shortCircuit != nil {
shortCircuit = forceShortCircuitType(shortCircuit, Number)
return *shortCircuit
}
return val.Add(other.Negate())
}
// Negate returns the numeric negative of the receiver, which must be a number.
// This method will panic when given a value of any other type.
func (val Value) Negate() Value {
if val.IsMarked() {
val, valMarks := val.Unmark()
return val.Negate().WithMarks(valMarks)
}
if shortCircuit := mustTypeCheck(Number, Number, val); shortCircuit != nil {
shortCircuit = forceShortCircuitType(shortCircuit, Number)
return *shortCircuit
}
ret := new(big.Float).Neg(val.v.(*big.Float))
return NumberVal(ret)
}
// Multiply returns the product of the receiver and the given other value.
// Both values must be numbers; this method will panic if not.
func (val Value) Multiply(other Value) Value {
if val.IsMarked() || other.IsMarked() {
val, valMarks := val.Unmark()
other, otherMarks := other.Unmark()
return val.Multiply(other).WithMarks(valMarks, otherMarks)
}
if shortCircuit := mustTypeCheck(Number, Number, val, other); shortCircuit != nil {
shortCircuit = forceShortCircuitType(shortCircuit, Number)
return *shortCircuit
}
// find the larger precision of the arguments
resPrec := val.v.(*big.Float).Prec()
otherPrec := other.v.(*big.Float).Prec()
if otherPrec > resPrec {
resPrec = otherPrec
}
// make sure we have enough precision for the product
ret := new(big.Float).SetPrec(512)
ret.Mul(val.v.(*big.Float), other.v.(*big.Float))
// now reduce the precision back to the greater argument, or the minimum
// required by the product.
minPrec := ret.MinPrec()
if minPrec > resPrec {
resPrec = minPrec
}
ret.SetPrec(resPrec)
return NumberVal(ret)
}
// Divide returns the quotient of the receiver and the given other value.
// Both values must be numbers; this method will panic if not.
//
// If the "other" value is exactly zero, this operation will return either
// PositiveInfinity or NegativeInfinity, depending on the sign of the
// receiver value. For some use-cases the presence of infinities may be
// undesirable, in which case the caller should check whether the
// other value equals zero before calling and raise an error instead.
//
// If both values are zero or infinity, this function will panic with
// an instance of big.ErrNaN.
func (val Value) Divide(other Value) Value {
if val.IsMarked() || other.IsMarked() {
val, valMarks := val.Unmark()
other, otherMarks := other.Unmark()
return val.Divide(other).WithMarks(valMarks, otherMarks)
}
if shortCircuit := mustTypeCheck(Number, Number, val, other); shortCircuit != nil {
shortCircuit = forceShortCircuitType(shortCircuit, Number)
return *shortCircuit
}
ret := new(big.Float)
ret.Quo(val.v.(*big.Float), other.v.(*big.Float))
return NumberVal(ret)
}
// Modulo returns the remainder of an integer division of the receiver and
// the given other value. Both values must be numbers; this method will panic
// if not.
//
// If the "other" value is exactly zero, this operation will return either
// PositiveInfinity or NegativeInfinity, depending on the sign of the
// receiver value. For some use-cases the presence of infinities may be
// undesirable, in which case the caller should check whether the
// other value equals zero before calling and raise an error instead.
//
// This operation is primarily here for use with nonzero natural numbers.
// Modulo with "other" as a non-natural number gets somewhat philosophical,
// and this function takes a position on what that should mean, but callers
// may wish to disallow such things outright or implement their own modulo
// if they disagree with the interpretation used here.
func (val Value) Modulo(other Value) Value {
if val.IsMarked() || other.IsMarked() {
val, valMarks := val.Unmark()
other, otherMarks := other.Unmark()
return val.Modulo(other).WithMarks(valMarks, otherMarks)
}
if shortCircuit := mustTypeCheck(Number, Number, val, other); shortCircuit != nil {
shortCircuit = forceShortCircuitType(shortCircuit, Number)
return *shortCircuit
}
// We cheat a bit here with infinities, just abusing the Multiply operation
// to get an infinite result of the correct sign.
if val == PositiveInfinity || val == NegativeInfinity || other == PositiveInfinity || other == NegativeInfinity {
return val.Multiply(other)
}
if other.RawEquals(Zero) {
return val
}
// FIXME: This is a bit clumsy. Should come back later and see if there's a
// more straightforward way to do this.
rat := val.Divide(other)
ratFloorInt, _ := rat.v.(*big.Float).Int(nil)
// start with a copy of the original larger value so that we do not lose
// precision.
v := val.v.(*big.Float)
work := new(big.Float).Copy(v).SetInt(ratFloorInt)
work.Mul(other.v.(*big.Float), work)
work.Sub(v, work)
return NumberVal(work)
}
// Absolute returns the absolute (signless) value of the receiver, which must
// be a number or this method will panic.
func (val Value) Absolute() Value {
if val.IsMarked() {
val, valMarks := val.Unmark()
return val.Absolute().WithMarks(valMarks)
}
if shortCircuit := mustTypeCheck(Number, Number, val); shortCircuit != nil {
shortCircuit = forceShortCircuitType(shortCircuit, Number)
return *shortCircuit
}
ret := (&big.Float{}).Abs(val.v.(*big.Float))
return NumberVal(ret)
}
// GetAttr returns the value of the given attribute of the receiver, which
// must be of an object type that has an attribute of the given name.
// This method will panic if the receiver type is not compatible.
//
// The method will also panic if the given attribute name is not defined
// for the value's type. Use the attribute-related methods on Type to
// check for the validity of an attribute before trying to use it.
//
// This method may be called on a value whose type is DynamicPseudoType,
// in which case the result will also be DynamicVal.
func (val Value) GetAttr(name string) Value {
if val.IsMarked() {
val, valMarks := val.Unmark()
return val.GetAttr(name).WithMarks(valMarks)
}
if val.ty == DynamicPseudoType {
return DynamicVal
}
if !val.ty.IsObjectType() {
panic("value is not an object")
}
name = NormalizeString(name)
if !val.ty.HasAttribute(name) {
panic("value has no attribute of that name")
}
attrType := val.ty.AttributeType(name)
if !val.IsKnown() {
return UnknownVal(attrType)
}
return Value{
ty: attrType,
v: val.v.(map[string]interface{})[name],
}
}
// Index returns the value of an element of the receiver, which must have
// either a list, map or tuple type. This method will panic if the receiver
// type is not compatible.
//
// The key value must be the correct type for the receving collection: a
// number if the collection is a list or tuple, or a string if it is a map.
// In the case of a list or tuple, the given number must be convertable to int
// or this method will panic. The key may alternatively be of
// DynamicPseudoType, in which case the result itself is an unknown of the
// collection's element type.
//
// The result is of the receiver collection's element type, or in the case
// of a tuple the type of the specific element index requested.
//
// This method may be called on a value whose type is DynamicPseudoType,
// in which case the result will also be the DynamicValue.
func (val Value) Index(key Value) Value {
if val.IsMarked() || key.IsMarked() {
val, valMarks := val.Unmark()
key, keyMarks := key.Unmark()
return val.Index(key).WithMarks(valMarks, keyMarks)
}
if val.ty == DynamicPseudoType {
return DynamicVal
}
switch {
case val.Type().IsListType():
elty := val.Type().ElementType()
if key.Type() == DynamicPseudoType {
return UnknownVal(elty)
}
if key.Type() != Number {
panic("element key for list must be number")
}
if !key.IsKnown() {
return UnknownVal(elty)
}
if !val.IsKnown() {
return UnknownVal(elty)
}
index, accuracy := key.v.(*big.Float).Int64()
if accuracy != big.Exact || index < 0 {
panic("element key for list must be non-negative integer")
}
return Value{
ty: elty,
v: val.v.([]interface{})[index],
}
case val.Type().IsMapType():
elty := val.Type().ElementType()
if key.Type() == DynamicPseudoType {
return UnknownVal(elty)
}
if key.Type() != String {
panic("element key for map must be string")
}
if !key.IsKnown() {
return UnknownVal(elty)
}
if !val.IsKnown() {
return UnknownVal(elty)
}
keyStr := key.v.(string)
return Value{
ty: elty,
v: val.v.(map[string]interface{})[keyStr],
}
case val.Type().IsTupleType():
if key.Type() == DynamicPseudoType {
return DynamicVal
}
if key.Type() != Number {
panic("element key for tuple must be number")
}
if !key.IsKnown() {
return DynamicVal
}
index, accuracy := key.v.(*big.Float).Int64()
if accuracy != big.Exact || index < 0 {
panic("element key for list must be non-negative integer")
}
eltys := val.Type().TupleElementTypes()
if !val.IsKnown() {
return UnknownVal(eltys[index])
}
return Value{
ty: eltys[index],
v: val.v.([]interface{})[index],
}
default:
panic("not a list, map, or tuple type")
}
}
// HasIndex returns True if the receiver (which must be supported for Index)
// has an element with the given index key, or False if it does not.
//
// The result will be UnknownVal(Bool) if either the collection or the
// key value are unknown.
//
// This method will panic if the receiver is not indexable, but does not
// impose any panic-causing type constraints on the key.
func (val Value) HasIndex(key Value) Value {
if val.IsMarked() || key.IsMarked() {
val, valMarks := val.Unmark()
key, keyMarks := key.Unmark()
return val.HasIndex(key).WithMarks(valMarks, keyMarks)
}
if val.ty == DynamicPseudoType {
return UnknownVal(Bool)
}
switch {
case val.Type().IsListType():
if key.Type() == DynamicPseudoType {
return UnknownVal(Bool)
}
if key.Type() != Number {
return False
}
if !key.IsKnown() {
return UnknownVal(Bool)
}
if !val.IsKnown() {
return UnknownVal(Bool)
}
index, accuracy := key.v.(*big.Float).Int64()
if accuracy != big.Exact || index < 0 {
return False
}
return BoolVal(int(index) < len(val.v.([]interface{})) && index >= 0)
case val.Type().IsMapType():
if key.Type() == DynamicPseudoType {
return UnknownVal(Bool)
}
if key.Type() != String {
return False
}
if !key.IsKnown() {
return UnknownVal(Bool)
}
if !val.IsKnown() {
return UnknownVal(Bool)
}
keyStr := key.v.(string)
_, exists := val.v.(map[string]interface{})[keyStr]
return BoolVal(exists)
case val.Type().IsTupleType():
if key.Type() == DynamicPseudoType {
return UnknownVal(Bool)
}
if key.Type() != Number {
return False
}
if !key.IsKnown() {
return UnknownVal(Bool)
}
index, accuracy := key.v.(*big.Float).Int64()
if accuracy != big.Exact || index < 0 {
return False
}
length := val.Type().Length()
return BoolVal(int(index) < length && index >= 0)
default:
panic("not a list, map, or tuple type")
}
}
// HasElement returns True if the receiver (which must be of a set type)
// has the given value as an element, or False if it does not.
//
// The result will be UnknownVal(Bool) if either the set or the
// given value are unknown.
//
// This method will panic if the receiver is not a set, or if it is a null set.
func (val Value) HasElement(elem Value) Value {
if val.IsMarked() || elem.IsMarked() {
val, valMarks := val.Unmark()
elem, elemMarks := elem.Unmark()
return val.HasElement(elem).WithMarks(valMarks, elemMarks)
}
ty := val.Type()
if !ty.IsSetType() {
panic("not a set type")
}
if !val.IsKnown() || !elem.IsKnown() {
return UnknownVal(Bool)
}
if val.IsNull() {
panic("can't call HasElement on a nil value")
}
if !ty.ElementType().Equals(elem.Type()) {
return False
}
s := val.v.(set.Set[interface{}])
return BoolVal(s.Has(elem.v))
}
// Length returns the length of the receiver, which must be a collection type
// or tuple type, as a number value. If the receiver is not a compatible type
// then this method will panic.
//
// If the receiver is unknown then the result is also unknown.
//
// If the receiver is null then this function will panic.
//
// Note that Length is not supported for strings. To determine the length
// of a string, use the Length function in funcs/stdlib.
func (val Value) Length() Value {
if val.IsMarked() {
val, valMarks := val.Unmark()
return val.Length().WithMarks(valMarks)
}
if val.Type().IsTupleType() {
// For tuples, we can return the length even if the value is not known.
return NumberIntVal(int64(val.Type().Length()))
}
if !val.IsKnown() {
return UnknownVal(Number)
}
if val.Type().IsSetType() {
// The Length rules are a little different for sets because if any
// unknown values are present then the values they are standing in for
// may or may not be equal to other elements in the set, and thus they
// may or may not coalesce with other elements and produce fewer
// items in the resulting set.
storeLength := int64(val.v.(set.Set[interface{}]).Length())
if storeLength == 1 || val.IsWhollyKnown() {
// If our set is wholly known then we know its length.
//
// We also know the length if the physical store has only one
// element, even if that element is unknown, because there's
// nothing else in the set for it to coalesce with and a single
// unknown value cannot represent more than one known value.
return NumberIntVal(storeLength)
}
// Otherwise, we cannot predict the length.
return UnknownVal(Number)
}
return NumberIntVal(int64(val.LengthInt()))
}
// LengthInt is like Length except it returns an int. It has the same behavior
// as Length except that it will panic if the receiver is unknown.
//
// This is an integration method provided for the convenience of code bridging
// into Go's type system.
//
// For backward compatibility with an earlier implementation error, LengthInt's
// result can disagree with Length's result for any set containing unknown
// values. Length can potentially indicate the set's length is unknown in that
// case, whereas LengthInt will return the maximum possible length as if the
// unknown values were each a placeholder for a value not equal to any other
// value in the set.
func (val Value) LengthInt() int {
val.assertUnmarked()
if val.Type().IsTupleType() {
// For tuples, we can return the length even if the value is not known.
return val.Type().Length()
}
if val.Type().IsObjectType() {
// For objects, the length is the number of attributes associated with the type.
return len(val.Type().AttributeTypes())
}
if !val.IsKnown() {
panic("value is not known")
}
if val.IsNull() {
panic("value is null")
}
switch {
case val.ty.IsListType():
return len(val.v.([]interface{}))
case val.ty.IsSetType():
// NOTE: This is technically not correct in cases where the set
// contains unknown values, because in that case we can't know how
// many known values those unknown values are standing in for -- they
// might coalesce with other values once known.
//
// However, this incorrect behavior is preserved for backward
// compatibility with callers that were relying on LengthInt rather
// than calling Length. Instead of panicking when a set contains an
// unknown value, LengthInt returns the largest possible length.
return val.v.(set.Set[interface{}]).Length()
case val.ty.IsMapType():
return len(val.v.(map[string]interface{}))
default:
panic("value is not a collection")
}
}
// ElementIterator returns an ElementIterator for iterating the elements
// of the receiver, which must be a collection type, a tuple type, or an object
// type. If called on a method of any other type, this method will panic.
//
// The value must be Known and non-Null, or this method will panic.
//
// If the receiver is of a list type, the returned keys will be of type Number
// and the values will be of the list's element type.
//
// If the receiver is of a map type, the returned keys will be of type String
// and the value will be of the map's element type. Elements are passed in
// ascending lexicographical order by key.
//
// If the receiver is of a set type, each element is returned as both the
// key and the value, since set members are their own identity.
//
// If the receiver is of a tuple type, the returned keys will be of type Number
// and the value will be of the corresponding element's type.
//
// If the receiver is of an object type, the returned keys will be of type
// String and the value will be of the corresponding attributes's type.
//
// ElementIterator is an integration method, so it cannot handle Unknown
// values. This method will panic if the receiver is Unknown.
func (val Value) ElementIterator() ElementIterator {
val.assertUnmarked()
if !val.IsKnown() {
panic("can't use ElementIterator on unknown value")
}
if val.IsNull() {
panic("can't use ElementIterator on null value")
}
return elementIterator(val)
}
// CanIterateElements returns true if the receiver can support the
// ElementIterator method (and by extension, ForEachElement) without panic.
func (val Value) CanIterateElements() bool {
return canElementIterator(val)
}
// ForEachElement executes a given callback function for each element of
// the receiver, which must be a collection type or tuple type, or this method
// will panic.
//
// ForEachElement uses ElementIterator internally, and so the values passed
// to the callback are as described for ElementIterator.
//
// Returns true if the iteration exited early due to the callback function
// returning true, or false if the loop ran to completion.
//
// ForEachElement is an integration method, so it cannot handle Unknown
// values. This method will panic if the receiver is Unknown.
func (val Value) ForEachElement(cb ElementCallback) bool {
val.assertUnmarked()
it := val.ElementIterator()
for it.Next() {
key, val := it.Element()
stop := cb(key, val)
if stop {
return true
}
}
return false
}
// Not returns the logical inverse of the receiver, which must be of type
// Bool or this method will panic.
func (val Value) Not() Value {
if val.IsMarked() {
val, valMarks := val.Unmark()
return val.Not().WithMarks(valMarks)
}
if shortCircuit := mustTypeCheck(Bool, Bool, val); shortCircuit != nil {
shortCircuit = forceShortCircuitType(shortCircuit, Bool)
return *shortCircuit
}
return BoolVal(!val.v.(bool))
}
// And returns the result of logical AND with the receiver and the other given
// value, which must both be of type Bool or this method will panic.
func (val Value) And(other Value) Value {
if val.IsMarked() || other.IsMarked() {
val, valMarks := val.Unmark()
other, otherMarks := other.Unmark()
return val.And(other).WithMarks(valMarks, otherMarks)
}
if shortCircuit := mustTypeCheck(Bool, Bool, val, other); shortCircuit != nil {
shortCircuit = forceShortCircuitType(shortCircuit, Bool)
return *shortCircuit
}
return BoolVal(val.v.(bool) && other.v.(bool))
}
// Or returns the result of logical OR with the receiver and the other given
// value, which must both be of type Bool or this method will panic.
func (val Value) Or(other Value) Value {
if val.IsMarked() || other.IsMarked() {
val, valMarks := val.Unmark()
other, otherMarks := other.Unmark()
return val.Or(other).WithMarks(valMarks, otherMarks)
}
if shortCircuit := mustTypeCheck(Bool, Bool, val, other); shortCircuit != nil {
shortCircuit = forceShortCircuitType(shortCircuit, Bool)
return *shortCircuit
}
return BoolVal(val.v.(bool) || other.v.(bool))
}
// LessThan returns True if the receiver is less than the other given value,
// which must both be numbers or this method will panic.
func (val Value) LessThan(other Value) Value {
if val.IsMarked() || other.IsMarked() {
val, valMarks := val.Unmark()
other, otherMarks := other.Unmark()
return val.LessThan(other).WithMarks(valMarks, otherMarks)
}
if shortCircuit := mustTypeCheck(Number, Bool, val, other); shortCircuit != nil {
shortCircuit = forceShortCircuitType(shortCircuit, Bool)
return *shortCircuit
}
return BoolVal(val.v.(*big.Float).Cmp(other.v.(*big.Float)) < 0)
}
// GreaterThan returns True if the receiver is greater than the other given
// value, which must both be numbers or this method will panic.
func (val Value) GreaterThan(other Value) Value {
if val.IsMarked() || other.IsMarked() {
val, valMarks := val.Unmark()
other, otherMarks := other.Unmark()
return val.GreaterThan(other).WithMarks(valMarks, otherMarks)
}
if shortCircuit := mustTypeCheck(Number, Bool, val, other); shortCircuit != nil {
shortCircuit = forceShortCircuitType(shortCircuit, Bool)
return *shortCircuit
}
return BoolVal(val.v.(*big.Float).Cmp(other.v.(*big.Float)) > 0)
}
// LessThanOrEqualTo is equivalent to LessThan and Equal combined with Or.
func (val Value) LessThanOrEqualTo(other Value) Value {
return val.LessThan(other).Or(val.Equals(other))
}
// GreaterThanOrEqualTo is equivalent to GreaterThan and Equal combined with Or.
func (val Value) GreaterThanOrEqualTo(other Value) Value {
return val.GreaterThan(other).Or(val.Equals(other))
}
// AsString returns the native string from a non-null, non-unknown cty.String
// value, or panics if called on any other value.
func (val Value) AsString() string {
val.assertUnmarked()
if val.ty != String {
panic("not a string")
}
if val.IsNull() {
panic("value is null")
}
if !val.IsKnown() {
panic("value is unknown")
}
return val.v.(string)
}
// AsBigFloat returns a big.Float representation of a non-null, non-unknown
// cty.Number value, or panics if called on any other value.
//
// For more convenient conversions to other native numeric types, use the
// "gocty" package.
func (val Value) AsBigFloat() *big.Float {
val.assertUnmarked()
if val.ty != Number {
panic("not a number")
}
if val.IsNull() {
panic("value is null")
}
if !val.IsKnown() {
panic("value is unknown")
}
// Copy the float so that callers can't mutate our internal state
return new(big.Float).Copy(val.v.(*big.Float))
}
// AsValueSlice returns a []cty.Value representation of a non-null, non-unknown
// value of any type that CanIterateElements, or panics if called on
// any other value.
//
// For more convenient conversions to slices of more specific types, use
// the "gocty" package.
func (val Value) AsValueSlice() []Value {
val.assertUnmarked()
l := val.LengthInt()
if l == 0 {
return nil
}
ret := make([]Value, 0, l)
for it := val.ElementIterator(); it.Next(); {
_, v := it.Element()
ret = append(ret, v)
}
return ret
}
// AsValueMap returns a map[string]cty.Value representation of a non-null,
// non-unknown value of any type that CanIterateElements, or panics if called
// on any other value.
//
// For more convenient conversions to maps of more specific types, use
// the "gocty" package.
func (val Value) AsValueMap() map[string]Value {
val.assertUnmarked()
l := val.LengthInt()
if l == 0 {
return nil
}
ret := make(map[string]Value, l)
for it := val.ElementIterator(); it.Next(); {
k, v := it.Element()
ret[k.AsString()] = v
}
return ret
}
// AsValueSet returns a ValueSet representation of a non-null,
// non-unknown value of any collection type, or panics if called
// on any other value.
//
// Unlike AsValueSlice and AsValueMap, this method requires specifically a
// collection type (list, set or map) and does not allow structural types
// (tuple or object), because the ValueSet type requires homogenous
// element types.
//
// The returned ValueSet can store only values of the receiver's element type.
func (val Value) AsValueSet() ValueSet {
val.assertUnmarked()
if !val.Type().IsCollectionType() {
panic("not a collection type")
}
// We don't give the caller our own set.Set (assuming we're a cty.Set value)
// because then the caller could mutate our internals, which is forbidden.
// Instead, we will construct a new set and append our elements into it.
ret := NewValueSet(val.Type().ElementType())
for it := val.ElementIterator(); it.Next(); {
_, v := it.Element()
ret.Add(v)
}
return ret
}
// EncapsulatedValue returns the native value encapsulated in a non-null,
// non-unknown capsule-typed value, or panics if called on any other value.
//
// The result is the same pointer that was passed to CapsuleVal to create
// the value. Since cty considers values to be immutable, it is strongly
// recommended to treat the encapsulated value itself as immutable too.
func (val Value) EncapsulatedValue() interface{} {
val.assertUnmarked()
if !val.Type().IsCapsuleType() {
panic("not a capsule-typed value")
}
return val.v
}
|