chore: bump Go dependencies

This commit is contained in:
lzap 2025-08-03 15:24:15 +00:00 committed by Simon de Vlieger
parent b3d1e4cf13
commit e118df5dfd
1119 changed files with 126580 additions and 8706 deletions

View file

@ -2,7 +2,8 @@ package overlay
import (
"fmt"
"github.com/vmware-labs/yaml-jsonpath/pkg/yamlpath"
"github.com/speakeasy-api/jsonpath/pkg/jsonpath/config"
"github.com/speakeasy-api/jsonpath/pkg/jsonpath/token"
"gopkg.in/yaml.v3"
"strings"
)
@ -13,9 +14,9 @@ func (o *Overlay) ApplyTo(root *yaml.Node) error {
for _, action := range o.Actions {
var err error
if action.Remove {
err = applyRemoveAction(root, action)
err = o.applyRemoveAction(root, action, nil)
} else {
err = applyUpdateAction(root, action, &[]string{})
err = o.applyUpdateAction(root, action, &[]string{})
}
if err != nil {
@ -29,41 +30,51 @@ func (o *Overlay) ApplyTo(root *yaml.Node) error {
func (o *Overlay) ApplyToStrict(root *yaml.Node) (error, []string) {
multiError := []string{}
warnings := []string{}
hasFilterExpression := false
for i, action := range o.Actions {
err := validateSelectorHasAtLeastOneTarget(root, action)
tokens := token.NewTokenizer(action.Target, config.WithPropertyNameExtension()).Tokenize()
for _, tok := range tokens {
if tok.Token == token.FILTER {
hasFilterExpression = true
}
}
actionWarnings := []string{}
err := o.validateSelectorHasAtLeastOneTarget(root, action)
if err != nil {
multiError = append(multiError, err.Error())
}
if action.Remove {
err = applyRemoveAction(root, action)
err = o.applyRemoveAction(root, action, &actionWarnings)
} else {
actionWarnings := []string{}
err = applyUpdateAction(root, action, &actionWarnings)
for _, warning := range actionWarnings {
warnings = append(warnings, fmt.Sprintf("update action (%v / %v) target=%s: %s", i+1, len(o.Actions), action.Target, warning))
}
err = o.applyUpdateAction(root, action, &actionWarnings)
}
for _, warning := range actionWarnings {
warnings = append(warnings, fmt.Sprintf("update action (%v / %v) target=%s: %s", i+1, len(o.Actions), action.Target, warning))
}
}
if hasFilterExpression && !o.UsesRFC9535() {
warnings = append(warnings, "overlay has a filter expression but lacks `x-speakeasy-jsonpath: rfc9535` extension. Deprecated jsonpath behaviour in use. See overlay.speakeasy.com for the implementation playground.")
}
if len(multiError) > 0 {
return fmt.Errorf("error applying overlay (strict): %v", strings.Join(multiError, ",")), warnings
}
return nil, warnings
}
func validateSelectorHasAtLeastOneTarget(root *yaml.Node, action Action) error {
func (o *Overlay) validateSelectorHasAtLeastOneTarget(root *yaml.Node, action Action) error {
if action.Target == "" {
return nil
}
p, err := yamlpath.NewPath(action.Target)
p, err := o.NewPath(action.Target, nil)
if err != nil {
return err
}
nodes, err := p.Find(root)
if err != nil {
return err
}
nodes := p.Query(root)
if len(nodes) == 0 {
return fmt.Errorf("selector %q did not match any targets", action.Target)
@ -72,19 +83,19 @@ func validateSelectorHasAtLeastOneTarget(root *yaml.Node, action Action) error {
return nil
}
func applyRemoveAction(root *yaml.Node, action Action) error {
func (o *Overlay) applyRemoveAction(root *yaml.Node, action Action, warnings *[]string) error {
if action.Target == "" {
return nil
}
idx := newParentIndex(root)
p, err := yamlpath.NewPath(action.Target)
p, err := o.NewPath(action.Target, warnings)
if err != nil {
return err
}
nodes, err := p.Find(root)
nodes := p.Query(root)
if err != nil {
return err
}
@ -106,8 +117,13 @@ func removeNode(idx parentIndex, node *yaml.Node) {
if child == node {
switch parent.Kind {
case yaml.MappingNode:
// we have to delete the key too
parent.Content = append(parent.Content[:i-1], parent.Content[i+1:]...)
if i%2 == 1 {
// if we select a value, we should delete the key too
parent.Content = append(parent.Content[:i-1], parent.Content[i+1:]...)
} else {
// if we select a key, we should delete the value
parent.Content = append(parent.Content[:i], parent.Content[i+2:]...)
}
return
case yaml.SequenceNode:
parent.Content = append(parent.Content[:i], parent.Content[i+1:]...)
@ -117,7 +133,7 @@ func removeNode(idx parentIndex, node *yaml.Node) {
}
}
func applyUpdateAction(root *yaml.Node, action Action, warnings *[]string) error {
func (o *Overlay) applyUpdateAction(root *yaml.Node, action Action, warnings *[]string) error {
if action.Target == "" {
return nil
}
@ -126,22 +142,19 @@ func applyUpdateAction(root *yaml.Node, action Action, warnings *[]string) error
return nil
}
p, err := yamlpath.NewPath(action.Target)
if err != nil {
return err
}
nodes, err := p.Find(root)
p, err := o.NewPath(action.Target, warnings)
if err != nil {
return err
}
nodes := p.Query(root)
prior, err := yaml.Marshal(root)
if err != nil {
return err
}
for _, node := range nodes {
if err := updateNode(node, action.Update); err != nil {
if err := updateNode(node, &action.Update); err != nil {
return err
}
}
@ -156,14 +169,14 @@ func applyUpdateAction(root *yaml.Node, action Action, warnings *[]string) error
return nil
}
func updateNode(node *yaml.Node, updateNode yaml.Node) error {
func updateNode(node *yaml.Node, updateNode *yaml.Node) error {
mergeNode(node, updateNode)
return nil
}
func mergeNode(node *yaml.Node, merge yaml.Node) {
func mergeNode(node *yaml.Node, merge *yaml.Node) {
if node.Kind != merge.Kind {
*node = merge
*node = *clone(merge)
return
}
switch node.Kind {
@ -178,7 +191,7 @@ func mergeNode(node *yaml.Node, merge yaml.Node) {
// mergeMappingNode will perform a shallow merge of the merge node into the main
// node.
func mergeMappingNode(node *yaml.Node, merge yaml.Node) {
func mergeMappingNode(node *yaml.Node, merge *yaml.Node) {
NextKey:
for i := 0; i < len(merge.Content); i += 2 {
mergeKey := merge.Content[i].Value
@ -187,16 +200,39 @@ NextKey:
for j := 0; j < len(node.Content); j += 2 {
nodeKey := node.Content[j].Value
if nodeKey == mergeKey {
mergeNode(node.Content[j+1], *mergeValue)
mergeNode(node.Content[j+1], mergeValue)
continue NextKey
}
}
node.Content = append(node.Content, merge.Content[i], mergeValue)
node.Content = append(node.Content, merge.Content[i], clone(mergeValue))
}
}
// mergeSequenceNode will append the merge node's content to the original node.
func mergeSequenceNode(node *yaml.Node, merge yaml.Node) {
node.Content = append(node.Content, merge.Content...)
func mergeSequenceNode(node *yaml.Node, merge *yaml.Node) {
node.Content = append(node.Content, clone(merge).Content...)
}
func clone(node *yaml.Node) *yaml.Node {
newNode := &yaml.Node{
Kind: node.Kind,
Style: node.Style,
Tag: node.Tag,
Value: node.Value,
Anchor: node.Anchor,
HeadComment: node.HeadComment,
LineComment: node.LineComment,
FootComment: node.FootComment,
}
if node.Alias != nil {
newNode.Alias = clone(node.Alias)
}
if node.Content != nil {
newNode.Content = make([]*yaml.Node, len(node.Content))
for i, child := range node.Content {
newNode.Content[i] = clone(child)
}
}
return newNode
}

View file

@ -18,7 +18,8 @@ func Compare(title string, y1 *yaml.Node, y2 yaml.Node) (*Overlay, error) {
}
return &Overlay{
Version: "1.0.0",
Version: "1.0.0",
JSONPathVersion: "rfc9535",
Info: Info{
Title: title,
Version: "0.0.0",

View file

@ -0,0 +1,47 @@
package overlay
import (
"fmt"
"github.com/speakeasy-api/jsonpath/pkg/jsonpath"
"github.com/speakeasy-api/jsonpath/pkg/jsonpath/config"
"github.com/vmware-labs/yaml-jsonpath/pkg/yamlpath"
"gopkg.in/yaml.v3"
)
type Queryable interface {
Query(root *yaml.Node) []*yaml.Node
}
type yamlPathQueryable struct {
path *yamlpath.Path
}
func (y yamlPathQueryable) Query(root *yaml.Node) []*yaml.Node {
if y.path == nil {
return []*yaml.Node{}
}
// errors aren't actually possible from yamlpath.
result, _ := y.path.Find(root)
return result
}
func (o *Overlay) NewPath(target string, warnings *[]string) (Queryable, error) {
rfcJSONPath, rfcJSONPathErr := jsonpath.NewPath(target, config.WithPropertyNameExtension())
if o.UsesRFC9535() {
return rfcJSONPath, rfcJSONPathErr
}
if rfcJSONPathErr != nil && warnings != nil {
*warnings = append(*warnings, fmt.Sprintf("invalid rfc9535 jsonpath %s: %s\nThis will be treated as an error in the future. Please fix and opt into the new implementation with `\"x-speakeasy-jsonpath\": rfc9535` in the root of your overlay. See overlay.speakeasy.com for an implementation playground.", target, rfcJSONPathErr.Error()))
}
path, err := yamlpath.NewPath(target)
return mustExecute(path), err
}
func (o *Overlay) UsesRFC9535() bool {
return o.JSONPathVersion == "rfc9535"
}
func mustExecute(path *yamlpath.Path) yamlPathQueryable {
return yamlPathQueryable{path}
}

View file

@ -13,10 +13,12 @@ type Extensions map[string]any
type Overlay struct {
Extensions `yaml:"-,inline"`
// Version is the version of the overlay configuration. As the RFC was never
// really ratifies, this value does not mean much.
// Version is the version of the overlay configuration. This is only ever expected to be 1.0.0
Version string `yaml:"overlay"`
// JSONPathVersion should be set to rfc9535, and is used for backwards compatability purposes
JSONPathVersion string `yaml:"x-speakeasy-jsonpath,omitempty"`
// Info describes the metadata for the overlay.
Info Info `yaml:"info"`