From 7798a353509448e5d706274115f3dd79835e9af5 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Thu, 5 Jun 2025 00:39:02 +0700 Subject: all: encode the whole BrokenlinksResult struct to JSON Previously, we only encode the BrokenlinksResult.PageLinks. The struct may changes in the future, so its better to encode the whole struct now rather than changing the output later. --- brokenlinks.go | 2 +- brokenlinks_worker.go | 2 +- cmd/jarink/main.go | 2 +- testdata/past_result.json | 14 ++++++++------ 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/brokenlinks.go b/brokenlinks.go index 833e2d2..4be4fe8 100644 --- a/brokenlinks.go +++ b/brokenlinks.go @@ -33,7 +33,7 @@ type BrokenlinksOptions struct { // BrokenlinksResult store the result of scanning for broken links. type BrokenlinksResult struct { // PageLinks store the page and its broken links. - PageLinks map[string][]Broken + PageLinks map[string][]Broken `json:"page_links"` } func newBrokenlinksResult() *BrokenlinksResult { diff --git a/brokenlinks_worker.go b/brokenlinks_worker.go index dbb3453..03a89bd 100644 --- a/brokenlinks_worker.go +++ b/brokenlinks_worker.go @@ -82,7 +82,7 @@ func newWorker(opts BrokenlinksOptions) (wrk *brokenlinksWorker, err error) { } wrk.pastResult = newBrokenlinksResult() - err = json.Unmarshal(pastresult, &wrk.pastResult.PageLinks) + err = json.Unmarshal(pastresult, &wrk.pastResult) if err != nil { return nil, err } diff --git a/cmd/jarink/main.go b/cmd/jarink/main.go index c8ba2e2..cba254f 100644 --- a/cmd/jarink/main.go +++ b/cmd/jarink/main.go @@ -45,7 +45,7 @@ func main() { } var resultJson []byte - resultJson, err = json.MarshalIndent(result.PageLinks, ``, ` `) + resultJson, err = json.MarshalIndent(result, ``, ` `) if err != nil { log.Fatal(err.Error()) } diff --git a/testdata/past_result.json b/testdata/past_result.json index 3ba37c1..a5a11fe 100644 --- a/testdata/past_result.json +++ b/testdata/past_result.json @@ -1,8 +1,10 @@ { - "http://127.0.0.1:11836/page2": [ - { - "link": "http://127.0.0.1:11836/", - "code": 404 - } - ] + "page_links": { + "http://127.0.0.1:11836/page2": [ + { + "link": "http://127.0.0.1:11836/", + "code": 404 + } + ] + } } -- cgit v1.3