flabk/pkg/asld/asld.go

106 lines
2.1 KiB
Go

// Package asld handles JSON-LD for asflab
//
// This will not go well
package asld
import (
"bytes"
"errors"
)
const (
tagName = "asld"
omitEmpty = "omitempty"
collapsible = "collapsible"
nullToken = "null"
)
var (
ErrNoMatching = errors.New("could not find matching")
ErrSyntaxError = errors.New("syntax error")
ErrEntryWithoutValue = errors.New("entry without value")
ErrMapNotStringIndexed = errors.New("map is not string indexed")
)
// assigned by init
var (
stoppableByByte map[byte]SymbolInfo
stoppableRaw []byte
iriPrefixes [][]byte
openSymbols []SymbolInfo
openRaw []byte
null []byte
)
func init() {
stoppableByByte = map[byte]SymbolInfo{}
for index, sym := range stoppable {
stoppable[index].enum = symbol(index)
stoppableByByte[sym.self] = sym
}
stoppableRaw = make([]byte, len(stoppable))
for index, symbol := range stoppable {
stoppableRaw[index] = symbol.self
}
iriPrefixesStrings := []string{
// Currently only doing https
"https://",
}
iriPrefixes = make([][]byte, len(iriPrefixesStrings))
for index, prefix := range iriPrefixesStrings {
iriPrefixes[index] = []byte(prefix)
}
openSymbolEnums := []symbol{
symbolOpenParen, symbolOpenArray, symbolString,
}
openSymbols = make([]SymbolInfo, len(openSymbolEnums))
for index, enum := range openSymbolEnums {
openSymbols[index] = stoppable[enum]
}
openRaw = _map(openSymbols, func(s SymbolInfo) byte {
return s.self
})
null = []byte(nullToken)
}
type SymbolInfo struct {
self byte
closer byte
enum symbol
}
func in[T comparable](this []T, has T) bool {
for _, elem := range this {
if elem == has {
return true
}
}
return false
}
func firstIn[T comparable](this []T, has T) (T, bool) {
for _, elem := range this {
if elem == has {
return elem, true
}
}
return has, false
}
func _map[T any, V any](v []T, f func(T) V) []V {
output := make([]V, len(v))
for index, elem := range v {
output[index] = f(elem)
}
return output
}
func isIRI(v []byte) bool {
for _, prefix := range iriPrefixes {
if bytes.Equal(v, prefix) {
return true
}
}
return false
}