feat: v1 API cleanup with helpers and improved JSON handling#5
Merged
Conversation
Drop the mapstructure dependency in favor of custom MarshalJSON / UnmarshalJSON, fix a handful of usability issues, and add convenience helpers for the common cases. Behavior changes: - ServeHTTP defaults Status to 500 when unset and writes a minimal fallback problem body if serialization fails. - Title and Instance now use omitempty. - WithExtensions is additive across calls. - Extension keys colliding with reserved members return ErrExtensionKeyCollision instead of being silently dropped. - FromJSON populates Type with "about:blank" when missing, per the RFC. API additions: - FromError(err, status) wraps a Go error as a problem. - *RFC9457 implements the error interface (nil-safe). - Status-named constructors: BadRequest, Unauthorized, Forbidden, NotFound, MethodNotAllowed, Conflict, Gone, UnprocessableEntity, TooManyRequests, InternalServerError, NotImplemented, BadGateway, ServiceUnavailable, GatewayTimeout. - Compile-time assertions for error, http.Handler, json.Marshaler, and json.Unmarshaler. - doc.go with a package overview. API changes (note: signatures break callers but this lands as a minor bump intentionally rather than cutting a /v2 module path): - ToJSON now returns []byte instead of string. - FromJSON now accepts []byte instead of string. - The redundant Write() method is removed (use ServeHTTP). - RFC9457Error (int iota) is replaced with sentinel errors: ErrUnableToMarshalJSON, ErrUnableToUnmarshalJSON, ErrExtensionKeyCollision. The translate-* variants are removed alongside mapstructure. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
0bcd093 to
6170a63
Compare
Member
Author
|
🎉 This PR is included in version 1.1.0 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
mapstructurein favor of customMarshalJSON/UnmarshalJSON, fixes several usability issues, and adds convenience helpers.FromError, status-named constructors (BadRequest,NotFound, etc.), and makes*RFC9457satisfy theerrorinterface./v2module path, even though some signatures change for callers — see "API changes" below.Behavior changes
ServeHTTPdefaultsStatusto 500 when unset; writes a minimal fallback body if serialization fails.TitleandInstancenow useomitempty.WithExtensionsis additive across calls.type,status,title,detail,instance) returnErrExtensionKeyCollision.FromJSONpopulatesTypewith"about:blank"when missing, per the RFC.API additions
FromError(err, status)to wrap a Go error.*RFC9457implementserror(nil-safe).BadRequest,Unauthorized,Forbidden,NotFound,MethodNotAllowed,Conflict,Gone,UnprocessableEntity,TooManyRequests,InternalServerError,NotImplemented,BadGateway,ServiceUnavailable,GatewayTimeout.error,http.Handler,json.Marshaler,json.Unmarshaler.doc.gopackage overview.API changes
Signatures break callers but this lands as a minor bump intentionally:
ToJSONnow returns[]byte(wasstring).FromJSONnow accepts[]byte(wasstring).Write()removed — useServeHTTP(already implementshttp.Handler).RFC9457Error(int iota) replaced with sentinel errors:ErrUnableToMarshalJSON,ErrUnableToUnmarshalJSON,ErrExtensionKeyCollision. The translate-* variants are removed along withmapstructure.Test plan
go vet ./...go build ./...go test ./...— all greengo mod tidy—mapstructureremoved🤖 Generated with Claude Code