Skip to content

Commit

Permalink
Use reflection to ignore encoding/json tags.
Browse files Browse the repository at this point in the history
  • Loading branch information
gsergey418alt committed Jan 29, 2025
1 parent ef3ff89 commit 7b88c81
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"os"
"path/filepath"
"reflect"
"strings"

"github.com/ipfs/kubo/misc/fsutil"
Expand Down Expand Up @@ -137,6 +138,27 @@ func ToMap(conf *Config) (map[string]interface{}, error) {
return m, nil
}

// Convert config to a map, without using encoding/json.
func ReflectToMap(conf interface{}) map[string]interface{} {
confmap := make(map[string]interface{})
rv := reflect.ValueOf(conf)
if rv.Kind() == reflect.Ptr {
rv = rv.Elem()
}

for i := 0; i < rv.NumField(); i++ {
field := rv.Field(i)
if field.CanInterface() {
if field.Kind() == reflect.Struct {
confmap[rv.Type().Field(i).Name] = ReflectToMap(field.Interface())
} else {
confmap[rv.Type().Field(i).Name] = field.Interface()
}
}
}
return confmap
}

// Clone copies the config. Use when updating.
func (c *Config) Clone() (*Config, error) {
var newConfig Config
Expand All @@ -155,10 +177,12 @@ func (c *Config) Clone() (*Config, error) {

// Check if the provided key is present in the structure.
func CheckKey(key string) error {
confmap, err := ToMap(&Config{})
if err != nil {
return err
}
conf := Config{}

// Convert an empty config to a map without JSON.
confmap := ReflectToMap(&conf)

// Parse the key and verify it's presence in the map.
var ok bool
var mcursor map[string]interface{}
var cursor interface{} = confmap
Expand Down

0 comments on commit 7b88c81

Please sign in to comment.