mirror of
https://github.com/kubernetes/node-problem-detector.git
synced 2026-08-28 01:47:20 +00:00
Fix Grype CVEs: update logrus and prometheus/prometheus
- Update github.com/sirupsen/logrus v1.9.0 -> v1.9.3 in test/go.mod to fix GHSA-4f99-4q7p-p3gh (High) - Update github.com/prometheus/prometheus v0.35.0 -> v0.311.3 to fix GHSA-vffh-x6r8-xx99 (Medium) - Run go mod tidy and go mod vendor to update vendor directory
This commit is contained in:
+202
@@ -0,0 +1,202 @@
|
||||
|
||||
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.
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Package yamlutils provides utilities to work with YAML documents.
|
||||
//
|
||||
// - [BytesToYAMLDoc] to construct a [yaml.Node] document
|
||||
// - [YAMLToJSON] to convert a [yaml.Node] document to JSON bytes
|
||||
// - [YAMLMapSlice] to serialize and deserialize YAML with the order of keys maintained
|
||||
package yamlutils
|
||||
|
||||
import (
|
||||
_ "go.yaml.in/yaml/v3" // for documentation purpose only
|
||||
)
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package yamlutils
|
||||
|
||||
type yamlError string
|
||||
|
||||
const (
|
||||
// ErrYAML is an error raised by YAML utilities
|
||||
ErrYAML yamlError = "yaml error"
|
||||
)
|
||||
|
||||
func (e yamlError) Error() string {
|
||||
return string(e)
|
||||
}
|
||||
+316
@@ -0,0 +1,316 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package yamlutils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"iter"
|
||||
"slices"
|
||||
"sort"
|
||||
"strconv"
|
||||
|
||||
"github.com/go-openapi/swag/conv"
|
||||
"github.com/go-openapi/swag/jsonutils"
|
||||
"github.com/go-openapi/swag/jsonutils/adapters/ifaces"
|
||||
"github.com/go-openapi/swag/typeutils"
|
||||
yaml "go.yaml.in/yaml/v3"
|
||||
)
|
||||
|
||||
var (
|
||||
_ yaml.Marshaler = YAMLMapSlice{}
|
||||
_ yaml.Unmarshaler = &YAMLMapSlice{}
|
||||
)
|
||||
|
||||
// YAMLMapSlice represents a YAML object, with the order of keys maintained.
|
||||
//
|
||||
// It is similar to [jsonutils.JSONMapSlice] and also knows how to marshal and unmarshal YAML.
|
||||
//
|
||||
// It behaves like an ordered map, but keys can't be accessed in constant time.
|
||||
type YAMLMapSlice []YAMLMapItem
|
||||
|
||||
// YAMLMapItem represents the value of a key in a YAML object held by [YAMLMapSlice].
|
||||
//
|
||||
// It is entirely equivalent to [jsonutils.JSONMapItem], with the same limitation that
|
||||
// you should not Marshal or Unmarshal directly this type, outside of a [YAMLMapSlice].
|
||||
type YAMLMapItem = jsonutils.JSONMapItem
|
||||
|
||||
func (s YAMLMapSlice) OrderedItems() iter.Seq2[string, any] {
|
||||
return func(yield func(string, any) bool) {
|
||||
for _, item := range s {
|
||||
if !yield(item.Key, item.Value) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// SetOrderedItems implements [ifaces.SetOrdered]: it merges keys passed by the iterator argument
|
||||
// into the [YAMLMapSlice].
|
||||
func (s *YAMLMapSlice) SetOrderedItems(items iter.Seq2[string, any]) {
|
||||
if items == nil {
|
||||
// force receiver to be a nil slice
|
||||
*s = nil
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
m := *s
|
||||
if len(m) > 0 {
|
||||
// update mode: short-circuited when unmarshaling fresh data structures
|
||||
idx := make(map[string]int, len(m))
|
||||
|
||||
for i, item := range m {
|
||||
idx[item.Key] = i
|
||||
}
|
||||
|
||||
for k, v := range items {
|
||||
idx, ok := idx[k]
|
||||
if ok {
|
||||
m[idx].Value = v
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
m = append(m, YAMLMapItem{Key: k, Value: v})
|
||||
}
|
||||
|
||||
*s = m
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
for k, v := range items {
|
||||
m = append(m, YAMLMapItem{Key: k, Value: v})
|
||||
}
|
||||
|
||||
*s = m
|
||||
}
|
||||
|
||||
// MarshalJSON renders this YAML object as JSON bytes.
|
||||
//
|
||||
// The difference with standard JSON marshaling is that the order of keys is maintained.
|
||||
func (s YAMLMapSlice) MarshalJSON() ([]byte, error) {
|
||||
return jsonutils.JSONMapSlice(s).MarshalJSON()
|
||||
}
|
||||
|
||||
// UnmarshalJSON builds this YAML object from JSON bytes.
|
||||
//
|
||||
// The difference with standard JSON marshaling is that the order of keys is maintained.
|
||||
func (s *YAMLMapSlice) UnmarshalJSON(data []byte) error {
|
||||
js := jsonutils.JSONMapSlice(*s)
|
||||
|
||||
if err := js.UnmarshalJSON(data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*s = YAMLMapSlice(js)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalYAML produces a YAML document as bytes
|
||||
//
|
||||
// The difference with standard YAML marshaling is that the order of keys is maintained.
|
||||
//
|
||||
// It implements [yaml.Marshaler].
|
||||
func (s YAMLMapSlice) MarshalYAML() (any, error) {
|
||||
if typeutils.IsNil(s) {
|
||||
return []byte("null\n"), nil
|
||||
}
|
||||
var n yaml.Node
|
||||
n.Kind = yaml.DocumentNode
|
||||
var nodes []*yaml.Node
|
||||
|
||||
for _, item := range s {
|
||||
nn, err := json2yaml(item.Value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ns := []*yaml.Node{
|
||||
{
|
||||
Kind: yaml.ScalarNode,
|
||||
Tag: yamlStringScalar,
|
||||
Value: item.Key,
|
||||
},
|
||||
nn,
|
||||
}
|
||||
nodes = append(nodes, ns...)
|
||||
}
|
||||
|
||||
n.Content = []*yaml.Node{
|
||||
{
|
||||
Kind: yaml.MappingNode,
|
||||
Content: nodes,
|
||||
},
|
||||
}
|
||||
|
||||
return yaml.Marshal(&n)
|
||||
}
|
||||
|
||||
// UnmarshalYAML builds a YAMLMapSlice object from a YAML document [yaml.Node].
|
||||
//
|
||||
// It implements [yaml.Unmarshaler].
|
||||
func (s *YAMLMapSlice) UnmarshalYAML(node *yaml.Node) error {
|
||||
if typeutils.IsNil(*s) {
|
||||
// allow to unmarshal with a simple var declaration (nil slice)
|
||||
*s = YAMLMapSlice{}
|
||||
}
|
||||
if node == nil {
|
||||
*s = nil
|
||||
return nil
|
||||
}
|
||||
|
||||
const sensibleAllocDivider = 2
|
||||
m := slices.Grow(*s, len(node.Content)/sensibleAllocDivider)
|
||||
m = m[:0]
|
||||
|
||||
for i := 0; i < len(node.Content); i += 2 {
|
||||
var nmi YAMLMapItem
|
||||
k, err := yamlStringScalarC(node.Content[i])
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to decode YAML map key: %w: %w", err, ErrYAML)
|
||||
}
|
||||
nmi.Key = k
|
||||
v, err := yamlNode(node.Content[i+1])
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to process YAML map value for key %q: %w: %w", k, err, ErrYAML)
|
||||
}
|
||||
nmi.Value = v
|
||||
m = append(m, nmi)
|
||||
}
|
||||
|
||||
*s = m
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func json2yaml(item any) (*yaml.Node, error) {
|
||||
if typeutils.IsNil(item) {
|
||||
return &yaml.Node{
|
||||
Kind: yaml.ScalarNode,
|
||||
Value: "null",
|
||||
}, nil
|
||||
}
|
||||
|
||||
switch val := item.(type) {
|
||||
case ifaces.Ordered:
|
||||
return orderedYAML(val)
|
||||
|
||||
case map[string]any:
|
||||
var n yaml.Node
|
||||
n.Kind = yaml.MappingNode
|
||||
keys := make([]string, 0, len(val))
|
||||
for k := range val {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
|
||||
for _, k := range keys {
|
||||
v := val[k]
|
||||
childNode, err := json2yaml(v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
n.Content = append(n.Content, &yaml.Node{
|
||||
Kind: yaml.ScalarNode,
|
||||
Tag: yamlStringScalar,
|
||||
Value: k,
|
||||
}, childNode)
|
||||
}
|
||||
return &n, nil
|
||||
|
||||
case []any:
|
||||
var n yaml.Node
|
||||
n.Kind = yaml.SequenceNode
|
||||
for i := range val {
|
||||
childNode, err := json2yaml(val[i])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
n.Content = append(n.Content, childNode)
|
||||
}
|
||||
return &n, nil
|
||||
case string:
|
||||
return &yaml.Node{
|
||||
Kind: yaml.ScalarNode,
|
||||
Tag: yamlStringScalar,
|
||||
Value: val,
|
||||
}, nil
|
||||
case float32:
|
||||
return floatNode(val)
|
||||
case float64:
|
||||
return floatNode(val)
|
||||
case int:
|
||||
return integerNode(val)
|
||||
case int8:
|
||||
return integerNode(val)
|
||||
case int16:
|
||||
return integerNode(val)
|
||||
case int32:
|
||||
return integerNode(val)
|
||||
case int64:
|
||||
return integerNode(val)
|
||||
case uint:
|
||||
return uintegerNode(val)
|
||||
case uint8:
|
||||
return uintegerNode(val)
|
||||
case uint16:
|
||||
return uintegerNode(val)
|
||||
case uint32:
|
||||
return uintegerNode(val)
|
||||
case uint64:
|
||||
return uintegerNode(val)
|
||||
case bool:
|
||||
return &yaml.Node{
|
||||
Kind: yaml.ScalarNode,
|
||||
Tag: yamlBoolScalar,
|
||||
Value: strconv.FormatBool(val),
|
||||
}, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unhandled type: %T: %w", val, ErrYAML)
|
||||
}
|
||||
}
|
||||
|
||||
func floatNode[T conv.Float](val T) (*yaml.Node, error) {
|
||||
return &yaml.Node{
|
||||
Kind: yaml.ScalarNode,
|
||||
Tag: yamlFloatScalar,
|
||||
Value: conv.FormatFloat(val),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func integerNode[T conv.Signed](val T) (*yaml.Node, error) {
|
||||
return &yaml.Node{
|
||||
Kind: yaml.ScalarNode,
|
||||
Tag: yamlIntScalar,
|
||||
Value: conv.FormatInteger(val),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func uintegerNode[T conv.Unsigned](val T) (*yaml.Node, error) {
|
||||
return &yaml.Node{
|
||||
Kind: yaml.ScalarNode,
|
||||
Tag: yamlIntScalar,
|
||||
Value: conv.FormatUinteger(val),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func orderedYAML[T ifaces.Ordered](val T) (*yaml.Node, error) {
|
||||
var n yaml.Node
|
||||
n.Kind = yaml.MappingNode
|
||||
for key, value := range val.OrderedItems() {
|
||||
childNode, err := json2yaml(value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
n.Content = append(n.Content, &yaml.Node{
|
||||
Kind: yaml.ScalarNode,
|
||||
Tag: yamlStringScalar,
|
||||
Value: key,
|
||||
}, childNode)
|
||||
}
|
||||
return &n, nil
|
||||
}
|
||||
+211
@@ -0,0 +1,211 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package yamlutils
|
||||
|
||||
import (
|
||||
json "encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/go-openapi/swag/jsonutils"
|
||||
yaml "go.yaml.in/yaml/v3"
|
||||
)
|
||||
|
||||
// YAMLToJSON converts a YAML document into JSON bytes.
|
||||
//
|
||||
// Note: a YAML document is the output from a [yaml.Marshaler], e.g a pointer to a [yaml.Node].
|
||||
//
|
||||
// [YAMLToJSON] is typically called after [BytesToYAMLDoc].
|
||||
func YAMLToJSON(value any) (json.RawMessage, error) {
|
||||
jm, err := transformData(value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
b, err := jsonutils.WriteJSON(jm)
|
||||
|
||||
return json.RawMessage(b), err
|
||||
}
|
||||
|
||||
// BytesToYAMLDoc converts a byte slice into a YAML document.
|
||||
//
|
||||
// This function only supports root documents that are objects.
|
||||
//
|
||||
// A YAML document is a pointer to a [yaml.Node].
|
||||
func BytesToYAMLDoc(data []byte) (any, error) {
|
||||
var document yaml.Node // preserve order that is present in the document
|
||||
if err := yaml.Unmarshal(data, &document); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if document.Kind != yaml.DocumentNode || len(document.Content) != 1 || document.Content[0].Kind != yaml.MappingNode {
|
||||
return nil, fmt.Errorf("only YAML documents that are objects are supported: %w", ErrYAML)
|
||||
}
|
||||
return &document, nil
|
||||
}
|
||||
|
||||
func yamlNode(root *yaml.Node) (any, error) {
|
||||
switch root.Kind {
|
||||
case yaml.DocumentNode:
|
||||
return yamlDocument(root)
|
||||
case yaml.SequenceNode:
|
||||
return yamlSequence(root)
|
||||
case yaml.MappingNode:
|
||||
return yamlMapping(root)
|
||||
case yaml.ScalarNode:
|
||||
return yamlScalar(root)
|
||||
case yaml.AliasNode:
|
||||
return yamlNode(root.Alias)
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported YAML node type: %v: %w", root.Kind, ErrYAML)
|
||||
}
|
||||
}
|
||||
|
||||
func yamlDocument(node *yaml.Node) (any, error) {
|
||||
if len(node.Content) != 1 {
|
||||
return nil, fmt.Errorf("unexpected YAML Document node content length: %d: %w", len(node.Content), ErrYAML)
|
||||
}
|
||||
return yamlNode(node.Content[0])
|
||||
}
|
||||
|
||||
func yamlMapping(node *yaml.Node) (any, error) {
|
||||
const sensibleAllocDivider = 2 // nodes concatenate (key,value) sequences
|
||||
m := make(YAMLMapSlice, len(node.Content)/sensibleAllocDivider)
|
||||
|
||||
if err := m.UnmarshalYAML(node); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func yamlSequence(node *yaml.Node) (any, error) {
|
||||
s := make([]any, 0)
|
||||
|
||||
for i := range len(node.Content) {
|
||||
v, err := yamlNode(node.Content[i])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to decode YAML sequence value: %w: %w", err, ErrYAML)
|
||||
}
|
||||
s = append(s, v)
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
const ( // See https://yaml.org/type/
|
||||
yamlStringScalar = "tag:yaml.org,2002:str"
|
||||
yamlIntScalar = "tag:yaml.org,2002:int"
|
||||
yamlBoolScalar = "tag:yaml.org,2002:bool"
|
||||
yamlFloatScalar = "tag:yaml.org,2002:float"
|
||||
yamlTimestamp = "tag:yaml.org,2002:timestamp"
|
||||
yamlNull = "tag:yaml.org,2002:null"
|
||||
)
|
||||
|
||||
func yamlScalar(node *yaml.Node) (any, error) {
|
||||
switch node.LongTag() {
|
||||
case yamlStringScalar:
|
||||
return node.Value, nil
|
||||
case yamlBoolScalar:
|
||||
b, err := strconv.ParseBool(node.Value)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to process scalar node. Got %q. Expecting bool content: %w: %w", node.Value, err, ErrYAML)
|
||||
}
|
||||
return b, nil
|
||||
case yamlIntScalar:
|
||||
i, err := strconv.ParseInt(node.Value, 10, 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to process scalar node. Got %q. Expecting integer content: %w: %w", node.Value, err, ErrYAML)
|
||||
}
|
||||
return i, nil
|
||||
case yamlFloatScalar:
|
||||
f, err := strconv.ParseFloat(node.Value, 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to process scalar node. Got %q. Expecting float content: %w: %w", node.Value, err, ErrYAML)
|
||||
}
|
||||
return f, nil
|
||||
case yamlTimestamp:
|
||||
// YAML timestamp is marshaled as string, not time
|
||||
return node.Value, nil
|
||||
case yamlNull:
|
||||
return nil, nil //nolint:nilnil
|
||||
default:
|
||||
return nil, fmt.Errorf("YAML tag %q is not supported: %w", node.LongTag(), ErrYAML)
|
||||
}
|
||||
}
|
||||
|
||||
func yamlStringScalarC(node *yaml.Node) (string, error) {
|
||||
if node.Kind != yaml.ScalarNode {
|
||||
return "", fmt.Errorf("expecting a string scalar but got %q: %w", node.Kind, ErrYAML)
|
||||
}
|
||||
switch node.LongTag() {
|
||||
case yamlStringScalar, yamlIntScalar, yamlFloatScalar:
|
||||
return node.Value, nil
|
||||
default:
|
||||
return "", fmt.Errorf("YAML tag %q is not supported as map key: %w", node.LongTag(), ErrYAML)
|
||||
}
|
||||
}
|
||||
|
||||
func format(t any) (string, error) {
|
||||
switch k := t.(type) {
|
||||
case string:
|
||||
return k, nil
|
||||
case uint:
|
||||
return strconv.FormatUint(uint64(k), 10), nil
|
||||
case uint8:
|
||||
return strconv.FormatUint(uint64(k), 10), nil
|
||||
case uint16:
|
||||
return strconv.FormatUint(uint64(k), 10), nil
|
||||
case uint32:
|
||||
return strconv.FormatUint(uint64(k), 10), nil
|
||||
case uint64:
|
||||
return strconv.FormatUint(k, 10), nil
|
||||
case int:
|
||||
return strconv.Itoa(k), nil
|
||||
case int8:
|
||||
return strconv.FormatInt(int64(k), 10), nil
|
||||
case int16:
|
||||
return strconv.FormatInt(int64(k), 10), nil
|
||||
case int32:
|
||||
return strconv.FormatInt(int64(k), 10), nil
|
||||
case int64:
|
||||
return strconv.FormatInt(k, 10), nil
|
||||
default:
|
||||
return "", fmt.Errorf("unexpected map key type, got: %T: %w", k, ErrYAML)
|
||||
}
|
||||
}
|
||||
|
||||
func transformData(input any) (out any, err error) {
|
||||
switch in := input.(type) {
|
||||
case yaml.Node:
|
||||
return yamlNode(&in)
|
||||
case *yaml.Node:
|
||||
return yamlNode(in)
|
||||
case map[any]any:
|
||||
o := make(YAMLMapSlice, 0, len(in))
|
||||
for ke, va := range in {
|
||||
var nmi YAMLMapItem
|
||||
if nmi.Key, err = format(ke); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
v, ert := transformData(va)
|
||||
if ert != nil {
|
||||
return nil, ert
|
||||
}
|
||||
nmi.Value = v
|
||||
o = append(o, nmi)
|
||||
}
|
||||
return o, nil
|
||||
case []any:
|
||||
len1 := len(in)
|
||||
o := make([]any, len1)
|
||||
for i := range len1 {
|
||||
o[i], err = transformData(in[i])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return o, nil
|
||||
}
|
||||
return input, nil
|
||||
}
|
||||
Reference in New Issue
Block a user