Skip to main content

GValue

The central type representing any GLYPH value.
type GValue struct {
    // Private fields
}

Type System

GType Enumeration

type GType uint8

const (
    TypeNull   GType = iota
    TypeBool
    TypeInt
    TypeFloat
    TypeStr
    TypeBytes
    TypeTime
    TypeID
    TypeList
    TypeMap
    TypeStruct
    TypeSum
)

Constructors

Create GLYPH values using type-specific constructors:

Scalar Constructors

Null
func() *GValue
Creates a null value.
v := glyph.Null()
Bool
func(v bool) *GValue
Creates a boolean value.
v := glyph.Bool(true)
Int
func(v int64) *GValue
Creates an integer value.
v := glyph.Int(42)
Float
func(v float64) *GValue
Creates a float value.
v := glyph.Float(3.14)
Str
func(v string) *GValue
Creates a string value.
v := glyph.Str("hello")
Bytes
func(v []byte) *GValue
Creates a bytes value.
v := glyph.Bytes([]byte{0x01, 0x02, 0x03})
Time
func(v time.Time) *GValue
Creates a time value.
v := glyph.Time(time.Now())
ID
func(prefix, value string) *GValue
Creates a reference ID value.
v := glyph.ID("m", "2024-01-15:ARS-LIV")
// Emits as: ^m:2024-01-15:ARS-LIV

Container Constructors

List
func(values ...*GValue) *GValue
Creates a list value.
v := glyph.List(
    glyph.Int(1),
    glyph.Int(2),
    glyph.Int(3),
)
// Emits as: [1 2 3]
Map
func(entries ...MapEntry) *GValue
Creates a map value.
v := glyph.Map(
    glyph.FieldVal("name", glyph.Str("Arsenal")),
    glyph.FieldVal("score", glyph.Int(2)),
)
// Emits as: {name=Arsenal score=2}
Struct
func(typeName string, fields ...MapEntry) *GValue
Creates a typed struct value.
v := glyph.Struct("Team",
    glyph.FieldVal("id", glyph.ID("t", "ARS")),
    glyph.FieldVal("name", glyph.Str("Arsenal")),
)
// Emits as: Team{id=^t:ARS name=Arsenal}
Sum
func(tag string, value *GValue) *GValue
Creates a tagged union value.
v := glyph.Sum("Success", glyph.Int(42))
// Emits as: Success(42)

Helper Functions

FieldVal
func(key string, value *GValue) MapEntry
Creates a map entry for use in Map/Struct constructors.
entry := glyph.FieldVal("name", glyph.Str("Arsenal"))

Accessors

Extract typed values from GValue instances:

Type Checking

Type
func(v *GValue) GType
Returns the value’s type.
if v.Type() == glyph.TypeStruct {
    // Handle struct
}
IsNull
func(v *GValue) bool
Returns true if value is null.
if v.IsNull() {
    return nil
}

Scalar Accessors

AsBool
func(v *GValue) (bool, error)
Extracts boolean value or returns error.
b, err := v.AsBool()
if err != nil {
    return err
}
AsInt
func(v *GValue) (int64, error)
Extracts integer value or returns error.
n, err := v.AsInt()
if err != nil {
    return err
}
AsFloat
func(v *GValue) (float64, error)
Extracts float value or returns error.
f, err := v.AsFloat()
if err != nil {
    return err
}
AsStr
func(v *GValue) (string, error)
Extracts string value or returns error.
s, err := v.AsStr()
if err != nil {
    return err
}
AsBytes
func(v *GValue) ([]byte, error)
Extracts bytes value or returns error.
b, err := v.AsBytes()
if err != nil {
    return err
}
AsTime
func(v *GValue) (time.Time, error)
Extracts time value or returns error.
t, err := v.AsTime()
if err != nil {
    return err
}
AsID
func(v *GValue) (RefID, error)
Extracts reference ID or returns error.
ref, err := v.AsID()
if err != nil {
    return err
}
fmt.Printf("Prefix: %s, Value: %s\n", ref.Prefix, ref.Value)

Container Accessors

AsList
func(v *GValue) ([]*GValue, error)
Extracts list elements or returns error.
list, err := v.AsList()
if err != nil {
    return err
}
for i, elem := range list {
    fmt.Printf("%d: %v\n", i, elem)
}
AsMap
func(v *GValue) ([]MapEntry, error)
Extracts map entries or returns error.
entries, err := v.AsMap()
if err != nil {
    return err
}
for _, entry := range entries {
    fmt.Printf("%s: %v\n", entry.Key, entry.Value)
}
AsStruct
func(v *GValue) (*StructValue, error)
Extracts struct value or returns error.
structVal, err := v.AsStruct()
if err != nil {
    return err
}
fmt.Printf("Type: %s\n", structVal.TypeName)
for _, field := range structVal.Fields {
    fmt.Printf("%s: %v\n", field.Key, field.Value)
}
AsSum
func(v *GValue) (*SumValue, error)
Extracts sum value or returns error.
sumVal, err := v.AsSum()
if err != nil {
    return err
}
fmt.Printf("Tag: %s, Value: %v\n", sumVal.Tag, sumVal.Value)

Field Access

Get
func(v *GValue, key string) *GValue
Returns field value by key from map or struct, or nil if not found.
name := team.Get("name")
if name != nil {
    str, _ := name.AsStr()
    fmt.Println(str)
}
Index
func(v *GValue, i int) (*GValue, error)
Returns i-th element of a list or returns error.
elem, err := list.Index(0)
if err != nil {
    return err
}
Len
func(v *GValue) int
Returns length of list, map, or struct.
count := list.Len()
fmt.Printf("List has %d elements\n", count)

Mutation

Set
func(v *GValue, key string, val *GValue)
Sets field value on a map or struct.
team.Set("score", glyph.Int(3))
Append
func(v *GValue, val *GValue)
Appends value to a list.
list.Append(glyph.Int(4))

Supporting Types

RefID

Represents a reference identifier.
type RefID struct {
    Prefix string // e.g., "m" for match, "t" for team
    Value  string // The actual ID value
}

func (r RefID) String() string // Returns "^prefix:value"

MapEntry

Represents a key-value pair.
type MapEntry struct {
    Key   string
    Value *GValue
}

StructValue

Represents a typed struct.
type StructValue struct {
    TypeName string
    Fields   []MapEntry
}

SumValue

Represents a tagged union.
type SumValue struct {
    Tag   string
    Value *GValue
}

Bridge Functions

Convert between GLYPH and other formats:

SJSON Conversion

ToSJSON
func(v *GValue) *sjson.Value
Converts GValue to SJSON value.
sjsonVal := glyph.ToSJSON(glyphVal)
FromSJSON
func(v *sjson.Value) *GValue
Converts SJSON value to GValue.
glyphVal := glyph.FromSJSON(sjsonVal)

JSON Conversion

ToJSON
func(v *GValue) ([]byte, error)
Converts GValue to JSON bytes.
jsonBytes, err := glyph.ToJSON(value)
if err != nil {
    return err
}
FromJSON
func(data []byte) (*GValue, error)
Parses JSON bytes into GValue.
value, err := glyph.FromJSON(jsonBytes)
if err != nil {
    return err
}

Go Native Conversion

ToAny
func(v *GValue) any
Converts GValue to Go any (interface) for JSON/Gen1 interop.
native := glyph.ToAny(value)
jsonBytes, _ := json.Marshal(native)
FromAny
func(v any) *GValue
Converts Go any (interface) to GValue.
var obj any
json.Unmarshal(jsonBytes, &obj)
value := glyph.FromAny(obj)

Binary Encoding

EncodeBinary
func(v *GValue) ([]byte, error)
Encodes GValue to SJSON binary format.
binary, err := glyph.EncodeBinary(value)
if err != nil {
    return err
}
DecodeBinary
func(data []byte) (*GValue, error)
Decodes SJSON binary format to GValue.
value, err := glyph.DecodeBinary(binary)
if err != nil {
    return err
}

Example: Building Complex Structures

// Create a match result
match := glyph.Struct("Match",
    glyph.FieldVal("id", glyph.ID("m", "2024-01-15:ARS-LIV")),
    glyph.FieldVal("kickoff", glyph.Time(time.Now())),
    glyph.FieldVal("home", glyph.Struct("Team",
        glyph.FieldVal("id", glyph.ID("t", "ARS")),
        glyph.FieldVal("name", glyph.Str("Arsenal")),
    )),
    glyph.FieldVal("away", glyph.Struct("Team",
        glyph.FieldVal("id", glyph.ID("t", "LIV")),
        glyph.FieldVal("name", glyph.Str("Liverpool")),
    )),
    glyph.FieldVal("xG", glyph.List(
        glyph.Float(1.72),
        glyph.Float(2.14),
    )),
    glyph.FieldVal("odds", glyph.List(
        glyph.Float(2.10),
        glyph.Float(3.40),
        glyph.Float(3.25),
    )),
)

// Access nested fields
homeTeam := match.Get("home")
if homeTeam != nil {
    teamStruct, _ := homeTeam.AsStruct()
    name := teamStruct.Fields[1].Value
    fmt.Println(name) // Arsenal
}

Build docs developers (and LLMs) love