Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
G
gostnops
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wei.xuan
gostnops
Commits
33cb3cd6
Commit
33cb3cd6
authored
Dec 07, 2020
by
李志信
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: add comment
parent
e8e545a4
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
12 additions
and
3 deletions
+12
-3
parser.go
encoding/json/parser.go
+12
-3
No files found.
encoding/json/parser.go
View file @
33cb3cd6
...
@@ -37,6 +37,7 @@ type HessianRegisterPair struct {
...
@@ -37,6 +37,7 @@ type HessianRegisterPair struct {
Obj
interface
{}
Obj
interface
{}
}
}
// jsonStructParser can use reflect to create arbitrary interface{} of go, from user defined json file.
type
jsonStructParser
struct
{
type
jsonStructParser
struct
{
structFields
[]
reflect
.
StructField
structFields
[]
reflect
.
StructField
hessianRegisterPair
[]
HessianRegisterPair
hessianRegisterPair
[]
HessianRegisterPair
...
@@ -44,6 +45,7 @@ type jsonStructParser struct {
...
@@ -44,6 +45,7 @@ type jsonStructParser struct {
subObjValueMap
map
[
string
]
reflect
.
Value
subObjValueMap
map
[
string
]
reflect
.
Value
}
}
// newJSONStructParser create a new json struct parser
func
newJSONStructParser
()
*
jsonStructParser
{
func
newJSONStructParser
()
*
jsonStructParser
{
return
&
jsonStructParser
{
return
&
jsonStructParser
{
structFields
:
make
([]
reflect
.
StructField
,
0
,
16
),
structFields
:
make
([]
reflect
.
StructField
,
0
,
16
),
...
@@ -53,7 +55,7 @@ func newJSONStructParser() *jsonStructParser {
...
@@ -53,7 +55,7 @@ func newJSONStructParser() *jsonStructParser {
}
}
}
}
// File2Interface
parse json @filePath
to interface
// File2Interface
first read json byte from @filePath, and parse it
to interface
func
File2Interface
(
filePath
string
)
([]
HessianRegisterPair
,
interface
{},
error
)
{
func
File2Interface
(
filePath
string
)
([]
HessianRegisterPair
,
interface
{},
error
)
{
defer
func
()
{
defer
func
()
{
defaultJSONStructParser
=
newJSONStructParser
()
defaultJSONStructParser
=
newJSONStructParser
()
...
@@ -77,7 +79,8 @@ func RemoveTargetNameField(v interface{}, targetName string) interface{} {
...
@@ -77,7 +79,8 @@ func RemoveTargetNameField(v interface{}, targetName string) interface{} {
func
(
jsp
*
jsonStructParser
)
cb
(
key
[]
byte
,
value
[]
byte
,
dataType
jsonparser
.
ValueType
,
offset
int
)
error
{
func
(
jsp
*
jsonStructParser
)
cb
(
key
[]
byte
,
value
[]
byte
,
dataType
jsonparser
.
ValueType
,
offset
int
)
error
{
switch
dataType
{
switch
dataType
{
case
jsonparser
.
Object
:
// sub interface parse
case
jsonparser
.
Object
:
// parse sub interface, use a new parser to deal with it the same way
newParser
:=
newJSONStructParser
()
newParser
:=
newJSONStructParser
()
subObj
:=
newParser
.
json2Struct
(
value
)
subObj
:=
newParser
.
json2Struct
(
value
)
javaClassName
,
err
:=
getJavaClassName
(
subObj
)
javaClassName
,
err
:=
getJavaClassName
(
subObj
)
...
@@ -141,13 +144,19 @@ func (jsp *jsonStructParser) cb(key []byte, value []byte, dataType jsonparser.Va
...
@@ -141,13 +144,19 @@ func (jsp *jsonStructParser) cb(key []byte, value []byte, dataType jsonparser.Va
return
nil
return
nil
}
}
// json2Struct parse data from json file to user defined interface
func
(
jsp
*
jsonStructParser
)
json2Struct
(
jsonData
[]
byte
)
interface
{}
{
func
(
jsp
*
jsonStructParser
)
json2Struct
(
jsonData
[]
byte
)
interface
{}
{
// first: call ObjectEach to parse jsonData to reflect.StructField item
if
err
:=
jsonparser
.
ObjectEach
(
jsonData
,
jsp
.
cb
);
err
!=
nil
{
if
err
:=
jsonparser
.
ObjectEach
(
jsonData
,
jsp
.
cb
);
err
!=
nil
{
log
.
Println
(
"jsonparser.ObjectEach error = "
,
err
)
log
.
Println
(
"jsonparser.ObjectEach error = "
,
err
)
}
}
// second: parse structField to reflectType
typ
:=
reflect
.
StructOf
(
jsp
.
structFields
)
typ
:=
reflect
.
StructOf
(
jsp
.
structFields
)
v
:=
reflect
.
New
(
typ
)
.
Elem
()
v
:=
reflect
.
New
(
typ
)
.
Elem
()
newty
:=
reflect
.
TypeOf
(
v
.
Addr
()
.
Interface
())
.
Elem
()
newty
:=
reflect
.
TypeOf
(
v
.
Addr
()
.
Interface
())
.
Elem
()
// finally: traverse each json field, and set user defined value
for
i
:=
0
;
i
<
typ
.
NumField
();
i
++
{
for
i
:=
0
;
i
<
typ
.
NumField
();
i
++
{
valStr
,
ok1
:=
jsp
.
valueMap
[
newty
.
Field
(
i
)
.
Name
]
valStr
,
ok1
:=
jsp
.
valueMap
[
newty
.
Field
(
i
)
.
Name
]
subObj
,
ok2
:=
jsp
.
subObjValueMap
[
newty
.
Field
(
i
)
.
Name
]
subObj
,
ok2
:=
jsp
.
subObjValueMap
[
newty
.
Field
(
i
)
.
Name
]
...
@@ -180,7 +189,6 @@ func (jsp *jsonStructParser) json2Struct(jsonData []byte) interface{} {
...
@@ -180,7 +189,6 @@ func (jsp *jsonStructParser) json2Struct(jsonData []byte) interface{} {
if
valStr
==
"true"
||
valStr
==
"1"
{
if
valStr
==
"true"
||
valStr
==
"1"
{
v
.
Field
(
i
)
.
SetBool
(
true
)
v
.
Field
(
i
)
.
SetBool
(
true
)
}
}
default
:
default
:
log
.
Println
(
"warning val: "
,
valStr
,
" in value is not supported"
)
log
.
Println
(
"warning val: "
,
valStr
,
" in value is not supported"
)
}
}
...
@@ -223,6 +231,7 @@ func (jsp *jsonStructParser) removeTargetNameField(v interface{}, targetName str
...
@@ -223,6 +231,7 @@ func (jsp *jsonStructParser) removeTargetNameField(v interface{}, targetName str
return
newi
.
Addr
()
.
Interface
()
return
newi
.
Addr
()
.
Interface
()
}
}
// getJavaClassName can read field JavaClassName of interface{}, used in cli-tool to do hessian registry
func
getJavaClassName
(
pkg
interface
{})
(
string
,
error
)
{
func
getJavaClassName
(
pkg
interface
{})
(
string
,
error
)
{
val
:=
reflect
.
ValueOf
(
pkg
)
.
Elem
()
val
:=
reflect
.
ValueOf
(
pkg
)
.
Elem
()
typ
:=
reflect
.
TypeOf
(
pkg
)
.
Elem
()
typ
:=
reflect
.
TypeOf
(
pkg
)
.
Elem
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment