types

package
v0.0.0-...-8b90e50 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package types provides custom types with specialized marshaling and unmarshaling behavior for the Sefaria API client. These types handle various encoding concerns including:

  • Date formatting and parsing with flexible input handling
  • Boolean-to-integer conversion for URL parameters
  • Generic string-or-type parsing for API responses

The types in this package are designed to handle the quirks and inconsistencies found in the Sefaria API responses and parameter encoding requirements.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BoolInt

type BoolInt bool

BoolInt represents a boolean that encodes as "1" or "0" for URL parameters and other contexts where boolean values need to be represented as integers.

This type is particularly useful for APIs that expect boolean values to be encoded as integers (1 for true, 0 for false) rather than the standard "true"/"false" strings.

BoolInt implements several interfaces:

  • url.Encoder for URL parameter encoding
  • flag.Value for command-line flag parsing
  • encoding.TextMarshaler/TextUnmarshaler for text encoding

Example usage:

var b BoolInt = true
// When used in URL parameters, encodes as "1"
// When used in flags, accepts "1", "0", "true", "false"

func (BoolInt) EncodeValues

func (b BoolInt) EncodeValues(key string, v *url.Values) error

EncodeValues implements url.Encoder for BoolInt. It encodes true as "1" and false as "0" for URL parameters.

func (BoolInt) MarshalText

func (b BoolInt) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler for BoolInt. Returns "1" for true, "0" for false.

func (*BoolInt) Set

func (b *BoolInt) Set(s string) error

Set implements flag.Value for BoolInt. Accepts "1", "0", "true", or "false" as valid values.

func (*BoolInt) String

func (b *BoolInt) String() string

String returns the string representation of BoolInt. Returns "1" for true, "0" for false.

func (*BoolInt) UnmarshalText

func (b *BoolInt) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler for BoolInt. Accepts "1", "0", "true", or "false" as valid values.

type Date

type Date struct {
	time.Time
}

Date represents a date with custom JSON marshaling/unmarshaling behavior. It uses dateparse.ParseAny for flexible date parsing and formats output as "2006-01-02" (date only, no time component).

This type is designed to handle the various date formats that may be returned by the Sefaria API, providing a consistent interface for working with dates throughout the client.

Example usage:

var d Date
json.Unmarshal([]byte(`"2024-01-15"`), &d)
json.Unmarshal([]byte(`"January 15, 2024"`), &d)
json.Unmarshal([]byte(`"15/01/2024"`), &d)

func (Date) MarshalJSON

func (d Date) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler for Date. It formats the date as "2006-01-02" (date only, no time).

func (*Date) UnmarshalJSON

func (d *Date) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler for Date. It handles various date formats by using dateparse.ParseAny, which can parse many common date formats automatically. Empty strings and "null" values are treated as zero time.

type StringOr

type StringOr[T any] struct {
	Value T
}

StringOr is a generic type that can unmarshal JSON as either a string representation of type T or as the actual type T directly.

This type is useful for handling APIs that inconsistently return values as either strings or their native types. For example, an API might return a boolean as "true" in one response and as true in another.

Supported types for T:

  • int: parses string numbers to integers
  • bool: parses "true"/"false" strings to booleans
  • float64: parses string numbers to floats
  • string: passes through string values

Example usage:

var priority StringOr[float32]
json.Unmarshal([]byte(`"3.14"`), &priority)  // Works
json.Unmarshal([]byte(`3.14`), &priority)    // Also works

var enabled StringOr[bool]
json.Unmarshal([]byte(`"true"`), &enabled)   // Works
json.Unmarshal([]byte(`true`), &enabled)     // Also works

func (StringOr[T]) MarshalJSON

func (s StringOr[T]) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler for StringOr. It marshals the underlying Value directly.

func (*StringOr[T]) UnmarshalJSON

func (s *StringOr[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler for StringOr. It first attempts to unmarshal the JSON as a string, then converts that string to type T. If that fails, it attempts to unmarshal directly as type T.

Empty strings and "null" values are treated as the zero value of T.

Jump to

Keyboard shortcuts

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