types

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 18, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package types maps PostgreSQL type OIDs to JSON-emitting encoder functions. Encoders take the raw text-format bytes from a DataRow cell and append the JSON form to the destination buffer.

The contract:

  • raw == nil means SQL NULL; the encoder writes the JSON literal null.
  • the returned slice is dst with the cell appended. No allocations for the common cases (numbers, bools, json passthrough).
  • encoders never retain raw beyond their own call.

Index

Constants

This section is empty.

Variables

View Source
var EncodeNumericBinary = encodeNumericBinary

EncodeNumericBinary is the exported alias for encodeNumericBinary. The default PickBinary dispatch does NOT pick it — on loopback the text path is faster for typical precisions. Callers that want binary numeric (wide precision on a high-RTT link) can build a custom Encoder table referencing this symbol directly.

Functions

func EncodeBool

func EncodeBool(dst, raw []byte) []byte

EncodeBool: text protocol returns "t" or "f".

func EncodeBytea

func EncodeBytea(dst, raw []byte) []byte

EncodeBytea handles both bytea_output forms:

  • "hex" (default since 9.0): \xDEADBEEF
  • "escape" (legacy): octal-escaped ASCII

In hex form, the only character that needs JSON escaping is the leading backslash; we write `"\\x...."` directly. In escape form we fall through to the generic string escaper.

func EncodeFloat

func EncodeFloat(dst, raw []byte) []byte

EncodeFloat handles float4/float8. Postgres can emit "NaN", "Infinity", "-Infinity" — none of which are valid JSON numbers, so we string-quote them. Everything else is a valid JSON number and passes through.

func EncodeInt

func EncodeInt(dst, raw []byte) []byte

EncodeInt validates and emits an integer as a raw JSON number. The validation is cheap (a single pass) and lets us drop the bytes in without strconv.

func EncodeJSONPassthrough

func EncodeJSONPassthrough(dst, raw []byte) []byte

EncodeJSONPassthrough drops json/jsonb's text representation directly into the output. PostgreSQL guarantees the text form is valid JSON (jsonb is canonicalised; json is validated at insert time). We trust that contract, which is the whole point of this fast path.

func EncodeNumeric

func EncodeNumeric(dst, raw []byte) []byte

EncodeNumeric: same shape as float, but Postgres' numeric never uses exponential notation by default and "NaN" is the only special form.

func EncodeOpaqueBinary

func EncodeOpaqueBinary(dst, raw []byte) []byte

EncodeOpaqueBinary emits the raw binary bytes as a hex-encoded JSON string with a "\\x" prefix, matching PG's own bytea text form. Used as a safe JSON-output fallback when the caller requested binary format for an OID that has no specialised decoder (typical for user-declared composite types in Config.BinaryOIDs). Human-readable enough to debug, always valid JSON.

func EncodeQuotedASCII

func EncodeQuotedASCII(dst, raw []byte) []byte

EncodeQuotedASCII is a fast path for values we know are pure ASCII with no characters that need escaping (timestamps, dates, intervals as the server formats them). We still validate cheaply.

func EncodeString

func EncodeString(dst, raw []byte) []byte

EncodeString quotes and escapes the raw bytes as a JSON string.

func EncodeUUID

func EncodeUUID(dst, raw []byte) []byte

EncodeUUID writes a quoted UUID. UUID text form is exactly 36 ASCII characters (8-4-4-4-12 hex with dashes). We do a length check for safety but skip per-byte validation: if the server gave us 36 ASCII chars labelled "uuid" we trust them.

func HasBinary

func HasBinary(oid protocol.OID) bool

HasBinary reports whether PickBinary returns a non-nil specialised encoder for oid (i.e. asking for binary is worth it).

func HasBinaryEx

func HasBinaryEx(oid protocol.OID, binaryNumeric bool) bool

HasBinaryEx is HasBinary with the BinaryNumeric toggle.

Types

type Encoder

type Encoder func(dst, raw []byte) []byte

func Pick

func Pick(oid protocol.OID) Encoder

Pick returns the text-format encoder for the given OID. Anything we do not specifically know about is rendered as a JSON string — that is always correct for the text protocol because the server has already produced a textual rendering.

func PickBinary

func PickBinary(oid protocol.OID) Encoder

PickBinary returns a binary-format encoder for the given OID, or nil if we have no specialised binary path for it (callers should fall back to requesting text format for that column). NUMERIC is excluded here — see PickBinaryEx.

func PickBinaryEx

func PickBinaryEx(oid protocol.OID, binaryNumeric bool) Encoder

PickBinaryEx is PickBinary with the BinaryNumeric toggle. When binaryNumeric is true, OIDNumeric returns the binary decoder that parses PG's base-10000 wire format. The default (false) keeps the text decoder — faster on loopback and typical precisions.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL