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
!6
Search package improvements to retrieve documents with text searches from OpenSearch
Code
Review changes
Check out branch
Download
Patches
Plain diff
Expand sidebar
Merged
Search package improvements to retrieve documents with text searches from OpenSearch
12-search-retrieval
into
main
Overview
0
Commits
9
Pipelines
0
Changes
4
Merged
Pieter van Staden
requested to merge
12-search-retrieval
into
main
3 years ago
Overview
0
Commits
9
Pipelines
0
Changes
4
Related to
#12 (closed)
0
0
Merge request reports
Viewing commit
4d59a554
Prev
Next
Show latest version
4 files
+
272
−
1
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
4
4d59a554
More reflection utilities
· 4d59a554
Jan Semmelink
authored
3 years ago
reflection/get.go
0 → 100644
+
69
−
0
View file @ 4d59a554
Edit in single-file editor
Open in Web IDE
package
reflection
import
(
"fmt"
"reflect"
"strings"
"gitlab.com/uafrica/go-utils/errors"
)
func
Get
(
v
reflect
.
Value
,
key
string
)
(
reflect
.
Value
,
error
)
{
return
get
(
""
,
v
,
key
)
}
func
get
(
name
string
,
v
reflect
.
Value
,
key
string
)
(
reflect
.
Value
,
error
)
{
if
key
==
""
{
return
v
,
nil
}
switch
v
.
Kind
()
{
case
reflect
.
Ptr
:
return
get
(
name
,
v
.
Elem
(),
key
)
case
reflect
.
Struct
:
if
key
[
0
]
!=
'.'
{
return
v
,
errors
.
Errorf
(
"get(%s): key=
\"
%s
\"
does not start with '.'"
,
name
,
key
)
}
fieldName
:=
key
[
1
:
]
remainingKey
:=
""
index
:=
strings
.
IndexAny
(
fieldName
,
".["
)
if
index
>
0
{
fieldName
=
key
[
1
:
index
+
1
]
remainingKey
=
key
[
index
+
1
:
]
}
t
:=
v
.
Type
()
fieldIndex
:=
0
for
fieldIndex
=
0
;
fieldIndex
<
t
.
NumField
();
fieldIndex
++
{
if
strings
.
SplitN
(
t
.
Field
(
fieldIndex
)
.
Tag
.
Get
(
"json"
),
","
,
2
)[
0
]
==
fieldName
{
break
}
}
if
fieldIndex
>=
t
.
NumField
()
{
return
v
,
errors
.
Errorf
(
"%s does not have field %s"
,
name
,
fieldName
)
}
return
get
(
name
+
"."
+
fieldName
,
v
.
Field
(
fieldIndex
),
remainingKey
)
case
reflect
.
Slice
:
if
!
strings
.
HasPrefix
(
key
,
"[]"
)
{
return
v
,
errors
.
Errorf
(
"canot get %s from slice, expecting
\"
[]
\"
in the key"
,
key
)
}
//make array of results from each item in the slice
var
result
reflect
.
Value
for
i
:=
0
;
i
<
v
.
Len
();
i
++
{
if
vv
,
err
:=
get
(
fmt
.
Sprintf
(
"%s[%d]"
,
name
,
i
),
v
.
Index
(
i
),
key
[
2
:
]);
err
!=
nil
{
return
v
,
errors
.
Wrapf
(
err
,
"failed on %s[%d]"
,
name
,
i
)
}
else
{
if
!
result
.
IsValid
()
{
result
=
reflect
.
MakeSlice
(
reflect
.
SliceOf
(
vv
.
Type
()),
0
,
v
.
Len
())
}
result
=
reflect
.
Append
(
result
,
vv
)
}
}
return
result
,
nil
default
:
}
return
v
,
errors
.
Errorf
(
"Cannot get %s from %s"
,
key
,
v
.
Kind
())
}
Loading