shtypes

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package shtypes container wrappers for handling JSON types that may or may not be weird e.g.: numbers as strings or numbers, bools as anything that looks truthy it adds a little more flexibility around some of the types than the json.Number and related classes

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BoolToInt

func BoolToInt(b bool) int

BoolToInt simple formatter for mapping a bool to a 1 or a zero.

func IsEmptyMapShape added in v0.7.1

func IsEmptyMapShape(raw []byte) bool

IsEmptyMapShape reports whether a raw JSON payload is one of the shapes PHP produces for "no rows": absent, null, or an empty array.

PHP's json_encode emits `[]` for an empty associative array rather than `{}`, because it cannot tell the difference between an empty map and an empty list. So an endpoint that normally returns a keyed object returns `[]` when there is nothing to return, and unmarshalling into a map or struct fails on a response that is entirely valid.

Callers use it to short-circuit before decoding:

if shtypes.IsEmptyMapShape(raw) {
    return nil // no rows, not an error
}

Lives here rather than in a single endpoint package because it is a property of the serialiser, not of any one response — it can appear wherever a map-shaped return can be empty.

Types

type MaybeBigInt

type MaybeBigInt int64

MaybeBigInt is a simple wrapper unmarshalling json responses that could be either an int or an int in a string.

func (*MaybeBigInt) UnmarshalJSON

func (fi *MaybeBigInt) UnmarshalJSON(b []byte) error

UnmarshalJSON is a helper interface for dealing with numbers that may be returned in json responses as either a string with a number in it or numbers.

type MaybeBool

type MaybeBool bool

MaybeBool is a simple wrapper unmarshalling json responses that could be either an int or an int in a string.

func (*MaybeBool) UnmarshalJSON

func (fi *MaybeBool) UnmarshalJSON(b []byte) error

UnmarshalJSON is a helper interface for dealing with things that may or may not be a string representing a bool, or number representing a bool.

type MaybeString

type MaybeString string

MaybeString is for things that may or may not be strings, but should be represented as a string. The API surfaces the same id-like columns as strings in list views and as numbers in detail views (e.g. a list returns `"client_id": "1"` where the detail view returns `"client_id": 1`), so either form decodes into a Go string.

func (MaybeString) Int added in v0.7.1

func (fi MaybeString) Int() (int, error)

Int parses the value as an int.

func (MaybeString) String added in v0.7.1

func (fi MaybeString) String() string

String returns the underlying string value.

func (*MaybeString) UnmarshalJSON

func (fi *MaybeString) UnmarshalJSON(b []byte) error

UnmarshalJSON accepts a JSON string, a JSON number, or `null`. Anything else (bool, array, object) returns an error rather than a silently stringified value. String escape sequences are decoded by the stdlib, so the stored value is the unescaped Go string.

Jump to

Keyboard shortcuts

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