Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions _examples/golang-basics/example.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion _examples/golang-basics/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ require (
github.com/webrpc/gen-kotlin v0.1.0 // indirect
github.com/webrpc/gen-openapi v0.16.3 // indirect
github.com/webrpc/gen-typescript v0.20.2 // indirect
github.com/webrpc/webrpc v0.29.0 // indirect
github.com/webrpc/webrpc v0.29.1-0.20251023011239-8943a04291e3 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
golang.org/x/arch v0.1.0 // indirect
golang.org/x/crypto v0.41.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions _examples/golang-basics/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ github.com/webrpc/gen-openapi v0.16.3 h1:Gwj1Wxdqu+jLsVd+JeUuvf92JyLTZ/nMNcSwVpd
github.com/webrpc/gen-openapi v0.16.3/go.mod h1:fwY3ylZmdiCr+WXjR8Ek8wm08CFRr2/GaXI7Zd/Ou4Y=
github.com/webrpc/gen-typescript v0.20.2 h1:FUoraPOQWLL70gSQGYFkuelxUhVSqI7LmmA45CaAbDE=
github.com/webrpc/gen-typescript v0.20.2/go.mod h1:XDW2lL0Ft7Mdf12wcj8STdvuV24k2L/vGY49Ag2M4Lw=
github.com/webrpc/webrpc v0.29.0 h1:BEbiVt9+D8fuO+pazDATxAI/Xv8DPU+gATGlVOQufpg=
github.com/webrpc/webrpc v0.29.0/go.mod h1:CbakUGryLYf/xqLP59wjkP4TleMOjyCNlPJYmVzIhUY=
github.com/webrpc/webrpc v0.29.1-0.20251023011239-8943a04291e3 h1:PP0EBVMFHGvd6E2REaoHI98otgVbPInT6QtMjZ/BiKc=
github.com/webrpc/webrpc v0.29.1-0.20251023011239-8943a04291e3/go.mod h1:CbakUGryLYf/xqLP59wjkP4TleMOjyCNlPJYmVzIhUY=
github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM=
github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
7 changes: 5 additions & 2 deletions _examples/golang-imports/api.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion _examples/golang-imports/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ require (
github.com/webrpc/gen-kotlin v0.1.0 // indirect
github.com/webrpc/gen-openapi v0.16.3 // indirect
github.com/webrpc/gen-typescript v0.20.2 // indirect
github.com/webrpc/webrpc v0.29.0 // indirect
github.com/webrpc/webrpc v0.29.1-0.20251023011239-8943a04291e3 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
golang.org/x/crypto v0.41.0 // indirect
golang.org/x/mod v0.27.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions _examples/golang-imports/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ github.com/webrpc/gen-openapi v0.16.3 h1:Gwj1Wxdqu+jLsVd+JeUuvf92JyLTZ/nMNcSwVpd
github.com/webrpc/gen-openapi v0.16.3/go.mod h1:fwY3ylZmdiCr+WXjR8Ek8wm08CFRr2/GaXI7Zd/Ou4Y=
github.com/webrpc/gen-typescript v0.20.2 h1:FUoraPOQWLL70gSQGYFkuelxUhVSqI7LmmA45CaAbDE=
github.com/webrpc/gen-typescript v0.20.2/go.mod h1:XDW2lL0Ft7Mdf12wcj8STdvuV24k2L/vGY49Ag2M4Lw=
github.com/webrpc/webrpc v0.29.0 h1:BEbiVt9+D8fuO+pazDATxAI/Xv8DPU+gATGlVOQufpg=
github.com/webrpc/webrpc v0.29.0/go.mod h1:CbakUGryLYf/xqLP59wjkP4TleMOjyCNlPJYmVzIhUY=
github.com/webrpc/webrpc v0.29.1-0.20251023011239-8943a04291e3 h1:PP0EBVMFHGvd6E2REaoHI98otgVbPInT6QtMjZ/BiKc=
github.com/webrpc/webrpc v0.29.1-0.20251023011239-8943a04291e3/go.mod h1:CbakUGryLYf/xqLP59wjkP4TleMOjyCNlPJYmVzIhUY=
github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM=
github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
131 changes: 131 additions & 0 deletions bigintHelpers.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
{{define "bigintHelpers"}}
{{- $services := .Services -}}
{{- $opts := .Opts -}}

//
// BigInt helpers
//

// BigInt is an alias of big.Int with custom JSON (decimal string) encoding.
type BigInt big.Int

func NewBigInt(v int64) BigInt { var bi big.Int; bi.SetInt64(v); return BigInt(bi) }

// AsInt exposes the underlying *big.Int.
func (b *BigInt) AsInt() *big.Int { return (*big.Int)(b) }

// String returns the decimal string representation of the BigInt.
func (b BigInt) String() string { return b.AsInt().String() }

// MarshalText implements encoding.TextMarshaler.
func (b BigInt) MarshalText() ([]byte, error) {
return []byte(fmt.Sprintf("\"%s\"", b.String())), nil
}

// UnmarshalText implements encoding.TextUnmarshaler.
func (b *BigInt) UnmarshalText(text []byte) error {
t := string(text)
if len(text) <= 2 || t == "null" || t == "" {
return nil
}
i, ok := big.NewInt(0).SetString(string(text[1:len(text)-1]), 10)
if !ok {
return fmt.Errorf("BigInt.UnmarshalText: failed to unmarshal %q", text)
}
*b = BigInt(*i)
return nil
}

// MarshalJSON implements json.Marshaler
func (b BigInt) MarshalJSON() ([]byte, error) {
return b.MarshalText()
}

// UnmarshalJSON implements json.Unmarshaler
func (b *BigInt) UnmarshalJSON(text []byte) error {
if string(text) == "null" {
return nil
}
return b.UnmarshalText(text)
}

// MarshalBinary implements encoding.BinaryMarshaler. The first byte is the sign byte
// to represent positive or negative numbers.
func (b BigInt) MarshalBinary() ([]byte, error) {
bytes := b.AsInt().Bytes()
out := make([]byte, len(bytes)+1)
copy(out[1:], bytes)
if b.AsInt().Sign() < 0 {
// Prepend a sign byte (0xFF for negative)
out[0] = 0xFF
} else {
// For zero or positive numbers, prepend 0x00
out[0] = 0x00
}
return out, nil
}

// UnmarshalBinary implements encoding.BinaryUnmarshaler. The first byte is the sign byte
// to represent positive or negative numbers.
func (b *BigInt) UnmarshalBinary(buff []byte) error {
if len(buff) == 0 {
*b = BigInt(*big.NewInt(0))
return nil
}
// Extract the sign byte
signByte := buff[0]
i := new(big.Int)
if len(buff) > 1 {
i.SetBytes(buff[1:])
}
// Apply sign if negative
if signByte == 0xFF {
i.Neg(i)
}
*b = BigInt(*i)
return nil
}

func (b BigInt) Value() (driver.Value, error) {
return b.String(), nil
}

func (b *BigInt) Scan(src interface{}) error {
if src == nil {
return nil
}

var svalue string
switch v := src.(type) {
case string:
svalue = v
case []byte:
svalue = string(v)
default:
return fmt.Errorf("BigInt.Scan: unexpected type %T", src)
}

// pgx driver returns NeX where N is digits and X is exponent
parts := strings.SplitN(svalue, "e", 2)

var ok bool
i := &big.Int{}
i, ok = i.SetString(parts[0], 10)
if !ok {
return fmt.Errorf("BigInt.Scan: failed to scan value %q", svalue)
}

if len(parts) >= 2 {
exp := big.NewInt(0)
exp, ok = exp.SetString(parts[1], 10)
if !ok {
return fmt.Errorf("BigInt.Scan failed to scan exp component %q", svalue)
}
i = i.Mul(i, big.NewInt(1).Exp(big.NewInt(10), exp, nil))
}

*b = BigInt(*i)
return nil
}

{{end -}}
3 changes: 3 additions & 0 deletions helpers.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ func MethodCtx(ctx context.Context) (method, bool) {
return m, true
}

// PtrTo is a useful helper when constructing values for optional fields.
func PtrTo[T any](v T) *T { return &v }

{{ if $opts.server}}
func ResponseWriterFromContext(ctx context.Context) http.ResponseWriter {
w, _ := ctx.Value(HTTPResponseWriterCtxKey).(http.ResponseWriter)
Expand Down
7 changes: 7 additions & 0 deletions main.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
{{- set $typeMap "uint16" "uint16" -}}
{{- set $typeMap "uint32" "uint32" -}}
{{- set $typeMap "uint64" "uint64" -}}
{{- set $typeMap "bigint" "BigInt" -}}
{{- set $typeMap "int" "int" -}}
{{- set $typeMap "int8" "int8" -}}
{{- set $typeMap "int16" "int16" -}}
Expand All @@ -58,6 +59,8 @@
{{- set $typeMap "string" "string" -}}
{{- set $typeMap "timestamp" "time.Time" -}}

{{- $usesBigInts := SchemaBigIntFieldsByType .WebRPCSchema -}}

{{/* INIT */}}
{{- $typePrefix := (last (split "/" $opts.importTypesFrom)) -}}
{{- if ne $typePrefix "" -}}
Expand Down Expand Up @@ -149,6 +152,10 @@ func WebRPCSchemaHash() string {

{{ template "helpers" dict "Opts" $opts }}

{{- if $usesBigInts}}
{{ template "bigintHelpers" dict "Schema" .WebRPCSchema "Services" .Services "Opts" $opts }}
{{ end -}}

{{/* Errors */}}
{{ template "errors" dict "WebrpcErrors" .WebrpcErrors "SchemaErrors" .Errors "Opts" $opts "TypePrefix" $typePrefix }}

Expand Down