pax_global_header00006660000000000000000000000064144466211270014521gustar00rootroot0000000000000052 comment=85ca4a2b29d3241fa4513f82be3d38fe2392a791 go-ordered-map-2.1.8/000077500000000000000000000000001444662112700143335ustar00rootroot00000000000000go-ordered-map-2.1.8/.circleci/000077500000000000000000000000001444662112700161665ustar00rootroot00000000000000go-ordered-map-2.1.8/.circleci/circle_build.sh000077500000000000000000000004051444662112700211440ustar00rootroot00000000000000#!/usr/bin/env bash set -ex # might as well run a little longer export FUZZ_TIME=20s # there are too many golangci plugins that don't work for 1.19 just yet, so just skip linting for it if [[ "$GO_VER" == 1.18.* ]]; then make else make test_with_fuzz fi go-ordered-map-2.1.8/.circleci/config.yml000066400000000000000000000010321444662112700201520ustar00rootroot00000000000000version: 2.1 jobs: test: parameters: golang-version: type: string docker: - image: cimg/go:<< parameters.golang-version >> steps: - checkout - run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.45.2 - run: .circleci/circle_build.sh workflows: test-workflow: jobs: - test: matrix: parameters: golang-version: - '1.18' - '1.19' go-ordered-map-2.1.8/.gitignore000066400000000000000000000000111444662112700163130ustar00rootroot00000000000000/vendor/ go-ordered-map-2.1.8/.golangci.yml000066400000000000000000000024131444662112700167170ustar00rootroot00000000000000run: tests: false linters: disable-all: true enable: - asciicheck - bidichk - bodyclose - containedctx - contextcheck - decorder - depguard - dogsled - dupl - durationcheck - errcheck - errchkjson # FIXME: commented out as it crashes with 1.18 for now # - errname - errorlint - exportloopref - forbidigo - funlen - gci - gochecknoglobals - gochecknoinits - gocognit - goconst - gocritic - gocyclo - godox - gofmt - gofumpt - goheader - goimports - gomnd - gomoddirectives - gomodguard - goprintffuncname - gosec - gosimple - govet - grouper - ifshort - importas - ineffassign - lll - maintidx - makezero - misspell - nakedret - nilerr - nilnil - noctx - nolintlint - paralleltest - prealloc - predeclared - promlinter # FIXME: doesn't support 1.18 yet # - revive - rowserrcheck - sqlclosecheck - staticcheck - structcheck - stylecheck - tagliatelle - tenv - testpackage - thelper - tparallel - typecheck - unconvert - unparam - unused - varcheck - varnamelen - wastedassign - whitespace go-ordered-map-2.1.8/CHANGELOG.md000066400000000000000000000016351444662112700161510ustar00rootroot00000000000000# Changelog [comment]: # (Changes since last release go here) ## 2.1.8 - Jun 27th 2023 * Added support for YAML serialization/deserialization ## 2.1.7 - Apr 13th 2023 * Renamed test_utils.go to utils_test.go ## 2.1.6 - Feb 15th 2023 * Added `GetAndMoveToBack()` and `GetAndMoveToFront()` methods ## 2.1.5 - Dec 13th 2022 * Added `Value()` method ## 2.1.4 - Dec 12th 2022 * Fixed a bug with UTF-8 special characters in JSON keys ## 2.1.3 - Dec 11th 2022 * Added support for JSON marshalling/unmarshalling of wrapper of primitive types ## 2.1.2 - Dec 10th 2022 * Allowing to pass options to `New`, to give a capacity hint, or initial data * Allowing to deserialize nested ordered maps from JSON without having to explicitly instantiate them * Added the `AddPairs` method ## 2.1.1 - Dec 9th 2022 * Fixing a bug with JSON marshalling ## 2.1.0 - Dec 7th 2022 * Added support for JSON serialization/deserialization go-ordered-map-2.1.8/LICENSE000066400000000000000000000261351444662112700153470ustar00rootroot00000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "{}" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright {yyyy} {name of copyright owner} Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. go-ordered-map-2.1.8/Makefile000066400000000000000000000012261444662112700157740ustar00rootroot00000000000000.DEFAULT_GOAL := all .PHONY: all all: test_with_fuzz lint # the TEST_FLAGS env var can be set to eg run only specific tests TEST_COMMAND = go test -v -count=1 -race -cover $(TEST_FLAGS) .PHONY: test test: $(TEST_COMMAND) .PHONY: bench bench: go test -bench=. FUZZ_TIME ?= 10s # see https://github.com/golang/go/issues/46312 # and https://stackoverflow.com/a/72673487/4867444 # if we end up having more fuzz tests .PHONY: test_with_fuzz test_with_fuzz: $(TEST_COMMAND) -fuzz=FuzzRoundTripJSON -fuzztime=$(FUZZ_TIME) $(TEST_COMMAND) -fuzz=FuzzRoundTripYAML -fuzztime=$(FUZZ_TIME) .PHONY: fuzz fuzz: test_with_fuzz .PHONY: lint lint: golangci-lint run go-ordered-map-2.1.8/README.md000066400000000000000000000117051444662112700156160ustar00rootroot00000000000000[![Go Reference](https://pkg.go.dev/badge/github.com/wk8/go-ordered-map/v2.svg)](https://pkg.go.dev/github.com/wk8/go-ordered-map/v2) [![Build Status](https://circleci.com/gh/wk8/go-ordered-map.svg?style=svg)](https://app.circleci.com/pipelines/github/wk8/go-ordered-map) # Golang Ordered Maps Same as regular maps, but also remembers the order in which keys were inserted, akin to [Python's `collections.OrderedDict`s](https://docs.python.org/3.7/library/collections.html#ordereddict-objects). It offers the following features: * optimal runtime performance (all operations are constant time) * optimal memory usage (only one copy of values, no unnecessary memory allocation) * allows iterating from newest or oldest keys indifferently, without memory copy, allowing to `break` the iteration, and in time linear to the number of keys iterated over rather than the total length of the ordered map * supports any generic types for both keys and values. If you're running go < 1.18, you can use [version 1](https://github.com/wk8/go-ordered-map/tree/v1) that takes and returns generic `interface{}`s instead of using generics * idiomatic API, akin to that of [`container/list`](https://golang.org/pkg/container/list) * support for JSON and YAML marshalling ## Documentation [The full documentation is available on pkg.go.dev](https://pkg.go.dev/github.com/wk8/go-ordered-map/v2). ## Installation ```bash go get -u github.com/wk8/go-ordered-map/v2 ``` Or use your favorite golang vendoring tool! ## Supported go versions Go >= 1.18 is required to use version >= 2 of this library, as it uses generics. If you're running go < 1.18, you can use [version 1](https://github.com/wk8/go-ordered-map/tree/v1) instead. ## Example / usage ```go package main import ( "fmt" "github.com/wk8/go-ordered-map/v2" ) func main() { om := orderedmap.New[string, string]() om.Set("foo", "bar") om.Set("bar", "baz") om.Set("coucou", "toi") fmt.Println(om.Get("foo")) // => "bar", true fmt.Println(om.Get("i dont exist")) // => "", false // iterating pairs from oldest to newest: for pair := om.Oldest(); pair != nil; pair = pair.Next() { fmt.Printf("%s => %s\n", pair.Key, pair.Value) } // prints: // foo => bar // bar => baz // coucou => toi // iterating over the 2 newest pairs: i := 0 for pair := om.Newest(); pair != nil; pair = pair.Prev() { fmt.Printf("%s => %s\n", pair.Key, pair.Value) i++ if i >= 2 { break } } // prints: // coucou => toi // bar => baz } ``` An `OrderedMap`'s keys must implement `comparable`, and its values can be anything, for example: ```go type myStruct struct { payload string } func main() { om := orderedmap.New[int, *myStruct]() om.Set(12, &myStruct{"foo"}) om.Set(1, &myStruct{"bar"}) value, present := om.Get(12) if !present { panic("should be there!") } fmt.Println(value.payload) // => foo for pair := om.Oldest(); pair != nil; pair = pair.Next() { fmt.Printf("%d => %s\n", pair.Key, pair.Value.payload) } // prints: // 12 => foo // 1 => bar } ``` Also worth noting that you can provision ordered maps with a capacity hint, as you would do by passing an optional hint to `make(map[K]V, capacity`): ```go om := orderedmap.New[int, *myStruct](28) ``` You can also pass in some initial data to store in the map: ```go om := orderedmap.New[int, string](orderedmap.WithInitialData[int, string]( orderedmap.Pair[int, string]{ Key: 12, Value: "foo", }, orderedmap.Pair[int, string]{ Key: 28, Value: "bar", }, )) ``` `OrderedMap`s also support JSON serialization/deserialization, and preserves order: ```go // serialization data, err := json.Marshal(om) ... // deserialization om := orderedmap.New[string, string]() // or orderedmap.New[int, any](), or any type you expect err := json.Unmarshal(data, &om) ... ``` Similarly, it also supports YAML serialization/deserialization using the yaml.v3 package, which also preserves order: ```go // serialization data, err := yaml.Marshal(om) ... // deserialization om := orderedmap.New[string, string]() // or orderedmap.New[int, any](), or any type you expect err := yaml.Unmarshal(data, &om) ... ``` ## Alternatives There are several other ordered map golang implementations out there, but I believe that at the time of writing none of them offer the same functionality as this library; more specifically: * [iancoleman/orderedmap](https://github.com/iancoleman/orderedmap) only accepts `string` keys, its `Delete` operations are linear * [cevaris/ordered_map](https://github.com/cevaris/ordered_map) uses a channel for iterations, and leaks goroutines if the iteration is interrupted before fully traversing the map * [mantyr/iterator](https://github.com/mantyr/iterator) also uses a channel for iterations, and its `Delete` operations are linear * [samdolan/go-ordered-map](https://github.com/samdolan/go-ordered-map) adds unnecessary locking (users should add their own locking instead if they need it), its `Delete` and `Get` operations are linear, iterations trigger a linear memory allocation go-ordered-map-2.1.8/example_test.go000066400000000000000000000026401444662112700173560ustar00rootroot00000000000000package orderedmap_test import ( "encoding/json" "fmt" "github.com/wk8/go-ordered-map/v2" ) func Example() { om := orderedmap.New[string, string](3) om.Set("foo", "bar") om.Set("bar", "baz") om.Set("coucou", "toi") fmt.Println("## Get operations: ##") fmt.Println(om.Get("foo")) fmt.Println(om.Get("i dont exist")) fmt.Println(om.Value("coucou")) fmt.Println("## Iterating over pairs from oldest to newest: ##") for pair := om.Oldest(); pair != nil; pair = pair.Next() { fmt.Printf("%s => %s\n", pair.Key, pair.Value) } fmt.Println("## Iterating over the 2 newest pairs: ##") i := 0 for pair := om.Newest(); pair != nil; pair = pair.Prev() { fmt.Printf("%s => %s\n", pair.Key, pair.Value) i++ if i >= 2 { break } } fmt.Println("## JSON serialization: ##") data, err := json.Marshal(om) if err != nil { panic(err) } fmt.Println(string(data)) fmt.Println("## JSON deserialization: ##") om2 := orderedmap.New[string, string]() if err := json.Unmarshal(data, &om2); err != nil { panic(err) } fmt.Println(om2.Oldest().Key) // Output: // ## Get operations: ## // bar true // false // toi // ## Iterating over pairs from oldest to newest: ## // foo => bar // bar => baz // coucou => toi // ## Iterating over the 2 newest pairs: ## // coucou => toi // bar => baz // ## JSON serialization: ## // {"foo":"bar","bar":"baz","coucou":"toi"} // ## JSON deserialization: ## // foo } go-ordered-map-2.1.8/go.mod000066400000000000000000000005311444662112700154400ustar00rootroot00000000000000module github.com/wk8/go-ordered-map/v2 go 1.18 require ( github.com/bahlo/generic-list-go v0.2.0 github.com/buger/jsonparser v1.1.1 github.com/mailru/easyjson v0.7.7 github.com/stretchr/testify v1.8.1 gopkg.in/yaml.v3 v3.0.1 ) require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect ) go-ordered-map-2.1.8/go.sum000066400000000000000000000041271444662112700154720ustar00rootroot00000000000000github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk= github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg= github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs= github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= go-ordered-map-2.1.8/json.go000066400000000000000000000106431444662112700156370ustar00rootroot00000000000000package orderedmap import ( "bytes" "encoding" "encoding/json" "fmt" "reflect" "unicode/utf8" "github.com/buger/jsonparser" "github.com/mailru/easyjson/jwriter" ) var ( _ json.Marshaler = &OrderedMap[int, any]{} _ json.Unmarshaler = &OrderedMap[int, any]{} ) // MarshalJSON implements the json.Marshaler interface. func (om *OrderedMap[K, V]) MarshalJSON() ([]byte, error) { //nolint:funlen if om == nil || om.list == nil { return []byte("null"), nil } writer := jwriter.Writer{} writer.RawByte('{') for pair, firstIteration := om.Oldest(), true; pair != nil; pair = pair.Next() { if firstIteration { firstIteration = false } else { writer.RawByte(',') } switch key := any(pair.Key).(type) { case string: writer.String(key) case encoding.TextMarshaler: writer.RawByte('"') writer.Raw(key.MarshalText()) writer.RawByte('"') case int: writer.IntStr(key) case int8: writer.Int8Str(key) case int16: writer.Int16Str(key) case int32: writer.Int32Str(key) case int64: writer.Int64Str(key) case uint: writer.UintStr(key) case uint8: writer.Uint8Str(key) case uint16: writer.Uint16Str(key) case uint32: writer.Uint32Str(key) case uint64: writer.Uint64Str(key) default: // this switch takes care of wrapper types around primitive types, such as // type myType string switch keyValue := reflect.ValueOf(key); keyValue.Type().Kind() { case reflect.String: writer.String(keyValue.String()) case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: writer.Int64Str(keyValue.Int()) case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: writer.Uint64Str(keyValue.Uint()) default: return nil, fmt.Errorf("unsupported key type: %T", key) } } writer.RawByte(':') // the error is checked at the end of the function writer.Raw(json.Marshal(pair.Value)) //nolint:errchkjson } writer.RawByte('}') return dumpWriter(&writer) } func dumpWriter(writer *jwriter.Writer) ([]byte, error) { if writer.Error != nil { return nil, writer.Error } var buf bytes.Buffer buf.Grow(writer.Size()) if _, err := writer.DumpTo(&buf); err != nil { return nil, err } return buf.Bytes(), nil } // UnmarshalJSON implements the json.Unmarshaler interface. func (om *OrderedMap[K, V]) UnmarshalJSON(data []byte) error { if om.list == nil { om.initialize(0) } return jsonparser.ObjectEach( data, func(keyData []byte, valueData []byte, dataType jsonparser.ValueType, offset int) error { if dataType == jsonparser.String { // jsonparser removes the enclosing quotes; we need to restore them to make a valid JSON valueData = data[offset-len(valueData)-2 : offset] } var key K var value V switch typedKey := any(&key).(type) { case *string: s, err := decodeUTF8(keyData) if err != nil { return err } *typedKey = s case encoding.TextUnmarshaler: if err := typedKey.UnmarshalText(keyData); err != nil { return err } case *int, *int8, *int16, *int32, *int64, *uint, *uint8, *uint16, *uint32, *uint64: if err := json.Unmarshal(keyData, typedKey); err != nil { return err } default: // this switch takes care of wrapper types around primitive types, such as // type myType string switch reflect.TypeOf(key).Kind() { case reflect.String: s, err := decodeUTF8(keyData) if err != nil { return err } convertedKeyData := reflect.ValueOf(s).Convert(reflect.TypeOf(key)) reflect.ValueOf(&key).Elem().Set(convertedKeyData) case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: if err := json.Unmarshal(keyData, &key); err != nil { return err } default: return fmt.Errorf("unsupported key type: %T", key) } } if err := json.Unmarshal(valueData, &value); err != nil { return err } om.Set(key, value) return nil }) } func decodeUTF8(input []byte) (string, error) { remaining, offset := input, 0 runes := make([]rune, 0, len(remaining)) for len(remaining) > 0 { r, size := utf8.DecodeRune(remaining) if r == utf8.RuneError && size <= 1 { return "", fmt.Errorf("not a valid UTF-8 string (at position %d): %s", offset, string(input)) } runes = append(runes, r) remaining = remaining[size:] offset += size } return string(runes), nil } go-ordered-map-2.1.8/json_fuzz_test.go000066400000000000000000000074061444662112700177570ustar00rootroot00000000000000package orderedmap // Adapted from https://github.com/dvyukov/go-fuzz-corpus/blob/c42c1b2/json/json.go import ( "encoding/json" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func FuzzRoundTripJSON(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { for _, testCase := range []struct { name string constructor func() any // should be a function that asserts that 2 objects of the type returned by constructor are equal equalityAssertion func(*testing.T, any, any) bool }{ { name: "with a string -> string map", constructor: func() any { return &OrderedMap[string, string]{} }, equalityAssertion: assertOrderedMapsEqual[string, string], }, { name: "with a string -> int map", constructor: func() any { return &OrderedMap[string, int]{} }, equalityAssertion: assertOrderedMapsEqual[string, int], }, { name: "with a string -> any map", constructor: func() any { return &OrderedMap[string, any]{} }, equalityAssertion: assertOrderedMapsEqual[string, any], }, { name: "with a struct with map fields", constructor: func() any { return new(testFuzzStruct) }, equalityAssertion: assertTestFuzzStructEqual, }, } { t.Run(testCase.name, func(t *testing.T) { v1 := testCase.constructor() if json.Unmarshal(data, v1) != nil { return } jsonData, err := json.Marshal(v1) require.NoError(t, err) v2 := testCase.constructor() require.NoError(t, json.Unmarshal(jsonData, v2)) if !assert.True(t, testCase.equalityAssertion(t, v1, v2), "failed with input data %q", string(data)) { // look at that what the standard lib does with regular map, to help with debugging var m1 map[string]any require.NoError(t, json.Unmarshal(data, &m1)) mapJsonData, err := json.Marshal(m1) require.NoError(t, err) var m2 map[string]any require.NoError(t, json.Unmarshal(mapJsonData, &m2)) t.Logf("initial data = %s", string(data)) t.Logf("unmarshalled map = %v", m1) t.Logf("re-marshalled from map = %s", string(mapJsonData)) t.Logf("re-marshalled from test obj = %s", string(jsonData)) t.Logf("re-unmarshalled map = %s", m2) } }) } }) } // only works for fairly basic maps, that's why it's just in this file func assertOrderedMapsEqual[K comparable, V any](t *testing.T, v1, v2 any) bool { om1, ok1 := v1.(*OrderedMap[K, V]) om2, ok2 := v2.(*OrderedMap[K, V]) if !assert.True(t, ok1, "v1 not an orderedmap") || !assert.True(t, ok2, "v2 not an orderedmap") { return false } success := assert.Equal(t, om1.Len(), om2.Len(), "om1 and om2 have different lengths: %d vs %d", om1.Len(), om2.Len()) for i, pair1, pair2 := 0, om1.Oldest(), om2.Oldest(); pair1 != nil && pair2 != nil; i, pair1, pair2 = i+1, pair1.Next(), pair2.Next() { success = assert.Equal(t, pair1.Key, pair2.Key, "different keys at position %d: %v vs %v", i, pair1.Key, pair2.Key) && success success = assert.Equal(t, pair1.Value, pair2.Value, "different values at position %d: %v vs %v", i, pair1.Value, pair2.Value) && success } return success } type testFuzzStruct struct { M1 *OrderedMap[int, any] M2 *OrderedMap[int, string] M3 *OrderedMap[string, string] } func assertTestFuzzStructEqual(t *testing.T, v1, v2 any) bool { s1, ok := v1.(*testFuzzStruct) s2, ok := v2.(*testFuzzStruct) if !assert.True(t, ok, "v1 not an testFuzzStruct") || !assert.True(t, ok, "v2 not an testFuzzStruct") { return false } success := assertOrderedMapsEqual[int, any](t, s1.M1, s2.M1) success = assertOrderedMapsEqual[int, string](t, s1.M2, s2.M2) && success success = assertOrderedMapsEqual[string, string](t, s1.M3, s2.M3) && success return success } go-ordered-map-2.1.8/json_test.go000066400000000000000000000211121444662112700166670ustar00rootroot00000000000000package orderedmap import ( "encoding/json" "errors" "fmt" "strconv" "strings" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) // to test marshalling TextMarshalers and unmarshalling TextUnmarshalers type marshallable int func (m marshallable) MarshalText() ([]byte, error) { return []byte(fmt.Sprintf("#%d#", m)), nil } func (m *marshallable) UnmarshalText(text []byte) error { if len(text) < 3 { return errors.New("too short") } if text[0] != '#' || text[len(text)-1] != '#' { return errors.New("missing prefix or suffix") } value, err := strconv.Atoi(string(text[1 : len(text)-1])) if err != nil { return err } *m = marshallable(value) return nil } func TestMarshalJSON(t *testing.T) { t.Run("int key", func(t *testing.T) { om := New[int, any]() om.Set(1, "bar") om.Set(7, "baz") om.Set(2, 28) om.Set(3, 100) om.Set(4, "baz") om.Set(5, "28") om.Set(6, "100") om.Set(8, "baz") om.Set(8, "baz") om.Set(9, "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque auctor augue accumsan mi maximus, quis viverra massa pretium. Phasellus imperdiet sapien a interdum sollicitudin. Duis at commodo lectus, a lacinia sem.") b, err := json.Marshal(om) assert.NoError(t, err) assert.Equal(t, `{"1":"bar","7":"baz","2":28,"3":100,"4":"baz","5":"28","6":"100","8":"baz","9":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque auctor augue accumsan mi maximus, quis viverra massa pretium. Phasellus imperdiet sapien a interdum sollicitudin. Duis at commodo lectus, a lacinia sem."}`, string(b)) }) t.Run("string key", func(t *testing.T) { om := New[string, any]() om.Set("test", "bar") om.Set("abc", true) b, err := json.Marshal(om) assert.NoError(t, err) assert.Equal(t, `{"test":"bar","abc":true}`, string(b)) }) t.Run("typed string key", func(t *testing.T) { type myString string om := New[myString, any]() om.Set("test", "bar") om.Set("abc", true) b, err := json.Marshal(om) assert.NoError(t, err) assert.Equal(t, `{"test":"bar","abc":true}`, string(b)) }) t.Run("typed int key", func(t *testing.T) { type myInt uint32 om := New[myInt, any]() om.Set(1, "bar") om.Set(7, "baz") om.Set(2, 28) om.Set(3, 100) om.Set(4, "baz") b, err := json.Marshal(om) assert.NoError(t, err) assert.Equal(t, `{"1":"bar","7":"baz","2":28,"3":100,"4":"baz"}`, string(b)) }) t.Run("TextMarshaller key", func(t *testing.T) { om := New[marshallable, any]() om.Set(marshallable(1), "bar") om.Set(marshallable(28), true) b, err := json.Marshal(om) assert.NoError(t, err) assert.Equal(t, `{"#1#":"bar","#28#":true}`, string(b)) }) t.Run("empty map", func(t *testing.T) { om := New[string, any]() b, err := json.Marshal(om) assert.NoError(t, err) assert.Equal(t, `{}`, string(b)) }) } func TestUnmarshallJSON(t *testing.T) { t.Run("int key", func(t *testing.T) { data := `{"1":"bar","7":"baz","2":28,"3":100,"4":"baz","5":"28","6":"100","8":"baz"}` om := New[int, any]() require.NoError(t, json.Unmarshal([]byte(data), &om)) assertOrderedPairsEqual(t, om, []int{1, 7, 2, 3, 4, 5, 6, 8}, []any{"bar", "baz", float64(28), float64(100), "baz", "28", "100", "baz"}) }) t.Run("string key", func(t *testing.T) { data := `{"test":"bar","abc":true}` om := New[string, any]() require.NoError(t, json.Unmarshal([]byte(data), &om)) assertOrderedPairsEqual(t, om, []string{"test", "abc"}, []any{"bar", true}) }) t.Run("typed string key", func(t *testing.T) { data := `{"test":"bar","abc":true}` type myString string om := New[myString, any]() require.NoError(t, json.Unmarshal([]byte(data), &om)) assertOrderedPairsEqual(t, om, []myString{"test", "abc"}, []any{"bar", true}) }) t.Run("typed int key", func(t *testing.T) { data := `{"1":"bar","7":"baz","2":28,"3":100,"4":"baz","5":"28","6":"100","8":"baz"}` type myInt uint32 om := New[myInt, any]() require.NoError(t, json.Unmarshal([]byte(data), &om)) assertOrderedPairsEqual(t, om, []myInt{1, 7, 2, 3, 4, 5, 6, 8}, []any{"bar", "baz", float64(28), float64(100), "baz", "28", "100", "baz"}) }) t.Run("TextUnmarshaler key", func(t *testing.T) { data := `{"#1#":"bar","#28#":true}` om := New[marshallable, any]() require.NoError(t, json.Unmarshal([]byte(data), &om)) assertOrderedPairsEqual(t, om, []marshallable{1, 28}, []any{"bar", true}) }) t.Run("when fed with an input that's not an object", func(t *testing.T) { for _, data := range []string{"true", `["foo"]`, "42", `"foo"`} { om := New[int, any]() require.Error(t, json.Unmarshal([]byte(data), &om)) } }) t.Run("empty map", func(t *testing.T) { data := `{}` om := New[int, any]() require.NoError(t, json.Unmarshal([]byte(data), &om)) assertLenEqual(t, om, 0) }) } // const specialCharacters = "\\\\/\"\b\f\n\r\t\x00\uffff\ufffd世界\u007f\u00ff\U0010FFFF" const specialCharacters = "\uffff\ufffd世界\u007f\u00ff\U0010FFFF" func TestJSONSpecialCharacters(t *testing.T) { baselineMap := map[string]any{specialCharacters: specialCharacters} baselineData, err := json.Marshal(baselineMap) require.NoError(t, err) // baseline proves this key is supported by official json library t.Logf("specialCharacters: %#v as []rune:%v", specialCharacters, []rune(specialCharacters)) t.Logf("baseline json data: %s", baselineData) t.Run("marshal special characters", func(t *testing.T) { om := New[string, any]() om.Set(specialCharacters, specialCharacters) b, err := json.Marshal(om) require.NoError(t, err) require.Equal(t, baselineData, b) type myString string om2 := New[myString, myString]() om2.Set(specialCharacters, specialCharacters) b, err = json.Marshal(om2) require.NoError(t, err) require.Equal(t, baselineData, b) }) t.Run("unmarshall special characters", func(t *testing.T) { om := New[string, any]() require.NoError(t, json.Unmarshal(baselineData, &om)) assertOrderedPairsEqual(t, om, []string{specialCharacters}, []any{specialCharacters}) type myString string om2 := New[myString, myString]() require.NoError(t, json.Unmarshal(baselineData, &om2)) assertOrderedPairsEqual(t, om2, []myString{specialCharacters}, []myString{specialCharacters}) }) } // to test structs that have nested map fields type nestedMaps struct { X int `json:"x" yaml:"x"` M *OrderedMap[string, []*OrderedMap[int, *OrderedMap[string, any]]] `json:"m" yaml:"m"` } func TestJSONRoundTrip(t *testing.T) { for _, testCase := range []struct { name string input string targetFactory func() any isPrettyPrinted bool }{ { name: "", input: `{ "x": 28, "m": { "foo": [ { "12": { "i": 12, "b": true, "n": null, "m": { "a": "b", "c": 28 } }, "28": { "a": false, "b": [ 1, 2, 3 ] } }, { "3": { "c": null, "d": 87 }, "4": { "e": true }, "5": { "f": 4, "g": 5, "h": 6 } } ], "bar": [ { "5": { "foo": "bar" } } ] } }`, targetFactory: func() any { return &nestedMaps{} }, isPrettyPrinted: true, }, { name: "with UTF-8 special chars in key", input: `{"�":0}`, targetFactory: func() any { return &OrderedMap[string, int]{} }, }, } { t.Run(testCase.name, func(t *testing.T) { target := testCase.targetFactory() require.NoError(t, json.Unmarshal([]byte(testCase.input), target)) var ( out []byte err error ) if testCase.isPrettyPrinted { out, err = json.MarshalIndent(target, "", " ") } else { out, err = json.Marshal(target) } if assert.NoError(t, err) { assert.Equal(t, strings.TrimSpace(testCase.input), string(out)) } }) } } func BenchmarkMarshalJSON(b *testing.B) { om := New[int, any]() om.Set(1, "bar") om.Set(7, "baz") om.Set(2, 28) om.Set(3, 100) om.Set(4, "baz") om.Set(5, "28") om.Set(6, "100") om.Set(8, "baz") om.Set(8, "baz") b.ResetTimer() for i := 0; i < b.N; i++ { _, _ = json.Marshal(om) } } go-ordered-map-2.1.8/orderedmap.go000066400000000000000000000212051444662112700170040ustar00rootroot00000000000000// Package orderedmap implements an ordered map, i.e. a map that also keeps track of // the order in which keys were inserted. // // All operations are constant-time. // // Github repo: https://github.com/wk8/go-ordered-map // package orderedmap import ( "fmt" list "github.com/bahlo/generic-list-go" ) type Pair[K comparable, V any] struct { Key K Value V element *list.Element[*Pair[K, V]] } type OrderedMap[K comparable, V any] struct { pairs map[K]*Pair[K, V] list *list.List[*Pair[K, V]] } type initConfig[K comparable, V any] struct { capacity int initialData []Pair[K, V] } type InitOption[K comparable, V any] func(config *initConfig[K, V]) // WithCapacity allows giving a capacity hint for the map, akin to the standard make(map[K]V, capacity). func WithCapacity[K comparable, V any](capacity int) InitOption[K, V] { return func(c *initConfig[K, V]) { c.capacity = capacity } } // WithInitialData allows passing in initial data for the map. func WithInitialData[K comparable, V any](initialData ...Pair[K, V]) InitOption[K, V] { return func(c *initConfig[K, V]) { c.initialData = initialData if c.capacity < len(initialData) { c.capacity = len(initialData) } } } // New creates a new OrderedMap. // options can either be one or several InitOption[K, V], or a single integer, // which is then interpreted as a capacity hint, à la make(map[K]V, capacity). func New[K comparable, V any](options ...any) *OrderedMap[K, V] { //nolint:varnamelen orderedMap := &OrderedMap[K, V]{} var config initConfig[K, V] for _, untypedOption := range options { switch option := untypedOption.(type) { case int: if len(options) != 1 { invalidOption() } config.capacity = option case InitOption[K, V]: option(&config) default: invalidOption() } } orderedMap.initialize(config.capacity) orderedMap.AddPairs(config.initialData...) return orderedMap } const invalidOptionMessage = `when using orderedmap.New[K,V]() with options, either provide one or several InitOption[K, V]; or a single integer which is then interpreted as a capacity hint, à la make(map[K]V, capacity).` //nolint:lll func invalidOption() { panic(invalidOptionMessage) } func (om *OrderedMap[K, V]) initialize(capacity int) { om.pairs = make(map[K]*Pair[K, V], capacity) om.list = list.New[*Pair[K, V]]() } // Get looks for the given key, and returns the value associated with it, // or V's nil value if not found. The boolean it returns says whether the key is present in the map. func (om *OrderedMap[K, V]) Get(key K) (val V, present bool) { if pair, present := om.pairs[key]; present { return pair.Value, true } return } // Load is an alias for Get, mostly to present an API similar to `sync.Map`'s. func (om *OrderedMap[K, V]) Load(key K) (V, bool) { return om.Get(key) } // Value returns the value associated with the given key or the zero value. func (om *OrderedMap[K, V]) Value(key K) (val V) { if pair, present := om.pairs[key]; present { val = pair.Value } return } // GetPair looks for the given key, and returns the pair associated with it, // or nil if not found. The Pair struct can then be used to iterate over the ordered map // from that point, either forward or backward. func (om *OrderedMap[K, V]) GetPair(key K) *Pair[K, V] { return om.pairs[key] } // Set sets the key-value pair, and returns what `Get` would have returned // on that key prior to the call to `Set`. func (om *OrderedMap[K, V]) Set(key K, value V) (val V, present bool) { if pair, present := om.pairs[key]; present { oldValue := pair.Value pair.Value = value return oldValue, true } pair := &Pair[K, V]{ Key: key, Value: value, } pair.element = om.list.PushBack(pair) om.pairs[key] = pair return } // AddPairs allows setting multiple pairs at a time. It's equivalent to calling // Set on each pair sequentially. func (om *OrderedMap[K, V]) AddPairs(pairs ...Pair[K, V]) { for _, pair := range pairs { om.Set(pair.Key, pair.Value) } } // Store is an alias for Set, mostly to present an API similar to `sync.Map`'s. func (om *OrderedMap[K, V]) Store(key K, value V) (V, bool) { return om.Set(key, value) } // Delete removes the key-value pair, and returns what `Get` would have returned // on that key prior to the call to `Delete`. func (om *OrderedMap[K, V]) Delete(key K) (val V, present bool) { if pair, present := om.pairs[key]; present { om.list.Remove(pair.element) delete(om.pairs, key) return pair.Value, true } return } // Len returns the length of the ordered map. func (om *OrderedMap[K, V]) Len() int { if om == nil || om.pairs == nil { return 0 } return len(om.pairs) } // Oldest returns a pointer to the oldest pair. It's meant to be used to iterate on the ordered map's // pairs from the oldest to the newest, e.g.: // for pair := orderedMap.Oldest(); pair != nil; pair = pair.Next() { fmt.Printf("%v => %v\n", pair.Key, pair.Value) } func (om *OrderedMap[K, V]) Oldest() *Pair[K, V] { if om == nil || om.list == nil { return nil } return listElementToPair(om.list.Front()) } // Newest returns a pointer to the newest pair. It's meant to be used to iterate on the ordered map's // pairs from the newest to the oldest, e.g.: // for pair := orderedMap.Oldest(); pair != nil; pair = pair.Next() { fmt.Printf("%v => %v\n", pair.Key, pair.Value) } func (om *OrderedMap[K, V]) Newest() *Pair[K, V] { if om == nil || om.list == nil { return nil } return listElementToPair(om.list.Back()) } // Next returns a pointer to the next pair. func (p *Pair[K, V]) Next() *Pair[K, V] { return listElementToPair(p.element.Next()) } // Prev returns a pointer to the previous pair. func (p *Pair[K, V]) Prev() *Pair[K, V] { return listElementToPair(p.element.Prev()) } func listElementToPair[K comparable, V any](element *list.Element[*Pair[K, V]]) *Pair[K, V] { if element == nil { return nil } return element.Value } // KeyNotFoundError may be returned by functions in this package when they're called with keys that are not present // in the map. type KeyNotFoundError[K comparable] struct { MissingKey K } func (e *KeyNotFoundError[K]) Error() string { return fmt.Sprintf("missing key: %v", e.MissingKey) } // MoveAfter moves the value associated with key to its new position after the one associated with markKey. // Returns an error iff key or markKey are not present in the map. If an error is returned, // it will be a KeyNotFoundError. func (om *OrderedMap[K, V]) MoveAfter(key, markKey K) error { elements, err := om.getElements(key, markKey) if err != nil { return err } om.list.MoveAfter(elements[0], elements[1]) return nil } // MoveBefore moves the value associated with key to its new position before the one associated with markKey. // Returns an error iff key or markKey are not present in the map. If an error is returned, // it will be a KeyNotFoundError. func (om *OrderedMap[K, V]) MoveBefore(key, markKey K) error { elements, err := om.getElements(key, markKey) if err != nil { return err } om.list.MoveBefore(elements[0], elements[1]) return nil } func (om *OrderedMap[K, V]) getElements(keys ...K) ([]*list.Element[*Pair[K, V]], error) { elements := make([]*list.Element[*Pair[K, V]], len(keys)) for i, k := range keys { pair, present := om.pairs[k] if !present { return nil, &KeyNotFoundError[K]{k} } elements[i] = pair.element } return elements, nil } // MoveToBack moves the value associated with key to the back of the ordered map, // i.e. makes it the newest pair in the map. // Returns an error iff key is not present in the map. If an error is returned, // it will be a KeyNotFoundError. func (om *OrderedMap[K, V]) MoveToBack(key K) error { _, err := om.GetAndMoveToBack(key) return err } // MoveToFront moves the value associated with key to the front of the ordered map, // i.e. makes it the oldest pair in the map. // Returns an error iff key is not present in the map. If an error is returned, // it will be a KeyNotFoundError. func (om *OrderedMap[K, V]) MoveToFront(key K) error { _, err := om.GetAndMoveToFront(key) return err } // GetAndMoveToBack combines Get and MoveToBack in the same call. If an error is returned, // it will be a KeyNotFoundError. func (om *OrderedMap[K, V]) GetAndMoveToBack(key K) (val V, err error) { if pair, present := om.pairs[key]; present { val = pair.Value om.list.MoveToBack(pair.element) } else { err = &KeyNotFoundError[K]{key} } return } // GetAndMoveToFront combines Get and MoveToFront in the same call. If an error is returned, // it will be a KeyNotFoundError. func (om *OrderedMap[K, V]) GetAndMoveToFront(key K) (val V, err error) { if pair, present := om.pairs[key]; present { val = pair.Value om.list.MoveToFront(pair.element) } else { err = &KeyNotFoundError[K]{key} } return } go-ordered-map-2.1.8/orderedmap_test.go000066400000000000000000000210341444662112700200430ustar00rootroot00000000000000package orderedmap import ( "fmt" "testing" "github.com/stretchr/testify/assert" ) func TestBasicFeatures(t *testing.T) { n := 100 om := New[int, int]() // set(i, 2 * i) for i := 0; i < n; i++ { assertLenEqual(t, om, i) oldValue, present := om.Set(i, 2*i) assertLenEqual(t, om, i+1) assert.Equal(t, 0, oldValue) assert.False(t, present) } // get what we just set for i := 0; i < n; i++ { value, present := om.Get(i) assert.Equal(t, 2*i, value) assert.Equal(t, value, om.Value(i)) assert.True(t, present) } // get pairs of what we just set for i := 0; i < n; i++ { pair := om.GetPair(i) assert.NotNil(t, pair) assert.Equal(t, 2*i, pair.Value) } // forward iteration i := 0 for pair := om.Oldest(); pair != nil; pair = pair.Next() { assert.Equal(t, i, pair.Key) assert.Equal(t, 2*i, pair.Value) i++ } // backward iteration i = n - 1 for pair := om.Newest(); pair != nil; pair = pair.Prev() { assert.Equal(t, i, pair.Key) assert.Equal(t, 2*i, pair.Value) i-- } // forward iteration starting from known key i = 42 for pair := om.GetPair(i); pair != nil; pair = pair.Next() { assert.Equal(t, i, pair.Key) assert.Equal(t, 2*i, pair.Value) i++ } // double values for pairs with even keys for j := 0; j < n/2; j++ { i = 2 * j oldValue, present := om.Set(i, 4*i) assert.Equal(t, 2*i, oldValue) assert.True(t, present) } // and delete pairs with odd keys for j := 0; j < n/2; j++ { i = 2*j + 1 assertLenEqual(t, om, n-j) value, present := om.Delete(i) assertLenEqual(t, om, n-j-1) assert.Equal(t, 2*i, value) assert.True(t, present) // deleting again shouldn't change anything value, present = om.Delete(i) assertLenEqual(t, om, n-j-1) assert.Equal(t, 0, value) assert.False(t, present) } // get the whole range for j := 0; j < n/2; j++ { i = 2 * j value, present := om.Get(i) assert.Equal(t, 4*i, value) assert.Equal(t, value, om.Value(i)) assert.True(t, present) i = 2*j + 1 value, present = om.Get(i) assert.Equal(t, 0, value) assert.Equal(t, value, om.Value(i)) assert.False(t, present) } // check iterations again i = 0 for pair := om.Oldest(); pair != nil; pair = pair.Next() { assert.Equal(t, i, pair.Key) assert.Equal(t, 4*i, pair.Value) i += 2 } i = 2 * ((n - 1) / 2) for pair := om.Newest(); pair != nil; pair = pair.Prev() { assert.Equal(t, i, pair.Key) assert.Equal(t, 4*i, pair.Value) i -= 2 } } func TestUpdatingDoesntChangePairsOrder(t *testing.T) { om := New[string, any]() om.Set("foo", "bar") om.Set("wk", 28) om.Set("po", 100) om.Set("bar", "baz") oldValue, present := om.Set("po", 102) assert.Equal(t, 100, oldValue) assert.True(t, present) assertOrderedPairsEqual(t, om, []string{"foo", "wk", "po", "bar"}, []any{"bar", 28, 102, "baz"}) } func TestDeletingAndReinsertingChangesPairsOrder(t *testing.T) { om := New[string, any]() om.Set("foo", "bar") om.Set("wk", 28) om.Set("po", 100) om.Set("bar", "baz") // delete a pair oldValue, present := om.Delete("po") assert.Equal(t, 100, oldValue) assert.True(t, present) // re-insert the same pair oldValue, present = om.Set("po", 100) assert.Nil(t, oldValue) assert.False(t, present) assertOrderedPairsEqual(t, om, []string{"foo", "wk", "bar", "po"}, []any{"bar", 28, "baz", 100}) } func TestEmptyMapOperations(t *testing.T) { om := New[string, any]() oldValue, present := om.Get("foo") assert.Nil(t, oldValue) assert.Nil(t, om.Value("foo")) assert.False(t, present) oldValue, present = om.Delete("bar") assert.Nil(t, oldValue) assert.False(t, present) assertLenEqual(t, om, 0) assert.Nil(t, om.Oldest()) assert.Nil(t, om.Newest()) } type dummyTestStruct struct { value string } func TestPackUnpackStructs(t *testing.T) { om := New[string, dummyTestStruct]() om.Set("foo", dummyTestStruct{"foo!"}) om.Set("bar", dummyTestStruct{"bar!"}) value, present := om.Get("foo") assert.True(t, present) assert.Equal(t, value, om.Value("foo")) if assert.NotNil(t, value) { assert.Equal(t, "foo!", value.value) } value, present = om.Set("bar", dummyTestStruct{"baz!"}) assert.True(t, present) if assert.NotNil(t, value) { assert.Equal(t, "bar!", value.value) } value, present = om.Get("bar") assert.Equal(t, value, om.Value("bar")) assert.True(t, present) if assert.NotNil(t, value) { assert.Equal(t, "baz!", value.value) } } // shamelessly stolen from https://github.com/python/cpython/blob/e19a91e45fd54a56e39c2d12e6aaf4757030507f/Lib/test/test_ordered_dict.py#L55-L61 func TestShuffle(t *testing.T) { ranLen := 100 for _, n := range []int{0, 10, 20, 100, 1000, 10000} { t.Run(fmt.Sprintf("shuffle test with %d items", n), func(t *testing.T) { om := New[string, string]() keys := make([]string, n) values := make([]string, n) for i := 0; i < n; i++ { // we prefix with the number to ensure that we don't get any duplicates keys[i] = fmt.Sprintf("%d_%s", i, randomHexString(t, ranLen)) values[i] = randomHexString(t, ranLen) value, present := om.Set(keys[i], values[i]) assert.Equal(t, "", value) assert.False(t, present) } assertOrderedPairsEqual(t, om, keys, values) }) } } func TestMove(t *testing.T) { om := New[int, any]() om.Set(1, "bar") om.Set(2, 28) om.Set(3, 100) om.Set(4, "baz") om.Set(5, "28") om.Set(6, "100") om.Set(7, "baz") om.Set(8, "baz") err := om.MoveAfter(2, 3) assert.Nil(t, err) assertOrderedPairsEqual(t, om, []int{1, 3, 2, 4, 5, 6, 7, 8}, []any{"bar", 100, 28, "baz", "28", "100", "baz", "baz"}) err = om.MoveBefore(6, 4) assert.Nil(t, err) assertOrderedPairsEqual(t, om, []int{1, 3, 2, 6, 4, 5, 7, 8}, []any{"bar", 100, 28, "100", "baz", "28", "baz", "baz"}) err = om.MoveToBack(3) assert.Nil(t, err) assertOrderedPairsEqual(t, om, []int{1, 2, 6, 4, 5, 7, 8, 3}, []any{"bar", 28, "100", "baz", "28", "baz", "baz", 100}) err = om.MoveToFront(5) assert.Nil(t, err) assertOrderedPairsEqual(t, om, []int{5, 1, 2, 6, 4, 7, 8, 3}, []any{"28", "bar", 28, "100", "baz", "baz", "baz", 100}) err = om.MoveToFront(100) assert.Equal(t, &KeyNotFoundError[int]{100}, err) } func TestGetAndMove(t *testing.T) { om := New[int, any]() om.Set(1, "bar") om.Set(2, 28) om.Set(3, 100) om.Set(4, "baz") om.Set(5, "28") om.Set(6, "100") om.Set(7, "baz") om.Set(8, "baz") value, err := om.GetAndMoveToBack(3) assert.Nil(t, err) assert.Equal(t, 100, value) assertOrderedPairsEqual(t, om, []int{1, 2, 4, 5, 6, 7, 8, 3}, []any{"bar", 28, "baz", "28", "100", "baz", "baz", 100}) value, err = om.GetAndMoveToFront(5) assert.Nil(t, err) assert.Equal(t, "28", value) assertOrderedPairsEqual(t, om, []int{5, 1, 2, 4, 6, 7, 8, 3}, []any{"28", "bar", 28, "baz", "100", "baz", "baz", 100}) value, err = om.GetAndMoveToBack(100) assert.Equal(t, &KeyNotFoundError[int]{100}, err) } func TestAddPairs(t *testing.T) { om := New[int, any]() om.AddPairs( Pair[int, any]{ Key: 28, Value: "foo", }, Pair[int, any]{ Key: 12, Value: "bar", }, Pair[int, any]{ Key: 28, Value: "baz", }, ) assertOrderedPairsEqual(t, om, []int{28, 12}, []any{"baz", "bar"}) } // sadly, we can't test the "actual" capacity here, see https://github.com/golang/go/issues/52157 func TestNewWithCapacity(t *testing.T) { zero := New[int, string](0) assert.Empty(t, zero.Len()) assert.PanicsWithValue(t, invalidOptionMessage, func() { _ = New[int, string](1, 2) }) assert.PanicsWithValue(t, invalidOptionMessage, func() { _ = New[int, string](1, 2, 3) }) om := New[int, string](-1) om.Set(1337, "quarante-deux") assert.Equal(t, 1, om.Len()) } func TestNewWithOptions(t *testing.T) { t.Run("wih capacity", func(t *testing.T) { om := New[string, any](WithCapacity[string, any](98)) assert.Equal(t, 0, om.Len()) }) t.Run("with initial data", func(t *testing.T) { om := New[string, int](WithInitialData( Pair[string, int]{ Key: "a", Value: 1, }, Pair[string, int]{ Key: "b", Value: 2, }, Pair[string, int]{ Key: "c", Value: 3, }, )) assertOrderedPairsEqual(t, om, []string{"a", "b", "c"}, []int{1, 2, 3}) }) t.Run("with an invalid option type", func(t *testing.T) { assert.PanicsWithValue(t, invalidOptionMessage, func() { _ = New[int, string]("foo") }) }) } func TestNilMap(t *testing.T) { // we want certain behaviors of a nil ordered map to be the same as they are for standard nil maps var om *OrderedMap[int, any] t.Run("len", func(t *testing.T) { assert.Equal(t, 0, om.Len()) }) t.Run("iterating - akin to range", func(t *testing.T) { assert.Nil(t, om.Oldest()) assert.Nil(t, om.Newest()) }) } go-ordered-map-2.1.8/testdata/000077500000000000000000000000001444662112700161445ustar00rootroot00000000000000go-ordered-map-2.1.8/testdata/fuzz/000077500000000000000000000000001444662112700171425ustar00rootroot00000000000000go-ordered-map-2.1.8/testdata/fuzz/FuzzRoundTripJSON/000077500000000000000000000000001444662112700224415ustar00rootroot0000000000000062c005f96216d8ba8f62ac0799dfc1a6893e68418238a831ee79cd9c39b4cfc6000066400000000000000000000000471444662112700331510ustar00rootroot00000000000000go-ordered-map-2.1.8/testdata/fuzz/FuzzRoundTripJSONgo test fuzz v1 []byte("{\"\xcc\":0}") 8093511184ad3e258aa13b957e75ff26c7fae64672dae0c0bc0a9fa5b61a05e7000066400000000000000000000000351444662112700332320ustar00rootroot00000000000000go-ordered-map-2.1.8/testdata/fuzz/FuzzRoundTripJSONgo test fuzz v1 []byte("{}") go-ordered-map-2.1.8/utils_test.go000066400000000000000000000043141444662112700170630ustar00rootroot00000000000000package orderedmap import ( "crypto/rand" "encoding/hex" "fmt" "testing" "github.com/stretchr/testify/assert" ) // assertOrderedPairsEqual asserts that the map contains the given keys and values // from oldest to newest. func assertOrderedPairsEqual[K comparable, V any]( t *testing.T, orderedMap *OrderedMap[K, V], expectedKeys []K, expectedValues []V, ) { t.Helper() assertOrderedPairsEqualFromNewest(t, orderedMap, expectedKeys, expectedValues) assertOrderedPairsEqualFromOldest(t, orderedMap, expectedKeys, expectedValues) } func assertOrderedPairsEqualFromNewest[K comparable, V any]( t *testing.T, orderedMap *OrderedMap[K, V], expectedKeys []K, expectedValues []V, ) { t.Helper() if assert.Equal(t, len(expectedKeys), len(expectedValues)) && assert.Equal(t, len(expectedKeys), orderedMap.Len()) { i := orderedMap.Len() - 1 for pair := orderedMap.Newest(); pair != nil; pair = pair.Prev() { assert.Equal(t, expectedKeys[i], pair.Key, "from newest index=%d on key", i) assert.Equal(t, expectedValues[i], pair.Value, "from newest index=%d on value", i) i-- } } } func assertOrderedPairsEqualFromOldest[K comparable, V any]( t *testing.T, orderedMap *OrderedMap[K, V], expectedKeys []K, expectedValues []V, ) { t.Helper() if assert.Equal(t, len(expectedKeys), len(expectedValues)) && assert.Equal(t, len(expectedKeys), orderedMap.Len()) { i := 0 for pair := orderedMap.Oldest(); pair != nil; pair = pair.Next() { assert.Equal(t, expectedKeys[i], pair.Key, "from oldest index=%d on key", i) assert.Equal(t, expectedValues[i], pair.Value, "from oldest index=%d on value", i) i++ } } } func assertLenEqual[K comparable, V any](t *testing.T, orderedMap *OrderedMap[K, V], expectedLen int) { t.Helper() assert.Equal(t, expectedLen, orderedMap.Len()) // also check the list length, for good measure assert.Equal(t, expectedLen, orderedMap.list.Len()) } func randomHexString(t *testing.T, length int) string { t.Helper() b := length / 2 //nolint:gomnd randBytes := make([]byte, b) if n, err := rand.Read(randBytes); err != nil || n != b { if err == nil { err = fmt.Errorf("only got %v random bytes, expected %v", n, b) } t.Fatal(err) } return hex.EncodeToString(randBytes) } go-ordered-map-2.1.8/yaml.go000066400000000000000000000026561444662112700156350ustar00rootroot00000000000000package orderedmap import ( "fmt" "gopkg.in/yaml.v3" ) var ( _ yaml.Marshaler = &OrderedMap[int, any]{} _ yaml.Unmarshaler = &OrderedMap[int, any]{} ) // MarshalYAML implements the yaml.Marshaler interface. func (om *OrderedMap[K, V]) MarshalYAML() (interface{}, error) { if om == nil { return []byte("null"), nil } node := yaml.Node{ Kind: yaml.MappingNode, } for pair := om.Oldest(); pair != nil; pair = pair.Next() { key, value := pair.Key, pair.Value keyNode := &yaml.Node{} // serialize key to yaml, then deserialize it back into the node // this is a hack to get the correct tag for the key if err := keyNode.Encode(key); err != nil { return nil, err } valueNode := &yaml.Node{} if err := valueNode.Encode(value); err != nil { return nil, err } node.Content = append(node.Content, keyNode, valueNode) } return &node, nil } // UnmarshalYAML implements the yaml.Unmarshaler interface. func (om *OrderedMap[K, V]) UnmarshalYAML(value *yaml.Node) error { if value.Kind != yaml.MappingNode { return fmt.Errorf("pipeline must contain YAML mapping, has %v", value.Kind) } if om.list == nil { om.initialize(0) } for index := 0; index < len(value.Content); index += 2 { var key K var val V if err := value.Content[index].Decode(&key); err != nil { return err } if err := value.Content[index+1].Decode(&val); err != nil { return err } om.Set(key, val) } return nil } go-ordered-map-2.1.8/yaml_fuzz_test.go000066400000000000000000000046351444662112700177510ustar00rootroot00000000000000package orderedmap // Adapted from https://github.com/dvyukov/go-fuzz-corpus/blob/c42c1b2/json/json.go import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "gopkg.in/yaml.v3" "testing" ) func FuzzRoundTripYAML(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { for _, testCase := range []struct { name string constructor func() any // should be a function that asserts that 2 objects of the type returned by constructor are equal equalityAssertion func(*testing.T, any, any) bool }{ { name: "with a string -> string map", constructor: func() any { return &OrderedMap[string, string]{} }, equalityAssertion: assertOrderedMapsEqual[string, string], }, { name: "with a string -> int map", constructor: func() any { return &OrderedMap[string, int]{} }, equalityAssertion: assertOrderedMapsEqual[string, int], }, { name: "with a string -> any map", constructor: func() any { return &OrderedMap[string, any]{} }, equalityAssertion: assertOrderedMapsEqual[string, any], }, { name: "with a struct with map fields", constructor: func() any { return new(testFuzzStruct) }, equalityAssertion: assertTestFuzzStructEqual, }, } { t.Run(testCase.name, func(t *testing.T) { v1 := testCase.constructor() if yaml.Unmarshal(data, v1) != nil { return } t.Log(data) t.Log(v1) yamlData, err := yaml.Marshal(v1) require.NoError(t, err) t.Log(string(yamlData)) v2 := testCase.constructor() err = yaml.Unmarshal(yamlData, v2) if err != nil { t.Log(string(yamlData)) t.Fatal(err) } if !assert.True(t, testCase.equalityAssertion(t, v1, v2), "failed with input data %q", string(data)) { // look at that what the standard lib does with regular map, to help with debugging var m1 map[string]any require.NoError(t, yaml.Unmarshal(data, &m1)) mapJsonData, err := yaml.Marshal(m1) require.NoError(t, err) var m2 map[string]any require.NoError(t, yaml.Unmarshal(mapJsonData, &m2)) t.Logf("initial data = %s", string(data)) t.Logf("unmarshalled map = %v", m1) t.Logf("re-marshalled from map = %s", string(mapJsonData)) t.Logf("re-marshalled from test obj = %s", string(yamlData)) t.Logf("re-unmarshalled map = %s", m2) } }) } }) } go-ordered-map-2.1.8/yaml_test.go000066400000000000000000000163351444662112700166730ustar00rootroot00000000000000package orderedmap import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "gopkg.in/yaml.v3" "testing" ) func TestMarshalYAML(t *testing.T) { t.Run("int key", func(t *testing.T) { om := New[int, any]() om.Set(1, "bar") om.Set(7, "baz") om.Set(2, 28) om.Set(3, 100) om.Set(4, "baz") om.Set(5, "28") om.Set(6, "100") om.Set(8, "baz") om.Set(8, "baz") om.Set(9, "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque auctor augue accumsan mi maximus, quis viverra massa pretium. Phasellus imperdiet sapien a interdum sollicitudin. Duis at commodo lectus, a lacinia sem.") b, err := yaml.Marshal(om) expected := `1: bar 7: baz 2: 28 3: 100 4: baz 5: "28" 6: "100" 8: baz 9: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque auctor augue accumsan mi maximus, quis viverra massa pretium. Phasellus imperdiet sapien a interdum sollicitudin. Duis at commodo lectus, a lacinia sem. ` assert.NoError(t, err) assert.Equal(t, expected, string(b)) }) t.Run("string key", func(t *testing.T) { om := New[string, any]() om.Set("test", "bar") om.Set("abc", true) b, err := yaml.Marshal(om) assert.NoError(t, err) expected := `test: bar abc: true ` assert.Equal(t, expected, string(b)) }) t.Run("typed string key", func(t *testing.T) { type myString string om := New[myString, any]() om.Set("test", "bar") om.Set("abc", true) b, err := yaml.Marshal(om) assert.NoError(t, err) assert.Equal(t, `test: bar abc: true `, string(b)) }) t.Run("typed int key", func(t *testing.T) { type myInt uint32 om := New[myInt, any]() om.Set(1, "bar") om.Set(7, "baz") om.Set(2, 28) om.Set(3, 100) om.Set(4, "baz") b, err := yaml.Marshal(om) assert.NoError(t, err) assert.Equal(t, `1: bar 7: baz 2: 28 3: 100 4: baz `, string(b)) }) t.Run("TextMarshaller key", func(t *testing.T) { om := New[marshallable, any]() om.Set(marshallable(1), "bar") om.Set(marshallable(28), true) b, err := yaml.Marshal(om) assert.NoError(t, err) assert.Equal(t, `'#1#': bar '#28#': true `, string(b)) }) t.Run("empty map with 0 elements", func(t *testing.T) { om := New[string, any]() b, err := yaml.Marshal(om) assert.NoError(t, err) assert.Equal(t, "{}\n", string(b)) }) t.Run("empty map with no elements (null)", func(t *testing.T) { om := &OrderedMap[string, string]{} b, err := yaml.Marshal(om) assert.NoError(t, err) assert.Equal(t, "{}\n", string(b)) }) } func TestUnmarshallYAML(t *testing.T) { t.Run("int key", func(t *testing.T) { data := ` 1: bar 7: baz 2: 28 3: 100 4: baz 5: "28" 6: "100" 8: baz ` om := New[int, any]() require.NoError(t, yaml.Unmarshal([]byte(data), &om)) assertOrderedPairsEqual(t, om, []int{1, 7, 2, 3, 4, 5, 6, 8}, []any{"bar", "baz", 28, 100, "baz", "28", "100", "baz"}) // serialize back to yaml to make sure things are equal }) t.Run("string key", func(t *testing.T) { data := `{"test":"bar","abc":true}` om := New[string, any]() require.NoError(t, yaml.Unmarshal([]byte(data), &om)) assertOrderedPairsEqual(t, om, []string{"test", "abc"}, []any{"bar", true}) }) t.Run("typed string key", func(t *testing.T) { data := `{"test":"bar","abc":true}` type myString string om := New[myString, any]() require.NoError(t, yaml.Unmarshal([]byte(data), &om)) assertOrderedPairsEqual(t, om, []myString{"test", "abc"}, []any{"bar", true}) }) t.Run("typed int key", func(t *testing.T) { data := ` 1: bar 7: baz 2: 28 3: 100 4: baz 5: "28" 6: "100" 8: baz ` type myInt uint32 om := New[myInt, any]() require.NoError(t, yaml.Unmarshal([]byte(data), &om)) assertOrderedPairsEqual(t, om, []myInt{1, 7, 2, 3, 4, 5, 6, 8}, []any{"bar", "baz", 28, 100, "baz", "28", "100", "baz"}) }) t.Run("TextUnmarshaler key", func(t *testing.T) { data := `{"#1#":"bar","#28#":true}` om := New[marshallable, any]() require.NoError(t, yaml.Unmarshal([]byte(data), &om)) assertOrderedPairsEqual(t, om, []marshallable{1, 28}, []any{"bar", true}) }) t.Run("when fed with an input that's not an object", func(t *testing.T) { for _, data := range []string{"true", `["foo"]`, "42", `"foo"`} { om := New[int, any]() require.Error(t, yaml.Unmarshal([]byte(data), &om)) } }) t.Run("empty map", func(t *testing.T) { data := `{}` om := New[int, any]() require.NoError(t, yaml.Unmarshal([]byte(data), &om)) assertLenEqual(t, om, 0) }) } func TestYAMLSpecialCharacters(t *testing.T) { baselineMap := map[string]any{specialCharacters: specialCharacters} baselineData, err := yaml.Marshal(baselineMap) require.NoError(t, err) // baseline proves this key is supported by official yaml library t.Logf("specialCharacters: %#v as []rune:%v", specialCharacters, []rune(specialCharacters)) t.Logf("baseline yaml data: %s", baselineData) t.Run("marshal special characters", func(t *testing.T) { om := New[string, any]() om.Set(specialCharacters, specialCharacters) b, err := yaml.Marshal(om) require.NoError(t, err) require.Equal(t, baselineData, b) type myString string om2 := New[myString, myString]() om2.Set(specialCharacters, specialCharacters) b, err = yaml.Marshal(om2) require.NoError(t, err) require.Equal(t, baselineData, b) }) t.Run("unmarshall special characters", func(t *testing.T) { om := New[string, any]() require.NoError(t, yaml.Unmarshal(baselineData, &om)) assertOrderedPairsEqual(t, om, []string{specialCharacters}, []any{specialCharacters}) type myString string om2 := New[myString, myString]() require.NoError(t, yaml.Unmarshal(baselineData, &om2)) assertOrderedPairsEqual(t, om2, []myString{specialCharacters}, []myString{specialCharacters}) }) } func TestYAMLRoundTrip(t *testing.T) { for _, testCase := range []struct { name string input string targetFactory func() any }{ { name: "empty map", input: "{}\n", targetFactory: func() any { return &OrderedMap[string, any]{} }, }, { name: "", input: `x: 28 m: bar: - 5: foo: bar foo: - 12: b: true i: 12 m: a: b c: 28 "n": null 28: a: false b: - 1 - 2 - 3 - 3: c: null d: 87 4: e: true 5: f: 4 g: 5 h: 6 `, targetFactory: func() any { return &nestedMaps{} }, }, { name: "with UTF-8 special chars in key", input: "�: 0\n", targetFactory: func() any { return &OrderedMap[string, int]{} }, }, } { t.Run(testCase.name, func(t *testing.T) { target := testCase.targetFactory() require.NoError(t, yaml.Unmarshal([]byte(testCase.input), target)) var ( out []byte err error ) out, err = yaml.Marshal(target) if assert.NoError(t, err) { assert.Equal(t, testCase.input, string(out)) } }) } } func BenchmarkMarshalYAML(b *testing.B) { om := New[int, any]() om.Set(1, "bar") om.Set(7, "baz") om.Set(2, 28) om.Set(3, 100) om.Set(4, "baz") om.Set(5, "28") om.Set(6, "100") om.Set(8, "baz") om.Set(8, "baz") b.ResetTimer() for i := 0; i < b.N; i++ { _, _ = yaml.Marshal(om) } }