Skip to content

data.encoderhero #

hero Encoder

//#!/usr/bin/env -S v -n -w -gc none  -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.data.encoderhero
import freeflowuniverse.herolib.core.base
import time

struct Person {
mut:
    name     string
    age      int = 20
    birthday time.Time
}

mut person := Person{
    name: 'Bob'
    birthday: time.now()
}
heroscript := encoderhero.encode[Person](person)!

println(heroscript)

person2 := encoderhero.decode[Person](heroscript)!

println(person2)

Constants #

const null = Null{}

fn decode #

fn decode[T](data string) !T

fn decode_array #

fn decode_array[T](_ []T, data string) ![]T

fn encode #

fn encode[T](val T) !string

encode is a generic function that encodes a type into a HEROSCRIPT string.

fn is_struct_array #

fn is_struct_array[U](_ []U) !bool

interface Encodable #

interface Encodable {
	heroscript() string
}

Decodable is an interface, that allows custom implementations for encoding structs to their string based JSON representations

enum ValueKind #

enum ValueKind {
	unknown
	array
	object
	string_
	number
}

ValueKind enumerates the kinds of possible values of the Any sumtype.

fn (ValueKind) str #

fn (k ValueKind) str() string

str returns the string representation of the specific ValueKind

struct Decoder #

struct Decoder[T] {
pub mut:
	object T
	data   string
}

struct Encoder #

struct Encoder {
pub mut:
	escape_unicode bool = true
	action_name    string
	action_names   []string
	params         paramsparser.Params
	children       []Encoder
	parent         ?&Encoder @[skip; str: skip]
}

Encoder encodes the an Any type into HEROSCRIPT representation. It provides parameters in order to change the end result.

fn (Encoder) export #

fn (e Encoder) export() !string

export exports an encoder into encoded heroscript

fn (Encoder) add_child #

fn (mut e Encoder) add_child[T](val T, parent string) !

needs to be a struct we are adding parent is the name of the action e.g define.customer:contact

fn (Encoder) add_child_list #

fn (mut e Encoder) add_child_list[U](val []U, parent string) !

fn (Encoder) add #

fn (mut e Encoder) add[T](val T) !

needs to be a struct we are adding parent is the name of the action e.g define.customer:contact

fn (Encoder) encode_array #

fn (mut e Encoder) encode_array[U](val []U) !

fn (Encoder) encode_struct #

fn (mut e Encoder) encode_struct[T](t T) !

now encode the struct

struct Null #

struct Null {
	is_null bool = true
}

Null struct is a simple representation of the null value in JSON.