Skip to content

Conversation

@bbasata
Copy link
Collaborator

@bbasata bbasata commented Mar 18, 2025

Previously: #14

This PR updates this repo for parity with zclconf/go-cty v1.6.0. Highlights:

  • This version includes a major version bump of github.com/vmihailenco/msgpack to v4.
  • "Fixed various defects in the handling of sets containing unknown values. This will cause unknown values to now be returned in more situations, whereas before cty would often return incorrect results when working with sets containing unknown values."

The changes remain "Unreleased" with no new release tag for the moment. This prevents a flood of Dependabot PRs like hashicorp/terraform-plugin-testing#444 for all affected providers.

Verification:

$ git ls-remote upstream v1.6.0^{}
afad73847ef0e9fade2382c37ece138c5ba6fa6a	refs/tags/v1.6.0^{}

$ GIT_PAGER= git diff -U0 --ignore-matching-lines 'github.com/(hashicorp|zclconf)/go-cty' afad738..HEAD -- . ':(exclude).github/CODEOWNERS'
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0d85102..c263472 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1 +1 @@
-# 1.6.0 (August 30, 2020)
+# 1.6.0 (Unreleased)
@@ -18 +18 @@
-# 1.5.1 (June 25, 2020)
+# 1.5.1 (Unreleased)
@@ -23 +23 @@
-# 1.5.0 (June 11, 2020)
+# 1.5.0 (March 17, 2025)
@@ -28 +28 @@
-# 1.4.2 (May 29, 2020)
+# 1.4.2
@@ -32 +33 @@
-# 1.4.1 (May 18, 2020)
+# 1.4.1 (March 5, 2025)
diff --git a/cty/function/stdlib/string.go b/cty/function/stdlib/string.go
index 01ebc47..60e0ab5 100644
--- a/cty/function/stdlib/string.go
+++ b/cty/function/stdlib/string.go
@@ -154 +153,0 @@ var SubstrFunc = function.New(&function.Spec{
-
diff --git a/go.mod b/go.mod
index 72f6293..e991685 100644
--- a/go.mod
+++ b/go.mod
@@ -7 +7 @@ require (
-	golang.org/x/text v0.3.2
+	golang.org/x/text v0.3.8
diff --git a/go.sum b/go.sum
index 8de1002..ffeb16a 100644
--- a/go.sum
+++ b/go.sum
@@ -17,0 +18 @@ github.com/vmihailenco/tagparser v0.1.1/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgq
+github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
@@ -18,0 +20,2 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
+golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
+golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
@@ -20 +23 @@ golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR
-golang.org/x/net v0.0.0-20200301022130-244492dfa37a h1:GuSPYbZzB5/dcLNCwLQLsg3obCJtX9IJhpXkvY7kzk0=
+golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
@@ -21,0 +25,5 @@ golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLL
+golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
+golang.org/x/net v0.0.0-20220722155237-a158d28d115b h1:PxfKdU9lEEDYjdIzOtC4qFWgkU2rGHdKlKowJSMN9h0=
+golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
+golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -23 +31,6 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
-golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
+golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
+golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
@@ -25 +37,0 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
-golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
@@ -26,0 +39,4 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
+golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
+golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
+golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY=
+golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
@@ -27,0 +44,3 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
+golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
+golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
+golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

$ go1.12 test ./...
ok  	github.com/hashicorp/go-cty/cty	2.483s
ok  	github.com/hashicorp/go-cty/cty/convert	2.069s
ok  	github.com/hashicorp/go-cty/cty/function	3.376s
ok  	github.com/hashicorp/go-cty/cty/function/stdlib	1.836s
ok  	github.com/hashicorp/go-cty/cty/gocty	2.677s
ok  	github.com/hashicorp/go-cty/cty/json	2.666s
ok  	github.com/hashicorp/go-cty/cty/msgpack	2.935s
ok  	github.com/hashicorp/go-cty/cty/set	(cached)

$ ag zclconf -G '.go$'

apparentlymart and others added 10 commits March 18, 2025 11:14
Previously a negative index would lead to a panic.
Previously we required that when converting to an object type the input
must have at least the attributes from the target type, and optionally
additional attributes that would then be discarded.

We'll now allow creating an object type with one or more optional
attributes. These are optional only for conversion purposes: converting
to an object type with optional arguments will substitute a null value
for any attribute that doesn't exist in the source object type.

This being handled only under conversion is consistent with some other
similar cases: an object type is not conforming to a type constraint if
it has attributes that are not in the type constraint, even though a
conversion to that type constraint would be accepted. Likewise, a number
doesn't conform to the string type even though a conversion is available.
This distinction is so that cty applications can potentially bring their
own separate conversion rules if they wish, ignoring the convert package
in this repository.

For now this new capability is explicitly experimental and so not subject
to our typical backward-compatibility promises. The behavior of any
function producing or working with object types that have optional
attributes is subject to change even in future minor versions of this
package.
This is just in the interests of keeping things up-to-date and doesn't
really confer any specific benefit.

The underlying library now defaults to always using the full-length
encoding for integers provided as int64, so this change also includes
an explicit opt-in to the old behavior of selecting the most compact
integer representation possible, because for cty the different numeric
encodings are just a size optimization and have no semantic meaning.
Early in the implementation of cty I made an oversight which has,
unfortunately, played out as a variety of incorrect behaviors throughout
the cty functions: when a set contains unknown values, those unknown
values can potentially be standing in for values that are equal to others
in the set, and thus the effective length of the set can't be predicted.

Previously the Length function would return, in effect, the maximum
possible size of the set, which would result if all of the values
represented by the unknowns are non-equal. The caller might then get a
different known result from a call with unknowns than from a subsequent
call with those unknowns replaced with known values, which violates the
guarantees that cty intends to make when handling unknown values.

This changeset therefore starts by addressing that root bug: Length will
now return an unknown number if called on a set containing unknown values,
correctly representing that the final length is unpredictable.

This in turn had some downstream consequences for other functionality.
In particular, it should not have been possible to convert a set whose
length is unknown to a list, because it's not possible for a list to have
an unknown length. Therefore this changeset also includes a fix to the
convert package so that an unknown-length set converts to an unknown
list, which also addresses the related problem that a conversion from a
set to list can't predict where the unknown values will appear in the
sort order and thus can't predict the list indices even for the known
elements.

The rest of the changes here are similar adjustments to account for the
possibility of an unknown set length, in most cases similarly returning
an unknown result.

This changeset causes a difference in observable behavior from the
previous version, but it's not considered to be a breaking change because
the previous behavior was defective. With that said, callers that were
inadvertently depending on the defective behavior will find that their
calling application now behaves slightly differently, and so may wish to
make adjustments if the defective prior behavior was actually desirable
for some unusual situation.

As a pragmatic accommodation for existing callers, the Value.LengthInt
function is now defined as returning the maximum possible length in
situations where the length is unknown. This aligns with the number of
iterations a caller would encounter using an ElementIterator on such a
value, and also reflects a suitable capacity to use when allocating a
Go slice to append iterated elements into.
@bbasata bbasata requested a review from a team as a code owner March 18, 2025 15:31
@bbasata bbasata merged commit 0fdcf0e into master Mar 18, 2025
1 check passed
@bbasata bbasata deleted the forward-to-v1.6.0 branch March 18, 2025 15:42
@bbasata bbasata mentioned this pull request Mar 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants