Skip to content
Snippets Groups Projects
Verified Commit f0987f43 authored by Volker Schukai's avatar Volker Schukai :alien:
Browse files

fix: slices are not covert by the path function #7

parent bd2c76a5
No related branches found
No related tags found
No related merge requests found
...@@ -94,40 +94,123 @@ func (s *Settings[C]) importStreams() { ...@@ -94,40 +94,123 @@ func (s *Settings[C]) importStreams() {
} }
} }
func replacePath(p string, c any) { //// replacePath replaces all pathInterface fields in the struct with the given path
//func replacePath(p string, c any) {
if reflect.TypeOf(c).Kind() != reflect.Ptr { //
// if reflect.TypeOf(c).Kind() != reflect.Ptr {
// panic("c must be a pointer")
// }
//
// if reflect.TypeOf(c).Elem().Kind() != reflect.Struct {
// panic("c must be a pointer to a struct")
// }
//
// fields := reflect.VisibleFields(reflect.TypeOf(c).Elem())
// for _, field := range fields {
//
// r := reflect.ValueOf(c).Elem().FieldByName(field.Name)
// if field.Type.Kind() == reflect.Struct {
// if r.CanAddr() {
// replacePath(p, r.Addr().Interface())
// }
// continue
// }
//
// _, ok := r.Interface().(pathInterface)
// if ok {
//
// if r.CanSet() {
// if !path.IsAbs(r.String()) {
// r.SetString(path.Join(p, r.String()))
// }
// }
// continue
//
// }
//
// if r.Kind() == reflect.Slice {
// for i := 0; i < r.Len(); i++ {
// if r.Index(i).CanAddr() {
// replacePath(p, r.Index(i).Addr().Interface())
// }
// }
// } else if r.Kind() == reflect.Map {
// for _, k := range r.MapKeys() {
// if r.MapIndex(k).CanAddr() {
// replacePath(p, r.MapIndex(k).Addr().Interface())
// }
// }
// } else if r.Kind() == reflect.Ptr {
// if r.Elem().CanAddr() {
// replacePath(p, r.Elem().Addr().Interface())
// }
// } else if r.Kind() == reflect.Interface {
// if r.Elem().CanAddr() {
// replacePath(p, r.Elem().Addr().Interface())
// }
// }
//
// }
//
//}
func replacePath(p string, c interface{}) {
cValue := reflect.ValueOf(c)
cType := cValue.Type()
if cType.Kind() != reflect.Ptr {
panic("c must be a pointer") panic("c must be a pointer")
} }
if reflect.TypeOf(c).Elem().Kind() != reflect.Struct { if cType.Elem().Kind() != reflect.Struct {
panic("c must be a pointer to a struct") panic("c must be a pointer to a struct")
} }
fields := reflect.VisibleFields(reflect.TypeOf(c).Elem()) fields := reflect.VisibleFields(cType.Elem())
for _, field := range fields { for _, field := range fields {
r := cValue.Elem().FieldByName(field.Name)
handleField(p, r)
}
}
r := reflect.ValueOf(c).Elem().FieldByName(field.Name) func handleField(p string, r reflect.Value) {
if field.Type.Kind() == reflect.Struct { switch r.Kind() {
if r.CanAddr() { case reflect.Struct:
replacePath(p, r.Addr().Interface()) if r.CanAddr() {
} replacePath(p, r.Addr().Interface())
continue
} }
case reflect.Slice, reflect.Map, reflect.Ptr, reflect.Interface:
_, ok := r.Interface().(pathInterface) forEachElem(r, func(e reflect.Value) {
if ok { if e.CanAddr() {
replacePath(p, e.Addr().Interface())
if r.CanSet() { }
if !path.IsAbs(r.String()) { })
r.SetString(path.Join(p, r.String())) default:
} // Check for pathInterface
if v, ok := r.Interface().(pathInterface); ok {
currentPath := v.String()
if r.CanSet() && !path.IsAbs(currentPath) {
r.SetString(path.Join(p, currentPath))
} }
} }
} }
}
func forEachElem(r reflect.Value, fn func(e reflect.Value)) {
switch r.Kind() {
case reflect.Slice:
for i := 0; i < r.Len(); i++ {
fn(r.Index(i))
}
case reflect.Map:
for _, k := range r.MapKeys() {
fn(r.MapIndex(k))
}
case reflect.Ptr, reflect.Interface:
if !r.IsNil() {
fn(r.Elem())
}
}
} }
func (s *Settings[C]) importFiles() { func (s *Settings[C]) importFiles() {
...@@ -148,7 +231,7 @@ func (s *Settings[C]) importFiles() { ...@@ -148,7 +231,7 @@ func (s *Settings[C]) importFiles() {
f, err := s.files.fs.Open(fn) f, err := s.files.fs.Open(fn)
if os.IsNotExist(err) { if os.IsNotExist(err) {
f.Close() _ = f.Close()
continue continue
} }
...@@ -156,7 +239,7 @@ func (s *Settings[C]) importFiles() { ...@@ -156,7 +239,7 @@ func (s *Settings[C]) importFiles() {
s.importStream(reader{s.files.format, r}, func(c *C) { s.importStream(reader{s.files.format, r}, func(c *C) {
replacePath(d, c) replacePath(d, c)
}) })
f.Close() _ = f.Close()
} }
for _, f := range s.files.files { for _, f := range s.files.files {
...@@ -164,7 +247,7 @@ func (s *Settings[C]) importFiles() { ...@@ -164,7 +247,7 @@ func (s *Settings[C]) importFiles() {
if err != nil { if err != nil {
s.errors = append(s.errors, err) s.errors = append(s.errors, err)
r.Close() _ = r.Close()
continue continue
} }
...@@ -173,7 +256,7 @@ func (s *Settings[C]) importFiles() { ...@@ -173,7 +256,7 @@ func (s *Settings[C]) importFiles() {
replacePath(d, c) replacePath(d, c)
}) })
r.Close() _ = r.Close()
} }
} }
......
...@@ -24,8 +24,6 @@ func TestReadExample3(t *testing.T) { ...@@ -24,8 +24,6 @@ func TestReadExample3(t *testing.T) {
c.SetDefaultDirectories() c.SetDefaultDirectories()
c.Import() c.Import()
//fmt.Println(c.Config().Host)
assert.Equal(t, c.Config().Host, "localhost") assert.Equal(t, c.Config().Host, "localhost")
} }
// Copyright 2022 schukai GmbH
// SPDX-License-Identifier: AGPL-3.0
package configuration
import (
"github.com/stretchr/testify/assert"
"io/ioutil"
"os"
"path"
"path/filepath"
"testing"
)
type Issue7Routing struct {
P PathValue `json:"p" yaml:"p"`
}
type Issue7Server struct {
Routing []Issue7Routing `json:"routing" yaml:"routing"`
}
type Issue7Config struct {
Server Issue7Server `json:"server" yaml:"server"`
}
func createIssue7TempFile(content string) (string, error) {
file, err := ioutil.TempFile("", "tempfile")
if err != nil {
return "", err
}
defer func() {
_ = file.Close()
}()
_, err = file.WriteString(content)
if err != nil {
return "", err
}
return file.Name(), nil
}
func TestPathRewrite(t *testing.T) {
c := New(Issue7Config{})
n, err := createIssue7TempFile(`{
"server": {
"routing": [
{
"p": "./test"
}
]
}
}`)
if err != nil {
t.Fatal(err)
}
c.SetMnemonic("my-app")
c.AddFile(n)
c.Import()
_ = os.Remove(n)
//fmt.Println(c.Config().Host)
expected := path.Join(filepath.Dir(n), "test")
assert.Equal(t, expected, c.Config().Server.Routing[0].P.String())
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment