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 ¶
EncodeValues implements url.Encoder for BoolInt. It encodes true as "1" and false as "0" for URL parameters.
func (BoolInt) MarshalText ¶
MarshalText implements encoding.TextMarshaler for BoolInt. Returns "1" for true, "0" for false.
func (*BoolInt) Set ¶
Set implements flag.Value for BoolInt. Accepts "1", "0", "true", or "false" as valid values.
func (*BoolInt) String ¶
String returns the string representation of BoolInt. Returns "1" for true, "0" for false.
func (*BoolInt) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler for BoolInt. Accepts "1", "0", "true", or "false" as valid values.
type Date ¶
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 ¶
MarshalJSON implements json.Marshaler for Date. It formats the date as "2006-01-02" (date only, no time).
func (*Date) UnmarshalJSON ¶
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 ¶
MarshalJSON implements json.Marshaler for StringOr. It marshals the underlying Value directly.
func (*StringOr[T]) UnmarshalJSON ¶
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.