Files
zeekayandHanzo Dev 5d1623a055 Go 1.27.1, and the code it can say more simply
Modules declare go 1.27.1 and the build images follow. go fix applied the
modernizers; omitzero is not applied, since it changes what the JSON says.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-16 19:25:30 -07:00

66 lines
1.9 KiB
Go

// Copyright (C) 2019-2025, Lux Industries, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
// Package json provides JSON serialization utilities for numeric types and
// a gorilla/rpc Codec that normalizes lowercase method names.
package json
import (
"errors"
"fmt"
"net/http"
"strings"
"unicode"
"unicode/utf8"
"github.com/gorilla/rpc/v2"
"github.com/gorilla/rpc/v2/json2"
)
// Null is the string representation of the JSON null literal.
const Null = "null"
var (
errUppercaseMethod = errors.New("method must start with a non-uppercase letter")
errInvalidArg = errors.New("couldn't unmarshal an argument. Ensure arguments are valid and properly formatted. See documentation for example calls")
)
// NewCodec returns a new json codec that requires the first character of the
// method name to be lowercase and upper-cases it for dispatch.
func NewCodec() rpc.Codec {
return lowercase{json2.NewCodec()}
}
type lowercase struct{ *json2.Codec }
func (lc lowercase) NewRequest(r *http.Request) rpc.CodecRequest {
return &request{lc.Codec.NewRequest(r).(*json2.CodecRequest)}
}
type request struct{ *json2.CodecRequest }
func (r *request) Method() (string, error) {
method, err := r.CodecRequest.Method()
methodSections := strings.SplitN(method, ".", 2)
if len(methodSections) != 2 || err != nil {
return method, err
}
class, function := methodSections[0], methodSections[1]
firstRune, runeLen := utf8.DecodeRuneInString(function)
if firstRune == utf8.RuneError {
return method, nil
}
if unicode.IsUpper(firstRune) {
return method, errUppercaseMethod
}
uppercaseRune := string(unicode.ToUpper(firstRune))
return fmt.Sprintf("%s.%s%s", class, uppercaseRune, function[runeLen:]), nil
}
func (r *request) ReadRequest(args any) error {
if err := r.CodecRequest.ReadRequest(args); err != nil {
return errInvalidArg
}
return nil
}