Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bobgroup-go-utils
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bob Public Utils
bobgroup-go-utils
Commits
8f8cf91d
Commit
8f8cf91d
authored
Nov 26, 2021
by
Jan Semmelink
Browse files
Options
Downloads
Patches
Plain Diff
Update parsing not to go into sub structs which has their own parsers, e.g. time.Time
parent
00b0546b
No related branches found
No related tags found
1 merge request
!12
One more change to detect when custom types (like time.Time) has their own parsers, then do not step into those structs.
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
struct_utils/named_values_to_struct.go
+18
-8
18 additions, 8 deletions
struct_utils/named_values_to_struct.go
with
18 additions
and
8 deletions
struct_utils/named_values_to_struct.go
+
18
−
8
View file @
8f8cf91d
...
...
@@ -52,7 +52,6 @@ func NamedValuesFromReader(prefix string, reader string_utils.KeyReader) map[str
logger
.
Debugf
(
"Key(%s) undefined"
,
key
)
continue
}
logger
.
Debugf
(
"key(%s)=
\"
%s
\"
"
,
key
,
value
)
result
[
strings
.
ToLower
(
key
)]
=
[]
string
{
value
}
//split only if valid CSV between [...]
...
...
@@ -224,7 +223,15 @@ func unmarshalNamedValuesIntoStructPtr(prefix string, namedValues map[string][]s
}
}
//recurse into anonymous sub-structs
//if type can scan/unmarshal, do not recurse into its fields
//because we do not want to iterate over fields inside structs like time.Time
//that implements their own parsers
{
fieldPtr
:=
structPtrValue
.
Elem
()
.
Field
(
i
)
.
Addr
()
.
Interface
()
_
,
ok1
:=
fieldPtr
.
(
sql
.
Scanner
)
_
,
ok2
:=
fieldPtr
.
(
json
.
Unmarshaler
)
if
!
ok1
&&
!
ok2
{
//recurse into sub-structs
if
structTypeField
.
Type
.
Kind
()
==
reflect
.
Struct
{
if
nameList
,
err
:=
unmarshalNamedValuesIntoStructPtr
(
fieldName
,
namedValues
,
structTypeField
.
Type
,
structPtrValue
.
Elem
()
.
Field
(
i
)
.
Addr
());
err
!=
nil
{
return
nil
,
errors
.
Wrapf
(
err
,
"failed on %s.%s"
,
structType
.
Name
(),
fieldName
)
...
...
@@ -233,6 +240,8 @@ func unmarshalNamedValuesIntoStructPtr(prefix string, namedValues map[string][]s
}
continue
}
}
}
fieldValues
,
ok
:=
namedValues
[
fieldName
]
if
!
ok
{
...
...
@@ -244,6 +253,7 @@ func unmarshalNamedValuesIntoStructPtr(prefix string, namedValues map[string][]s
continue
//field has no value specified in URL, do not remove values not defined (cannot clear defined struct fields with named values)
}
structPtrFieldValue
:=
structPtrValue
.
Elem
()
.
Field
(
i
)
if
structPtrFieldValue
.
Kind
()
==
reflect
.
Ptr
{
//this is a ptr, allocate a new value and set it
//then we can dereference to set it below
...
...
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
sign in
to comment