From 70dc9b190c83ec7e7a1fbf59613fed4e269b05a4 Mon Sep 17 00:00:00 2001 From: Tom Fleet Date: Thu, 11 Sep 2025 08:32:57 +0100 Subject: [PATCH] Add options --- Taskfile.yml | 4 -- problem.go | 144 +++++++++++++++++++++++++++++++++++++-- problem_test.go | 2 - testdata/empty.json | 4 +- testdata/no-extra.json | 2 +- testdata/with-extra.json | 2 +- 6 files changed, 140 insertions(+), 18 deletions(-) diff --git a/Taskfile.yml b/Taskfile.yml index a62aea5..941a797 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -38,8 +38,6 @@ tasks: cmds: - go test -race ./... {{ .CLI_ARGS }} - - bench: desc: Run all project benchmarks sources: @@ -100,5 +98,3 @@ tasks: cmds: - go get -u ./... - go mod tidy - - diff --git a/problem.go b/problem.go index 84c482b..6849f56 100644 --- a/problem.go +++ b/problem.go @@ -1,5 +1,3 @@ -//go:build goexperiment.jsonv2 - // Package problem provides a data structure for an [RFC-7807] "problem" used in HTTP APIs. // // HTTP status codes are sometimes not sufficient to convey enough information about an @@ -53,7 +51,7 @@ type Problem struct { // although it MAY be localized to suit client preferences (expressed with the Accept-Language request header). // // Consumers SHOULD NOT automatically dereference the type URI. - Type string `json:"type,omitempty"` + Type string `json:"type,omitzero"` // Title is a short, human-readable summary of the problem type. // @@ -62,13 +60,13 @@ type Problem struct { // The title string is advisory and included only for users who are not aware // of the semantics of the "Type" URI and do not have the ability to discover them // (e.g., offline log analysis). - Title string `json:"title,omitempty"` + Title string `json:"title,omitzero"` // Detail is helpful messaging to the consumer about how to correct the problem. // // Consumers SHOULD NOT parse the "detail" member for information; extensions are more suitable and // less error-prone ways to obtain such information. - Detail string `json:"detail,omitempty"` + Detail string `json:"detail,omitzero"` // Instance is a URI reference that identifies the specific occurrence of the problem. // @@ -76,7 +74,7 @@ type Problem struct { // // Note that Instance may be a relative URI, in which case it should be interpreted as relative // to the server's base URI. - Instance string `json:"instance,omitempty"` + Instance string `json:"instance,omitzero"` // Status is the HTTP status code generated by the server for this occurrence of the problem. // @@ -89,5 +87,137 @@ type Problem struct { // the generator was, in cases where it has been changed (e.g., by an intermediary or cache), // and when message bodies persist without HTTP information. Generic HTTP software will still // use the HTTP status code. - Status int `json:"status,omitempty"` + Status int `json:"status,omitzero"` +} + +// New returns a new [Problem]. +// +// The only field that is set by default is Type, which is set to 'about:blank'. +func New(options ...Option) Problem { + problem := Problem{ + Type: "about:blank", + } + + for _, option := range options { + option(&problem) + } + + return problem +} + +// Option is a functional option passed to [New] in order to modify a [Problem]. +type Option func(*Problem) + +// Type is an [Option] that sets the Type of the problem. +// +// Type is a URI reference that identifies the problem type which, when looked up, +// should provide human-readable documentation for the problem type. Consumers MUST +// use the "type" string as the primary identifier for the problem type. +// +// Note that Type may be a relative URI, in which case it should be interpreted as relative +// to the server's base URI. +// +// prob := problem.New(problem.Type("https://api.mysite.com/docs/errors")) +func Type(t string) Option { + return func(p *Problem) { + p.Type = t + } +} + +// Title is an [Option] that sets the Title of the problem. +// +// The title is a short, human-readable summary of the problem type which should not change +// from occurrence to occurrence of the problem. +// +// The title string is advisory and included only for users who are not aware +// of the semantics of the "Type" URI and do not have the ability to discover them +// (e.g., offline log analysis). +// +// prob := problem.New(problem.Title("Insufficient Funds")) +func Title(title string) Option { + return func(p *Problem) { + p.Title = title + } +} + +// Detail is an [Option] that sets the Detail of the problem. +// +// Detail is helpful messaging to the consumer about how to correct the problem. +// +// prob := problem.New(problem.Detail("Typically happens when your balance is too low")) +func Detail(detail string) Option { + return func(p *Problem) { + p.Detail = detail + } +} + +// Instance is an [Option] that sets the Instance of the problem. +// +// It is a URI reference that identifies the specific occurrence of the problem which +// may or may not yield further information if looked up. +// +// Note that Instance may be a relative URI, in which case it should be interpreted as relative +// to the server's base URI. +// +// prob := problem.New(problem.Instance("https://api.mything.io/errors/{specific_id}")) +func Instance(instance string) Option { + return func(p *Problem) { + p.Instance = instance + } +} + +// Status is an [Option] that sets the Status of the problem. +// +// Status is the HTTP status code generated by the server for this occurrence of the problem. +// +// The "status" member, if present, is only advisory; it conveys the HTTP status code used +// for the convenience of the consumer. Generators MUST use the same status code in the +// actual HTTP response, to assure that generic HTTP software that does not understand this +// format still behaves correctly. +// +// prob := problem.New(problem.Status(http.StatusBadRequest)) +func Status(status int) Option { + return func(p *Problem) { + p.Status = status + } +} + +// Extra is an [Option] that adds a key, value pair to the additional +// response data. +// +// This option adds a single key, value pair at a time, if you want to add an +// entire map at once, use [ExtraMap]. +// +// If a key is already present, it will be overridden. +// +// The "extra" member holds any arbitrary extension data useful to a specific error. +// +// Clients consuming problem details MUST ignore any such extensions that they don't recognize; +// this allows problem types to evolve and include additional information in the future. +func Extra(key string, value any) Option { + return func(p *Problem) { + // In case it hasn't been initialised yet + if p.Extra == nil { + p.Extra = make(map[string]any) + } + + p.Extra[key] = value + } +} + +// ExtraMap is an [Option] that sets the additional response data "Extra" to the +// passed in map. +// +// If you want to add a single key, value pair at a time, use [Extra]. +// +// This option will override any map currently set on the problem. +// +// The "extra" member holds any arbitrary extension data useful to a specific error. +// +// Clients consuming problem details MUST ignore any such extensions that they don't recognize; +// this allows problem types to evolve and include additional information in the future. +func ExtraMap(extra map[string]any) Option { + return func(p *Problem) { + p.Extra = extra + } } diff --git a/problem_test.go b/problem_test.go index 72c7a1b..857fe1e 100644 --- a/problem_test.go +++ b/problem_test.go @@ -1,5 +1,3 @@ -//go:build goexperiment.jsonv2 - package problem_test import ( diff --git a/testdata/empty.json b/testdata/empty.json index c00582e..9e26dfe 100644 --- a/testdata/empty.json +++ b/testdata/empty.json @@ -1,3 +1 @@ -{ - "status": 0 -} +{} \ No newline at end of file diff --git a/testdata/no-extra.json b/testdata/no-extra.json index 49b01eb..f12ea34 100644 --- a/testdata/no-extra.json +++ b/testdata/no-extra.json @@ -4,4 +4,4 @@ "detail": "Your current balance is 30, but that costs 50", "instance": "/account/12345/msgs/abc", "status": 400 -} +} \ No newline at end of file diff --git a/testdata/with-extra.json b/testdata/with-extra.json index 012ad01..891e5cc 100644 --- a/testdata/with-extra.json +++ b/testdata/with-extra.json @@ -9,4 +9,4 @@ "/accounts/67890" ], "balance": 30 -} +} \ No newline at end of file -- knotmirror 0.0.1