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
Merge requests
!11
Use Scan() method before json.Unmarshal for param values
Code
Review changes
Check out branch
Download
Patches
Plain diff
Expand sidebar
Merged
Use Scan() method before json.Unmarshal for param values
18-use-scan-for-param-values
into
main
Overview
0
Commits
1
Pipelines
0
Changes
1
Merged
Pieter van Staden
requested to merge
18-use-scan-for-param-values
into
main
3 years ago
Overview
0
Commits
1
Pipelines
0
Changes
1
Related to
#18 (closed)
Details in issue
0
0
Merge request reports
Compare
main
main (base)
and
latest version
latest version
00b0546b
1 commit,
3 years ago
1 file
+
28
−
12
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
struct_utils/named_values_to_struct.go
+
28
−
12
View file @ 00b0546b
Edit in single-file editor
Open in Web IDE
Show full file
package
struct_utils
import
(
"database/sql"
"encoding/csv"
"encoding/json"
"reflect"
@@ -284,19 +285,34 @@ func unmarshalValue(v interface{}, t reflect.Type) (reflect.Value, error) {
newValuePtr
:=
reflect
.
New
(
t
)
if
reflect
.
ValueOf
(
v
)
.
Type
()
.
AssignableTo
(
t
)
{
newValuePtr
.
Elem
()
.
Set
(
reflect
.
ValueOf
(
v
))
//can assign as is
}
else
{
//needs conversion
s
,
ok
:=
v
.
(
string
)
if
!
ok
{
jsonValue
,
_
:=
json
.
Marshal
(
v
)
s
=
string
(
jsonValue
)
}
//is string value, unmarshal as quoted or unquoted JSON value
if
err
:=
json
.
Unmarshal
([]
byte
(
"
\"
"
+
s
+
"
\"
"
),
newValuePtr
.
Interface
());
err
!=
nil
{
if
err
:=
json
.
Unmarshal
([]
byte
(
s
),
newValuePtr
.
Interface
());
err
!=
nil
{
return
newValuePtr
.
Elem
(),
errors
.
Wrapf
(
err
,
"invalid
\"
%s
\"
"
,
s
)
}
return
newValuePtr
.
Elem
(),
nil
}
//needs conversion
s
,
ok
:=
v
.
(
string
)
if
!
ok
{
jsonValue
,
_
:=
json
.
Marshal
(
v
)
s
=
string
(
jsonValue
)
}
//now we have string value
if
valueScanner
,
ok
:=
newValuePtr
.
Interface
()
.
(
sql
.
Scanner
);
ok
{
//if has scanner - prefer that over json unmarshal
//because we do not know if json expects quoted/unquoted for this type
//and if we try quoted, it fail, then try unquoted, we have two different
//errors one one of them will be kind of meaning nothing, but which?
//scanner should always take the value as typed, so let's try that first
if
err
:=
valueScanner
.
Scan
(
s
);
err
==
nil
{
return
newValuePtr
.
Elem
(),
nil
}
}
//try JSON unmarshal as is else with quotes
if
err
:=
json
.
Unmarshal
([]
byte
(
s
),
newValuePtr
.
Interface
());
err
==
nil
{
return
newValuePtr
.
Elem
(),
nil
}
if
err
:=
json
.
Unmarshal
([]
byte
(
"
\"
"
+
s
+
"
\"
"
),
newValuePtr
.
Interface
());
err
!=
nil
{
return
newValuePtr
.
Elem
(),
errors
.
Wrapf
(
err
,
"invalid
\"
%s
\"
"
,
s
)
}
return
newValuePtr
.
Elem
(),
nil
}
Loading