Skip to content

Commit

Permalink
fix(generate): get key value with ast.StringNode (#2358)
Browse files Browse the repository at this point in the history
* fix(generate): get key value with ast.StringNode

* fix(ast): get a string key with *ast.StringNode
  • Loading branch information
suzuki-shunsuke authored Oct 16, 2023
1 parent 62d36cd commit cca85e9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
8 changes: 6 additions & 2 deletions pkg/asset/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/goccy/go-yaml/ast"
)

func UpdateASTFile(file *ast.File, pkgs interface{}) error {
func UpdateASTFile(file *ast.File, pkgs interface{}) error { //nolint:cyclop
node, err := yaml.ValueToNode(pkgs)
if err != nil {
return fmt.Errorf("convert packages to node: %w", err)
Expand All @@ -20,7 +20,11 @@ func UpdateASTFile(file *ast.File, pkgs interface{}) error {
continue
}
for _, mapValue := range body.Values {
if mapValue.Key.String() != "packages" {
key, ok := mapValue.Key.(*ast.StringNode)
if !ok {
continue
}
if key.Value != "packages" {
continue
}
switch mapValue.Value.Type() {
Expand Down
6 changes: 5 additions & 1 deletion pkg/controller/generate/output/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ func (o *Outputter) generateInsert(cfgFilePath string, pkgs []*aqua.Package) err

func getPkgsAST(values []*ast.MappingValueNode) *ast.MappingValueNode {
for _, mapValue := range values {
if mapValue.Key.String() != "packages" {
key, ok := mapValue.Key.(*ast.StringNode)
if !ok {
continue
}
if key.Value != "packages" {
continue
}
return mapValue
Expand Down
6 changes: 5 additions & 1 deletion pkg/controller/update/ast/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ func parsePackageNode(logE *logrus.Entry, node ast.Node, newVersions map[string]
var pkgVersion string
var nameNode *ast.StringNode
for _, mvn := range mvs {
switch mvn.Key.String() {
key, ok := mvn.Key.(*ast.StringNode)
if !ok {
continue
}
switch key.Value {
case "registry":
sn, ok := mvn.Value.(*ast.StringNode)
if !ok {
Expand Down
12 changes: 10 additions & 2 deletions pkg/controller/update/ast/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ const typeStandard = "standard"

func findMappingValue(values []*ast.MappingValueNode, key string) *ast.MappingValueNode {
for _, value := range values {
if value.Key.String() == key {
sn, ok := value.Key.(*ast.StringNode)
if !ok {
continue
}
if sn.Value == key {
return value
}
}
Expand Down Expand Up @@ -65,7 +69,11 @@ func parseRegistryNode(logE *logrus.Entry, node ast.Node, newVersions map[string
var newVersion string
var rgstName string
for _, mvn := range mvs {
switch mvn.Key.String() {
key, ok := mvn.Key.(*ast.StringNode)
if !ok {
continue
}
switch key.Value {
case "ref":
sn, ok := mvn.Value.(*ast.StringNode)
if !ok {
Expand Down

0 comments on commit cca85e9

Please sign in to comment.