Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Configuration
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Jira
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container Registry
Monitor
Service Desk
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OSS
Libraries
Go
Application
Configuration
Commits
f0987f43
Verified
Commit
f0987f43
authored
1 year ago
by
Volker Schukai
Browse files
Options
Downloads
Patches
Plain Diff
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
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
import.go
+108
-25
108 additions, 25 deletions
import.go
import_test.go
+0
-2
0 additions, 2 deletions
import_test.go
issue-7_test.go
+73
-0
73 additions, 0 deletions
issue-7_test.go
with
181 additions
and
27 deletions
import.go
+
108
−
25
View file @
f0987f43
...
@@ -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
()
}
}
}
}
...
...
This diff is collapsed.
Click to expand it.
import_test.go
+
0
−
2
View file @
f0987f43
...
@@ -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"
)
}
}
This diff is collapsed.
Click to expand it.
issue-7_test.go
0 → 100644
+
73
−
0
View file @
f0987f43
// 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
())
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment