gopkg/tool/json2go/parser_test.go

302 lines
12 KiB
Go

// Package json2go...
//
// Description : json2go...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-11-09 4:21 下午
package json2go
import (
"fmt"
"testing"
)
func TestParse(t *testing.T) {
testJSON := `{
"name": "zhang",
"test": {
"name": "aa",
"age": 18,
"third": {"a":"1", "b":"like"},
"result": true,
"result_fail":false
},
"list": [1,2,3,4],
"list1": [2.2, 3, 4, 5],
"list_object":[{"name":"zhang"}, {"age": 18}]
}`
testJSON = `{
"ref": "refs/heads/master",
"before": "4c8219477db1e93e5540c9af4889f2633a9ca9e9",
"after": "5888c985a9afd9f1b25807fade69c53938aef485",
"compare_url": "https://git.zhangdeman.cn/zhangdeman/manager-job/compare/4c8219477db1e93e5540c9af4889f2633a9ca9e9...5888c985a9afd9f1b25807fade69c53938aef485",
"commits": [
{
"id": "5888c985a9afd9f1b25807fade69c53938aef485",
"message": "更新文案\n",
"url": "https://git.zhangdeman.cn/zhangdeman/manager-job/commit/5888c985a9afd9f1b25807fade69c53938aef485",
"author": {
"name": "白茶清欢",
"email": "go_developer@163.com",
"username": "zhangdeman"
},
"committer": {
"name": "白茶清欢",
"email": "go_developer@163.com",
"username": "zhangdeman"
},
"verification": null,
"timestamp": "2021-11-11T17:54:40+08:00",
"added": [],
"removed": [],
"modified": [
"publish_admin.sh"
]
}
],
"head_commit": {
"id": "5888c985a9afd9f1b25807fade69c53938aef485",
"message": "更新文案\n",
"url": "https://git.zhangdeman.cn/zhangdeman/manager-job/commit/5888c985a9afd9f1b25807fade69c53938aef485",
"author": {
"name": "白茶清欢",
"email": "go_developer@163.com",
"username": "zhangdeman"
},
"committer": {
"name": "白茶清欢",
"email": "go_developer@163.com",
"username": "zhangdeman"
},
"verification": null,
"timestamp": "2021-11-11T17:54:40+08:00",
"added": [],
"removed": [],
"modified": [
"publish_admin.sh"
]
},
"repository": {
"id": 24,
"owner": {"id":1,"login":"zhangdeman","full_name":"","email":"go_developer@163.com","avatar_url":"https://git.zhangdeman.cn/user/avatar/zhangdeman/-1","language":"","is_admin":false,"last_login":"0001-01-01T00:00:00Z","created":"2021-05-04T12:58:50+08:00","restricted":false,"active":false,"prohibit_login":false,"location":"","website":"","description":"","visibility":"public","followers_count":0,"following_count":0,"starred_repos_count":0,"username":"zhangdeman"},
"name": "manager-job",
"full_name": "zhangdeman/manager-job",
"description": "各种管理JOB",
"empty": false,
"private": false,
"fork": false,
"template": false,
"parent": null,
"mirror": false,
"size": 24,
"html_url": "https://git.zhangdeman.cn/zhangdeman/manager-job",
"ssh_url": "www@git.zhangdeman.cn:zhangdeman/manager-job.git",
"clone_url": "https://git.zhangdeman.cn/zhangdeman/manager-job.git",
"original_url": "",
"website": "",
"stars_count": 0,
"forks_count": 0,
"watchers_count": 1,
"open_issues_count": 0,
"open_pr_counter": 0,
"release_counter": 0,
"default_branch": "master",
"archived": false,
"created_at": "2021-11-11T16:54:10+08:00",
"updated_at": "2021-11-11T17:02:22+08:00",
"permissions": {
"admin": true,
"push": true,
"pull": true
},
"has_issues": true,
"internal_tracker": {
"enable_time_tracker": false,
"allow_only_contributors_to_track_time": true,
"enable_issue_dependencies": true
},
"has_wiki": true,
"has_pull_requests": true,
"has_projects": true,
"ignore_whitespace_conflicts": false,
"allow_merge_commits": true,
"allow_rebase": true,
"allow_rebase_explicit": true,
"allow_squash_merge": true,
"default_merge_style": "merge",
"avatar_url": "",
"internal": false,
"mirror_interval": ""
},
"pusher": {"id":1,"login":"zhangdeman","full_name":"","email":"go_developer@163.com","avatar_url":"https://git.zhangdeman.cn/user/avatar/zhangdeman/-1","language":"","is_admin":false,"last_login":"0001-01-01T00:00:00Z","created":"2021-05-04T12:58:50+08:00","restricted":false,"active":false,"prohibit_login":false,"location":"","website":"","description":"","visibility":"public","followers_count":0,"following_count":0,"starred_repos_count":0,"username":"zhangdeman"},
"sender": {"id":1,"login":"zhangdeman","full_name":"","email":"go_developer@163.com","avatar_url":"https://git.zhangdeman.cn/user/avatar/zhangdeman/-1","language":"","is_admin":false,"last_login":"0001-01-01T00:00:00Z","created":"2021-05-04T12:58:50+08:00","restricted":false,"active":false,"prohibit_login":false,"location":"","website":"","description":"","visibility":"public","followers_count":0,"following_count":0,"starred_repos_count":0,"username":"zhangdeman"}
}
`
fmt.Println(NewJSON2GO("DemoJSON").Parse(testJSON))
}
type DemoJSON struct {
Repository struct {
Mirror bool `json:"mirror"`
OriginalUrl string `json:"original_url"`
StarsCount int64 `json:"stars_count"`
IgnoreWhitespaceConflicts bool `json:"ignore_whitespace_conflicts"`
FullName string `json:"full_name"`
Fork bool `json:"fork"`
CreatedAt string `json:"created_at"`
Internal bool `json:"internal"`
AllowRebaseExplicit bool `json:"allow_rebase_explicit"`
MirrorInterval string `json:"mirror_interval"`
CloneUrl string `json:"clone_url"`
WatchersCount int64 `json:"watchers_count"`
OpenIssuesCount int64 `json:"open_issues_count"`
ReleaseCounter int64 `json:"release_counter"`
InternalTracker struct {
EnableTimeTracker bool `json:"enable_time_tracker"`
AllowOnlyContributorsToTrackTime bool `json:"allow_only_contributors_to_track_time"`
EnableIssueDependencies bool `json:"enable_issue_dependencies"`
} `json:"internal_tracker"`
Name string `json:"name"`
Empty bool `json:"empty"`
SshUrl string `json:"ssh_url"`
ForksCount int64 `json:"forks_count"`
Permissions struct {
Pull bool `json:"pull"`
Admin bool `json:"admin"`
Push bool `json:"push"`
} `json:"permissions"`
Private bool `json:"private"`
Size int64 `json:"size"`
HasWiki bool `json:"has_wiki"`
HasProjects bool `json:"has_projects"`
AllowMergeCommits bool `json:"allow_merge_commits"`
AvatarUrl string `json:"avatar_url"`
Id int64 `json:"id"`
Description string `json:"description"`
Website string `json:"website"`
UpdatedAt string `json:"updated_at"`
HasPullRequests bool `json:"has_pull_requests"`
Parent map[string]interface{} `json:"parent"`
OpenPrCounter int64 `json:"open_pr_counter"`
HasIssues bool `json:"has_issues"`
AllowRebase bool `json:"allow_rebase"`
AllowSquashMerge bool `json:"allow_squash_merge"`
Owner struct {
IsAdmin bool `json:"is_admin"`
Website string `json:"website"`
Email string `json:"email"`
AvatarUrl string `json:"avatar_url"`
ProhibitLogin bool `json:"prohibit_login"`
Description string `json:"description"`
FollowingCount int64 `json:"following_count"`
StarredReposCount int64 `json:"starred_repos_count"`
Username string `json:"username"`
Login string `json:"login"`
Created string `json:"created"`
Visibility string `json:"visibility"`
FullName string `json:"full_name"`
LastLogin string `json:"last_login"`
Restricted bool `json:"restricted"`
Active bool `json:"active"`
Location string `json:"location"`
FollowersCount int64 `json:"followers_count"`
Id int64 `json:"id"`
Language string `json:"language"`
} `json:"owner"`
Template bool `json:"template"`
HtmlUrl string `json:"html_url"`
DefaultBranch string `json:"default_branch"`
Archived bool `json:"archived"`
DefaultMergeStyle string `json:"default_merge_style"`
} `json:"repository"`
Sender struct {
Created string `json:"created"`
Website string `json:"website"`
FollowingCount int64 `json:"following_count"`
StarredReposCount int64 `json:"starred_repos_count"`
FullName string `json:"full_name"`
Language string `json:"language"`
LastLogin string `json:"last_login"`
Visibility string `json:"visibility"`
FollowersCount int64 `json:"followers_count"`
Username string `json:"username"`
IsAdmin bool `json:"is_admin"`
Active bool `json:"active"`
Location string `json:"location"`
Restricted bool `json:"restricted"`
Description string `json:"description"`
AvatarUrl string `json:"avatar_url"`
ProhibitLogin bool `json:"prohibit_login"`
Id int64 `json:"id"`
Login string `json:"login"`
Email string `json:"email"`
} `json:"sender"`
Ref string `json:"ref"`
Before string `json:"before"`
After string `json:"after"`
HeadCommit struct {
Message string `json:"message"`
Url string `json:"url"`
Author struct {
Name string `json:"name"`
Email string `json:"email"`
Username string `json:"username"`
} `json:"author"`
Verification map[string]interface{} `json:"verification"`
Timestamp string `json:"timestamp"`
Id string `json:"id"`
Committer struct {
Name string `json:"name"`
Email string `json:"email"`
Username string `json:"username"`
} `json:"committer"`
Added []interface{} `json:"added"`
Removed []interface{} `json:"removed"`
Modified []string `json:"modified"`
} `json:"head_commit"`
CompareUrl string `json:"compare_url"`
Commits []struct {
Url string `json:"url"`
Author struct {
Name string `json:"name"`
Email string `json:"email"`
Username string `json:"username"`
} `json:"author"`
Committer struct {
Username string `json:"username"`
Name string `json:"name"`
Email string `json:"email"`
} `json:"committer"`
Verification map[string]interface{} `json:"verification"`
Removed []interface{} `json:"removed"`
Id string `json:"id"`
Message string `json:"message"`
Modified []string `json:"modified"`
Timestamp string `json:"timestamp"`
Added []interface{} `json:"added"`
} `json:"commits"`
Pusher struct {
Language string `json:"language"`
Website string `json:"website"`
Id int64 `json:"id"`
Email string `json:"email"`
Active bool `json:"active"`
Description string `json:"description"`
Visibility string `json:"visibility"`
Login string `json:"login"`
Restricted bool `json:"restricted"`
Location string `json:"location"`
StarredReposCount int64 `json:"starred_repos_count"`
Username string `json:"username"`
IsAdmin bool `json:"is_admin"`
ProhibitLogin bool `json:"prohibit_login"`
LastLogin string `json:"last_login"`
Created string `json:"created"`
FollowersCount int64 `json:"followers_count"`
FollowingCount int64 `json:"following_count"`
FullName string `json:"full_name"`
AvatarUrl string `json:"avatar_url"`
} `json:"pusher"`
}