From 23c3f03d8b7650cde2e2c6f02618af08893ecabe Mon Sep 17 00:00:00 2001 From: Steve Kemp Date: Sun, 12 Feb 2023 11:14:18 +0200 Subject: [PATCH 1/2] When converting hashes to strings, sort their keys first. This allows the following code to return "true" as expected: ``` (print (eq { :name "Ale" :age 3 :colors [:red :green :blue] } { :colors [:red :green :blue] :age 3 :name "Ale" })) ``` --- eval/eval_test.go | 6 ++++++ primitive/hash.go | 26 ++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/eval/eval_test.go b/eval/eval_test.go index e892c09..c83bd75 100644 --- a/eval/eval_test.go +++ b/eval/eval_test.go @@ -278,6 +278,12 @@ a {"(* 4 -1)", "-4"}, {"(# 3 2)", "9"}, + // hash equality + {`(eq { :name "Ale" :age 3} + { :age 3 :name "Ale"})`, "#t"}, + {`(eq { :name "ale" :age 3} + { :age 3 :name "Ale"})`, "#f"}, + // since we're variadic we start with the first // number, and apply the operation to any subsequent // ones. diff --git a/primitive/hash.go b/primitive/hash.go index a8f4e77..a5f5148 100644 --- a/primitive/hash.go +++ b/primitive/hash.go @@ -1,5 +1,7 @@ package primitive +import "sort" + // Hash holds a collection of other types, indexed by string type Hash struct { @@ -51,12 +53,32 @@ func (h *Hash) SetStruct(name string) { } // ToString converts this object to a string. +// +// Note that we sort the keys before returning the stringified object, +// which allows us to use the "eq" test on hashes with identical key/values, +// regardless of their ordering. func (h Hash) ToString() string { + // Output prefix. out := "{\n" - for k, v := range h.Entries { - out += "\t" + k + " => " + v.ToString() + "\n" + + // Get the keys in our hash. + keys := []string{} + + for x := range h.Entries { + keys = append(keys, x) } + + // Sort the list of keys + sort.Strings(keys) + + // Now we can get a consistent ordering for our + // hash keys/value pairs. + for _, key := range keys { + out += "\t" + key + " => " + h.Entries[key].ToString() + "\n" + } + + // Terminate the string representation and return. out += "}" return out } From 685cce24e9ac59f496130e2915f324eb19756317 Mon Sep 17 00:00:00 2001 From: Steve Kemp Date: Sun, 12 Feb 2023 11:20:01 +0200 Subject: [PATCH 2/2] Fix CI --- .github/run-tests.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/run-tests.sh b/.github/run-tests.sh index 0a879d6..75c330d 100755 --- a/.github/run-tests.sh +++ b/.github/run-tests.sh @@ -37,6 +37,8 @@ echo "Launching shadowed-variable check .." go vet -vettool=$(which shadow) ./... echo "Completed shadowed-variable check .." +go env -w GOFLAGS="-buildvcs=false" + # Run golang tests go test ./...