2018-10-20 05:16:18 -04:00
|
|
|
// Copyright 2018 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
package metadecoders
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
qt "github.com/frankban/quicktest"
|
2018-10-20 05:16:18 -04:00
|
|
|
)
|
|
|
|
|
2021-12-02 11:30:36 -05:00
|
|
|
func TestUnmarshalXML(t *testing.T) {
|
|
|
|
c := qt.New(t)
|
|
|
|
|
|
|
|
xmlDoc := `<?xml version="1.0" encoding="utf-8" standalone="yes"?>
|
|
|
|
<rss version="2.0"
|
|
|
|
xmlns:atom="http://www.w3.org/2005/Atom">
|
|
|
|
<channel>
|
|
|
|
<title>Example feed</title>
|
|
|
|
<link>https://example.com/</link>
|
|
|
|
<description>Example feed</description>
|
|
|
|
<generator>Hugo -- gohugo.io</generator>
|
|
|
|
<language>en-us</language>
|
|
|
|
<copyright>Example</copyright>
|
|
|
|
<lastBuildDate>Fri, 08 Jan 2021 14:44:10 +0000</lastBuildDate>
|
|
|
|
<atom:link href="https://example.com/feed.xml" rel="self" type="application/rss+xml"/>
|
|
|
|
<item>
|
|
|
|
<title>Example title</title>
|
|
|
|
<link>https://example.com/2021/11/30/example-title/</link>
|
|
|
|
<pubDate>Tue, 30 Nov 2021 15:00:00 +0000</pubDate>
|
|
|
|
<guid>https://example.com/2021/11/30/example-title/</guid>
|
|
|
|
<description>Example description</description>
|
|
|
|
</item>
|
|
|
|
</channel>
|
|
|
|
</rss>`
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
expect := map[string]any{
|
2021-12-02 11:30:36 -05:00
|
|
|
"-atom": "http://www.w3.org/2005/Atom", "-version": "2.0",
|
2022-03-17 17:03:27 -04:00
|
|
|
"channel": map[string]any{
|
2021-12-02 11:30:36 -05:00
|
|
|
"copyright": "Example",
|
|
|
|
"description": "Example feed",
|
|
|
|
"generator": "Hugo -- gohugo.io",
|
2022-03-17 17:03:27 -04:00
|
|
|
"item": map[string]any{
|
2021-12-02 11:30:36 -05:00
|
|
|
"description": "Example description",
|
|
|
|
"guid": "https://example.com/2021/11/30/example-title/",
|
|
|
|
"link": "https://example.com/2021/11/30/example-title/",
|
|
|
|
"pubDate": "Tue, 30 Nov 2021 15:00:00 +0000",
|
|
|
|
"title": "Example title"},
|
|
|
|
"language": "en-us",
|
|
|
|
"lastBuildDate": "Fri, 08 Jan 2021 14:44:10 +0000",
|
2022-03-17 17:03:27 -04:00
|
|
|
"link": []any{"https://example.com/", map[string]any{
|
2021-12-02 11:30:36 -05:00
|
|
|
"-href": "https://example.com/feed.xml",
|
|
|
|
"-rel": "self",
|
|
|
|
"-type": "application/rss+xml"}},
|
|
|
|
"title": "Example feed",
|
|
|
|
}}
|
|
|
|
|
|
|
|
d := Default
|
|
|
|
|
|
|
|
m, err := d.Unmarshal([]byte(xmlDoc), XML)
|
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
c.Assert(m, qt.DeepEquals, expect)
|
|
|
|
|
|
|
|
}
|
2018-10-20 05:16:18 -04:00
|
|
|
func TestUnmarshalToMap(t *testing.T) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2018-10-20 05:16:18 -04:00
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
expect := map[string]any{"a": "b"}
|
2018-10-20 05:16:18 -04:00
|
|
|
|
2018-12-23 04:40:32 -05:00
|
|
|
d := Default
|
|
|
|
|
2018-10-20 05:16:18 -04:00
|
|
|
for i, test := range []struct {
|
|
|
|
data string
|
|
|
|
format Format
|
2022-03-17 17:03:27 -04:00
|
|
|
expect any
|
2018-10-20 05:16:18 -04:00
|
|
|
}{
|
|
|
|
{`a = "b"`, TOML, expect},
|
|
|
|
{`a: "b"`, YAML, expect},
|
|
|
|
// Make sure we get all string keys, even for YAML
|
2022-03-17 17:03:27 -04:00
|
|
|
{"a: Easy!\nb:\n c: 2\n d: [3, 4]", YAML, map[string]any{"a": "Easy!", "b": map[string]any{"c": 2, "d": []any{3, 4}}}},
|
|
|
|
{"a:\n true: 1\n false: 2", YAML, map[string]any{"a": map[string]any{"true": 1, "false": 2}}},
|
2018-10-20 05:16:18 -04:00
|
|
|
{`{ "a": "b" }`, JSON, expect},
|
2021-12-02 11:30:36 -05:00
|
|
|
{`<root><a>b</a></root>`, XML, expect},
|
2018-10-20 05:16:18 -04:00
|
|
|
{`#+a: b`, ORG, expect},
|
|
|
|
// errors
|
|
|
|
{`a = b`, TOML, false},
|
2018-12-23 04:40:32 -05:00
|
|
|
{`a,b,c`, CSV, false}, // Use Unmarshal for CSV
|
2018-10-20 05:16:18 -04:00
|
|
|
} {
|
2019-08-10 15:05:17 -04:00
|
|
|
msg := qt.Commentf("%d: %s", i, test.format)
|
2018-12-23 04:40:32 -05:00
|
|
|
m, err := d.UnmarshalToMap([]byte(test.data), test.format)
|
2018-10-20 05:16:18 -04:00
|
|
|
if b, ok := test.expect.(bool); ok && !b {
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.Not(qt.IsNil), msg)
|
2018-10-20 05:16:18 -04:00
|
|
|
} else {
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil, msg)
|
|
|
|
c.Assert(m, qt.DeepEquals, test.expect, msg)
|
2018-10-20 05:16:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalToInterface(t *testing.T) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2018-10-20 05:16:18 -04:00
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
expect := map[string]any{"a": "b"}
|
2018-10-20 05:16:18 -04:00
|
|
|
|
2018-12-23 04:40:32 -05:00
|
|
|
d := Default
|
|
|
|
|
2018-10-20 05:16:18 -04:00
|
|
|
for i, test := range []struct {
|
|
|
|
data string
|
|
|
|
format Format
|
2022-03-17 17:03:27 -04:00
|
|
|
expect any
|
2018-10-20 05:16:18 -04:00
|
|
|
}{
|
2022-03-17 17:03:27 -04:00
|
|
|
{`[ "Brecker", "Blake", "Redman" ]`, JSON, []any{"Brecker", "Blake", "Redman"}},
|
2018-10-20 05:16:18 -04:00
|
|
|
{`{ "a": "b" }`, JSON, expect},
|
2022-12-12 12:12:46 -05:00
|
|
|
{``, JSON, map[string]any{}},
|
2018-10-20 05:16:18 -04:00
|
|
|
{`#+a: b`, ORG, expect},
|
2022-03-17 17:03:27 -04:00
|
|
|
{`#+DATE: <2020-06-26 Fri>`, ORG, map[string]any{"date": "2020-06-26"}},
|
2018-10-20 05:16:18 -04:00
|
|
|
{`a = "b"`, TOML, expect},
|
|
|
|
{`a: "b"`, YAML, expect},
|
2021-12-02 11:30:36 -05:00
|
|
|
{`<root><a>b</a></root>`, XML, expect},
|
2019-03-23 15:15:06 -04:00
|
|
|
{`a,b,c`, CSV, [][]string{{"a", "b", "c"}}},
|
2022-03-17 17:03:27 -04:00
|
|
|
{"a: Easy!\nb:\n c: 2\n d: [3, 4]", YAML, map[string]any{"a": "Easy!", "b": map[string]any{"c": 2, "d": []any{3, 4}}}},
|
2018-10-20 05:16:18 -04:00
|
|
|
// errors
|
|
|
|
{`a = "`, TOML, false},
|
|
|
|
} {
|
2019-08-10 15:05:17 -04:00
|
|
|
msg := qt.Commentf("%d: %s", i, test.format)
|
2018-12-23 04:40:32 -05:00
|
|
|
m, err := d.Unmarshal([]byte(test.data), test.format)
|
2018-10-20 05:16:18 -04:00
|
|
|
if b, ok := test.expect.(bool); ok && !b {
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.Not(qt.IsNil), msg)
|
2018-10-20 05:16:18 -04:00
|
|
|
} else {
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil, msg)
|
|
|
|
c.Assert(m, qt.DeepEquals, test.expect, msg)
|
2018-10-20 05:16:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
func TestUnmarshalStringTo(t *testing.T) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
|
|
|
|
d := Default
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
expectMap := map[string]any{"a": "b"}
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
|
|
|
|
for i, test := range []struct {
|
|
|
|
data string
|
2022-03-17 17:03:27 -04:00
|
|
|
to any
|
|
|
|
expect any
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
}{
|
|
|
|
{"a string", "string", "a string"},
|
2022-03-17 17:03:27 -04:00
|
|
|
{`{ "a": "b" }`, make(map[string]any), expectMap},
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
{"32", int64(1234), int64(32)},
|
|
|
|
{"32", int(1234), int(32)},
|
|
|
|
{"3.14159", float64(1), float64(3.14159)},
|
2022-03-17 17:03:27 -04:00
|
|
|
{"[3,7,9]", []any{}, []any{3, 7, 9}},
|
|
|
|
{"[3.1,7.2,9.3]", []any{}, []any{3.1, 7.2, 9.3}},
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
} {
|
2019-08-10 15:05:17 -04:00
|
|
|
msg := qt.Commentf("%d: %T", i, test.to)
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
m, err := d.UnmarshalStringTo(test.data, test.to)
|
|
|
|
if b, ok := test.expect.(bool); ok && !b {
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.Not(qt.IsNil), msg)
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
} else {
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil, msg)
|
|
|
|
c.Assert(m, qt.DeepEquals, test.expect, msg)
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-20 05:16:18 -04:00
|
|
|
func TestStringifyYAMLMapKeys(t *testing.T) {
|
|
|
|
cases := []struct {
|
2022-03-17 17:03:27 -04:00
|
|
|
input any
|
|
|
|
want any
|
2018-10-20 05:16:18 -04:00
|
|
|
replaced bool
|
|
|
|
}{
|
|
|
|
{
|
2022-03-17 17:03:27 -04:00
|
|
|
map[any]any{"a": 1, "b": 2},
|
|
|
|
map[string]any{"a": 1, "b": 2},
|
2018-10-20 05:16:18 -04:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
2022-03-17 17:03:27 -04:00
|
|
|
map[any]any{"a": []any{1, map[any]any{"b": 2}}},
|
|
|
|
map[string]any{"a": []any{1, map[string]any{"b": 2}}},
|
2018-10-20 05:16:18 -04:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
2022-03-17 17:03:27 -04:00
|
|
|
map[any]any{true: 1, "b": false},
|
|
|
|
map[string]any{"true": 1, "b": false},
|
2018-10-20 05:16:18 -04:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
2022-03-17 17:03:27 -04:00
|
|
|
map[any]any{1: "a", 2: "b"},
|
|
|
|
map[string]any{"1": "a", "2": "b"},
|
2018-10-20 05:16:18 -04:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
2022-03-17 17:03:27 -04:00
|
|
|
map[any]any{"a": map[any]any{"b": 1}},
|
|
|
|
map[string]any{"a": map[string]any{"b": 1}},
|
2018-10-20 05:16:18 -04:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
2022-03-17 17:03:27 -04:00
|
|
|
map[string]any{"a": map[string]any{"b": 1}},
|
|
|
|
map[string]any{"a": map[string]any{"b": 1}},
|
2018-10-20 05:16:18 -04:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
2022-03-17 17:03:27 -04:00
|
|
|
[]any{map[any]any{1: "a", 2: "b"}},
|
|
|
|
[]any{map[string]any{"1": "a", "2": "b"}},
|
2018-10-20 05:16:18 -04:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, c := range cases {
|
|
|
|
res, replaced := stringifyMapKeys(c.input)
|
|
|
|
|
|
|
|
if c.replaced != replaced {
|
|
|
|
t.Fatalf("[%d] Replaced mismatch: %t", i, replaced)
|
|
|
|
}
|
|
|
|
if !c.replaced {
|
|
|
|
res = c.input
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(res, c.want) {
|
|
|
|
t.Errorf("[%d] given %q\nwant: %q\n got: %q", i, c.input, c.want, res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkStringifyMapKeysStringsOnlyInterfaceMaps(b *testing.B) {
|
2022-03-17 17:03:27 -04:00
|
|
|
maps := make([]map[any]any, b.N)
|
2018-10-20 05:16:18 -04:00
|
|
|
for i := 0; i < b.N; i++ {
|
2022-03-17 17:03:27 -04:00
|
|
|
maps[i] = map[any]any{
|
|
|
|
"a": map[any]any{
|
2018-10-20 05:16:18 -04:00
|
|
|
"b": 32,
|
|
|
|
"c": 43,
|
2022-03-17 17:03:27 -04:00
|
|
|
"d": map[any]any{
|
2018-10-20 05:16:18 -04:00
|
|
|
"b": 32,
|
|
|
|
"c": 43,
|
|
|
|
},
|
|
|
|
},
|
2022-03-17 17:03:27 -04:00
|
|
|
"b": []any{"a", "b"},
|
2018-10-20 05:16:18 -04:00
|
|
|
"c": "d",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
stringifyMapKeys(maps[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkStringifyMapKeysStringsOnlyStringMaps(b *testing.B) {
|
2022-03-17 17:03:27 -04:00
|
|
|
m := map[string]any{
|
|
|
|
"a": map[string]any{
|
2018-10-20 05:16:18 -04:00
|
|
|
"b": 32,
|
|
|
|
"c": 43,
|
2022-03-17 17:03:27 -04:00
|
|
|
"d": map[string]any{
|
2018-10-20 05:16:18 -04:00
|
|
|
"b": 32,
|
|
|
|
"c": 43,
|
|
|
|
},
|
|
|
|
},
|
2022-03-17 17:03:27 -04:00
|
|
|
"b": []any{"a", "b"},
|
2018-10-20 05:16:18 -04:00
|
|
|
"c": "d",
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
stringifyMapKeys(m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkStringifyMapKeysIntegers(b *testing.B) {
|
2022-03-17 17:03:27 -04:00
|
|
|
maps := make([]map[any]any, b.N)
|
2018-10-20 05:16:18 -04:00
|
|
|
for i := 0; i < b.N; i++ {
|
2022-03-17 17:03:27 -04:00
|
|
|
maps[i] = map[any]any{
|
|
|
|
1: map[any]any{
|
2018-10-20 05:16:18 -04:00
|
|
|
4: 32,
|
|
|
|
5: 43,
|
2022-03-17 17:03:27 -04:00
|
|
|
6: map[any]any{
|
2018-10-20 05:16:18 -04:00
|
|
|
7: 32,
|
|
|
|
8: 43,
|
|
|
|
},
|
|
|
|
},
|
2022-03-17 17:03:27 -04:00
|
|
|
2: []any{"a", "b"},
|
2018-10-20 05:16:18 -04:00
|
|
|
3: "d",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
stringifyMapKeys(maps[i])
|
|
|
|
}
|
|
|
|
}
|