From c1be7a1408ed30cff2e8e359cf7e1ef69d66e988 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sun, 17 May 2026 12:35:17 +0300 Subject: [PATCH] json2 --- api/endpoints_test.v | 38 +++++++++++++++++++------------------- ci/ci_routes.v | 10 +++++----- ci/ci_trigger.v | 4 ++-- config/loader.v | 4 ++-- github.v | 10 +++++----- tests/first_run.v | 8 ++++---- webhook.v | 2 +- 7 files changed, 38 insertions(+), 38 deletions(-) diff --git a/api/endpoints_test.v b/api/endpoints_test.v index eed915b..8aeb027 100644 --- a/api/endpoints_test.v +++ b/api/endpoints_test.v @@ -29,7 +29,7 @@ import os import log import net.http import time -import json +import x.json2 as json const test_port = 8765 const test_url = 'http://127.0.0.1:${test_port}' @@ -234,7 +234,7 @@ fn fetch_test_repo_id() !int { if resp.status_code != 200 { return error('listing returned ${resp.status_code}') } - repos := json.decode([]ApiRepoSummary, resp.body)! + repos := json.decode[[]ApiRepoSummary](resp.body)! for r in repos { if r.name == test_repo { return r.id @@ -316,7 +316,7 @@ fn test_api_v1_me_with_bearer() { header: bearer_header() ) or { panic(err) } assert resp.status_code == 200 - user := json.decode(ApiUserSummary, resp.body) or { panic(err) } + user := json.decode[ApiUserSummary](resp.body) or { panic(err) } assert user.username == test_username } @@ -329,14 +329,14 @@ fn test_api_v1_me_with_session_cookie() { } ) or { panic(err) } assert resp.status_code == 200 - user := json.decode(ApiUserSummary, resp.body) or { panic(err) } + user := json.decode[ApiUserSummary](resp.body) or { panic(err) } assert user.username == test_username } fn test_api_v1_user_lookup() { resp := http.get(url('/api/v1/users/${test_username}')) or { panic(err) } assert resp.status_code == 200 - user := json.decode(ApiUserSummary, resp.body) or { panic(err) } + user := json.decode[ApiUserSummary](resp.body) or { panic(err) } assert user.username == test_username missing := http.get(url('/api/v1/users/ghost_user')) or { panic(err) } @@ -346,7 +346,7 @@ fn test_api_v1_user_lookup() { fn test_api_v1_user_repos() { resp := http.get(url('/api/v1/users/${test_username}/repos')) or { panic(err) } assert resp.status_code == 200 - repos := json.decode([]ApiRepoSummary, resp.body) or { panic(err) } + repos := json.decode[[]ApiRepoSummary](resp.body) or { panic(err) } assert repos.len >= 1 mut found := false for r in repos { @@ -361,7 +361,7 @@ fn test_api_v1_user_repos() { fn test_api_v1_repo_show() { resp := http.get(url('/api/v1/repos/${test_username}/${test_repo}')) or { panic(err) } assert resp.status_code == 200 - r := json.decode(ApiRepoSummary, resp.body) or { panic(err) } + r := json.decode[ApiRepoSummary](resp.body) or { panic(err) } assert r.name == test_repo assert r.user_name == test_username @@ -372,7 +372,7 @@ fn test_api_v1_repo_show() { fn test_api_v1_repo_issues_list_empty() { resp := http.get(url('/api/v1/repos/${test_username}/${test_repo}/issues')) or { panic(err) } assert resp.status_code == 200 - issues := json.decode([]ApiIssueSummary, resp.body) or { panic(err) } + issues := json.decode[[]ApiIssueSummary](resp.body) or { panic(err) } assert issues.len == 0 } @@ -408,19 +408,19 @@ fn test_api_v1_create_issue_succeeds() { data: 'title=first-issue&body=hello' ) or { panic(err) } assert resp.status_code == 200 - issue := json.decode(ApiIssueSummary, resp.body) or { panic(err) } + issue := json.decode[ApiIssueSummary](resp.body) or { panic(err) } assert issue.title == 'first-issue' assert issue.status == 'open' listing := http.get(url('/api/v1/repos/${test_username}/${test_repo}/issues')) or { panic(err) } - issues := json.decode([]ApiIssueSummary, listing.body) or { panic(err) } + issues := json.decode[[]ApiIssueSummary](listing.body) or { panic(err) } assert issues.len >= 1 single := http.get(url('/api/v1/repos/${test_username}/${test_repo}/issues/${issue.id}')) or { panic(err) } assert single.status_code == 200 - got := json.decode(ApiIssueSummary, single.body) or { panic(err) } + got := json.decode[ApiIssueSummary](single.body) or { panic(err) } assert got.id == issue.id } @@ -434,7 +434,7 @@ fn test_api_v1_repo_issue_not_found() { fn test_api_v1_repo_pulls_empty() { resp := http.get(url('/api/v1/repos/${test_username}/${test_repo}/pulls')) or { panic(err) } assert resp.status_code == 200 - prs := json.decode([]ApiPullSummary, resp.body) or { panic(err) } + prs := json.decode[[]ApiPullSummary](resp.body) or { panic(err) } assert prs.len == 0 } @@ -459,7 +459,7 @@ fn test_api_v1_issues_count() { } ) or { panic(err) } assert resp.status_code == 200 - decoded := json.decode(ApiIssueCount, resp.body) or { panic(err) } + decoded := json.decode[ApiIssueCount](resp.body) or { panic(err) } assert decoded.success assert decoded.result >= 1 } @@ -478,7 +478,7 @@ fn test_api_v1_branches_count() { } ) or { panic(err) } assert resp.status_code == 200 - decoded := json.decode(ApiBranchCount, resp.body) or { panic(err) } + decoded := json.decode[ApiBranchCount](resp.body) or { panic(err) } assert decoded.success assert decoded.result == 0 } @@ -492,7 +492,7 @@ fn test_api_v1_commits_count() { } ) or { panic(err) } assert resp.status_code == 200 - decoded := json.decode(ApiCommitCount, resp.body) or { panic(err) } + decoded := json.decode[ApiCommitCount](resp.body) or { panic(err) } assert decoded.success assert decoded.result == 0 } @@ -507,7 +507,7 @@ fn test_api_v1_repo_star_toggle() { } ) or { panic(err) } assert resp.status_code == 200 - first := json.decode(ApiBoolResult, resp.body) or { panic(err) } + first := json.decode[ApiBoolResult](resp.body) or { panic(err) } assert first.success assert first.result == true @@ -518,7 +518,7 @@ fn test_api_v1_repo_star_toggle() { 'token': session_cookie() } ) or { panic(err) } - second := json.decode(ApiBoolResult, resp2.body) or { panic(err) } + second := json.decode[ApiBoolResult](resp2.body) or { panic(err) } assert second.result == false } @@ -532,7 +532,7 @@ fn test_api_v1_repo_watch_toggle() { } ) or { panic(err) } assert resp.status_code == 200 - first := json.decode(ApiBoolResult, resp.body) or { panic(err) } + first := json.decode[ApiBoolResult](resp.body) or { panic(err) } assert first.success } @@ -546,7 +546,7 @@ fn test_api_v1_repo_tree_files_with_branch() { rid := repo_id() resp := http.get(url('/api/v1/repos/${rid}/tree/files?branch=main')) or { panic(err) } assert resp.status_code == 200 - decoded := json.decode(ApiFilesResult, resp.body) or { panic(err) } + decoded := json.decode[ApiFilesResult](resp.body) or { panic(err) } assert decoded.success } diff --git a/ci/ci_routes.v b/ci/ci_routes.v index 3d995bc..a2b33b5 100644 --- a/ci/ci_routes.v +++ b/ci/ci_routes.v @@ -2,7 +2,7 @@ module main import veb import api -import json +import x.json2 as json import net.http import time import git @@ -19,7 +19,7 @@ struct CiStatusCallback { @['/api/v1/ci/status'; post] pub fn (mut app App) handle_ci_status_callback() veb.Result { body := ctx.req.data - callback := json.decode(CiStatusCallback, body) or { + callback := json.decode[CiStatusCallback](body) or { return ctx.json_error('Invalid request body') } @@ -62,7 +62,7 @@ pub fn (mut app App) ci_runs(username string, repo_name string) veb.Result { http.Response{} } if !ci_service_error && response.status_code == 200 { - runs_resp := json.decode(CiApiRunListResponse, response.body) or { + runs_resp := json.decode[CiApiRunListResponse](response.body) or { CiApiRunListResponse{} } if runs_resp.success { @@ -113,7 +113,7 @@ pub fn (mut app App) ci_run_detail(username string, repo_name string, run_id_str ci_run_json := response.body // Parse the response to display - run_data := json.decode(CiApiRunResponse, ci_run_json) or { return ctx.not_found() } + run_data := json.decode[CiApiRunResponse](ci_run_json) or { return ctx.not_found() } ci_run := run_data.result @@ -144,7 +144,7 @@ pub fn (mut app App) ci_restart_run(username string, repo_name string, run_id_st return ctx.not_found() } - result := json.decode(CiApiRunResponse, response.body) or { return ctx.not_found() } + result := json.decode[CiApiRunResponse](response.body) or { return ctx.not_found() } if result.success { new_run := result.result diff --git a/ci/ci_trigger.v b/ci/ci_trigger.v index a3f7fa8..aa36eec 100644 --- a/ci/ci_trigger.v +++ b/ci/ci_trigger.v @@ -1,6 +1,6 @@ module main -import json +import x.json2 as json import net.http import os import git @@ -90,7 +90,7 @@ fn (mut app App) send_ci_trigger(repo Repo, branch_name string, yaml_config stri } if response.status_code == 200 { - result := json.decode(CiTriggerResponse, response.body) or { + result := json.decode[CiTriggerResponse](response.body) or { app.warn('Failed to parse CI trigger response') return } diff --git a/config/loader.v b/config/loader.v index 7c36ad4..cb91a7b 100644 --- a/config/loader.v +++ b/config/loader.v @@ -1,7 +1,7 @@ module config import os -import json +import x.json2 as json pub struct Config { pub: @@ -33,5 +33,5 @@ pub: pub fn read_config(path string) !Config { config_raw := os.read_file(path)! - return json.decode(Config, config_raw)! + return json.decode[Config](config_raw)! } diff --git a/github.v b/github.v index 2174daa..ca7451f 100644 --- a/github.v +++ b/github.v @@ -3,7 +3,7 @@ module main import veb -import json +import x.json2 as json import net.http import time // import veb.auth as oauth @@ -122,7 +122,7 @@ fn fetch_github_repo_description(clone_url string) string { eprintln('[github-info] non-200 status ${resp.status_code}: ${resp.body#[..200]}') return '' } - info := json.decode(GitHubRepoInfo, resp.body) or { + info := json.decode[GitHubRepoInfo](resp.body) or { eprintln('[github-info] cannot decode response: ${err}') return '' } @@ -146,7 +146,7 @@ fn (mut app App) import_github_contributors(repo_id int, clone_url string) ! { if resp.status_code != 200 { return error('github api ${resp.status_code}: ${resp.body}') } - contributors := json.decode([]GitHubContributor, resp.body) or { + contributors := json.decode[[]GitHubContributor](resp.body) or { return error('cannot decode github contributors: ${err}') } if contributors.len == 0 { @@ -223,7 +223,7 @@ fn (mut app App) import_github_issues(repo_id int, clone_url string, owner_user_ eprintln('[github-import] ERROR body: ${resp.body}') return error('github api ${resp.status_code}: ${resp.body}') } - issues := json.decode([]GitHubIssue, resp.body) or { + issues := json.decode[[]GitHubIssue](resp.body) or { eprintln('[github-import] ERROR: cannot decode response: ${err}') eprintln('[github-import] response body was: ${resp.body#[..1000]}') return error('cannot decode github issues: ${err}') @@ -336,7 +336,7 @@ pub fn (mut app App) handle_oauth() veb.Result { return ctx.text('Received ${user_response.status_code} error while attempting to contact GitHub') } - github_user := json.decode(GitHubUser, user_response.body) or { return ctx.redirect_to_index() } + github_user := json.decode[GitHubUser](user_response.body) or { return ctx.redirect_to_index() } if github_user.email.trim_space().len == 0 { app.add_security_log( diff --git a/tests/first_run.v b/tests/first_run.v index 428a006..e6779f8 100644 --- a/tests/first_run.v +++ b/tests/first_run.v @@ -272,7 +272,7 @@ fn test_api_branches_count(username string, repo_name string) { assert api_branches_count_result.status_code == 200 - response_json := json.decode(api.ApiBranchCount, api_branches_count_result.body) or { + response_json := json.decode[api.ApiBranchCount](api_branches_count_result.body) or { exit_with_message(err.str()) } assert response_json.result > 0 @@ -391,7 +391,7 @@ fn get_repo_commit_count(token string, username string, repo_name string, branch url: prepare_url('api/v1/${username}/${repo_name}/${branch_name}/commits/count') ) or { exit_with_message(err.str()) } - response_json := json.decode(api.ApiCommitCount, response.body) or { + response_json := json.decode[api.ApiCommitCount](response.body) or { exit_with_message(err.str()) } dump(response_json.result) @@ -408,7 +408,7 @@ fn get_repo_issue_count(token string, username string, repo_name string) int { url: prepare_url('api/v1/${username}/${repo_name}/issues/count') ) or { exit_with_message(err.str()) } - response_json := json.decode(api.ApiIssueCount, response.body) or { + response_json := json.decode[api.ApiIssueCount](response.body) or { exit_with_message(err.str()) } @@ -424,7 +424,7 @@ fn get_repo_branch_count(token string, username string, repo_name string) int { url: prepare_url('api/v1/${username}/${repo_name}/branches/count') ) or { exit_with_message(err.str()) } - response_json := json.decode(api.ApiBranchCount, response.body) or { + response_json := json.decode[api.ApiBranchCount](response.body) or { exit_with_message(err.str()) } diff --git a/webhook.v b/webhook.v index 8aadcb1..24e1b28 100644 --- a/webhook.v +++ b/webhook.v @@ -7,7 +7,7 @@ import net.http import crypto.hmac import crypto.sha256 import encoding.hex -import json +import x.json2 as json pub struct WebhookIssuePayload { action string -- 2.39.5