Files
zeekayandClaude Opus 5 7b23b7e68d Go 1.27.1, and the code it can say more simply
Modules declare go 1.27.1 and the build images follow. go fix applied the
modernizers; omitzero is not applied, since it changes what the JSON says.

Co-Authored-By: Claude Opus 5 <[email protected]>
2026-09-16 19:25:40 -07:00

95 lines
1.9 KiB
Go

package file
import (
"fmt"
"os"
"path/filepath"
"reflect"
"strings"
"github.com/BurntSushi/toml"
"github.com/hanzoai/ingress-parser/parser"
"gopkg.in/yaml.v3"
)
// decodeFileToNode decodes the configuration in filePath in a tree of untyped nodes.
// If filters is not empty, it skips any configuration element whose name is not among filters.
func decodeFileToNode(filePath string, filters ...string) (*parser.Node, error) {
content, err := os.ReadFile(filepath.Clean(filePath))
if err != nil {
return nil, err
}
data := make(map[string]any)
switch strings.ToLower(filepath.Ext(filePath)) {
case ".toml":
err = toml.Unmarshal(content, &data)
if err != nil {
return nil, err
}
case ".yml", ".yaml", ".json":
err = yaml.Unmarshal(content, data)
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("unsupported file extension: %s", filePath)
}
if len(data) == 0 {
return nil, fmt.Errorf("no configuration found in file: %s", filePath)
}
node, err := decodeRawToNode(data, filters...)
if err != nil {
return nil, err
}
if len(node.Children) == 0 {
return nil, fmt.Errorf("no valid configuration found in file: %s", filePath)
}
return node, nil
}
func getRootFieldNames(element any) []string {
if element == nil {
return nil
}
rootType := reflect.TypeOf(element)
return getFieldNames(rootType)
}
func getFieldNames(rootType reflect.Type) []string {
if rootType.Kind() == reflect.Pointer {
rootType = rootType.Elem()
}
if rootType.Kind() != reflect.Struct {
return nil
}
var names []string
for field := range rootType.Fields() {
if !parser.IsExported(field) {
continue
}
if field.Anonymous &&
(field.Type.Kind() == reflect.Pointer && field.Type.Elem().Kind() == reflect.Struct || field.Type.Kind() == reflect.Struct) {
names = append(names, getFieldNames(field.Type)...)
continue
}
names = append(names, field.Name)
}
return names
}