Compare commits

...

2 Commits

Author SHA1 Message Date
emilis 3004475000 tidy 2022-08-03 15:20:12 +01:00
emilis c96643250c wip chunker i gotta go faste bye 2022-08-03 15:20:03 +01:00
11 changed files with 349 additions and 13 deletions

View File

@ -1,12 +1,14 @@
package main
import "sectorinf.com/emilis/flabk/flabk/flabweb"
// MUST (client and server) application/ld+json; profile="https://www.w3.org/ns/activitystreams
// SHOULD application/activity+json
// public actor https://www.w3.org/ns/activitystreams#Public
func main() {
panic(asfhttp.New("my.site").Run())
panic(flabweb.New("my.site").Run())
}
// {

4
go.mod
View File

@ -1,11 +1,10 @@
module sectorinf.com/emilis/flabk
go 1.18
go 1.19
require (
github.com/gin-gonic/gin v1.8.1
github.com/go-ap/jsonld v0.0.0-20220615144122-1d862b15410d
github.com/piprate/json-gold v0.4.1
github.com/stretchr/testify v1.7.2
)
@ -23,7 +22,6 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
golang.org/x/net v0.0.0-20220630215102-69896b714898 // indirect

6
go.sum
View File

@ -43,19 +43,14 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/pelletier/go-toml/v2 v2.0.2 h1:+jQXlF3scKIcSEKkdHzXhCTDLPFi5r1wnK6yPS+49Gw=
github.com/pelletier/go-toml/v2 v2.0.2/go.mod h1:MovirKjgVRESsAvNZlAjtFwV867yGuwRkXbG66OzopI=
github.com/piprate/json-gold v0.4.1 h1:JYbYN36n6YcAYipKy3ttv3X2HDQPeqWqmwta35NPj04=
github.com/piprate/json-gold v0.4.1/go.mod h1:OK1z7UgtBZk06n2cDE2OSq1kffmjFFp5/2yhLLCz9UM=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35 h1:J9b7z+QKAmPf4YLrFg6oQUotqHQeUNWwkvo7jZp1GLU=
github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35/go.mod h1:prYjPmNq4d1NPVmpShWobRqXY3q7Vp+80DqgxxUrUIA=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
@ -91,7 +86,6 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -1 +0,0 @@
package asld

View File

@ -0,0 +1,5 @@
package consts
const (
PkgTag = "ld"
)

View File

@ -0,0 +1,223 @@
package chunk
import (
"errors"
"fmt"
"unicode"
"unicode/utf8"
"sectorinf.com/emilis/flabk/pkg/coll"
)
var (
ErrNoMatch = errors.New("no matching")
ErrMapMemberNotString = errors.New("map member not a string")
ErrUnexpectedSymbol = errors.New("unexpected symbol")
)
type Chunk struct {
vector coll.Vector[byte]
posLeft int
posRight int
// todo global pos impl
globLeft int
globRight int
}
// Match finds the matching closer to the symbol at the left position and sets the
// right position to this index
func (c Chunk) Match() (Chunk, error) {
s, ok := matchers[c.vector[c.posLeft]]
if !ok {
panic(fmt.Sprintf("Match called on %c with no matcher defined", c.vector[c.posLeft]))
}
if s.StartFromRight {
for c.posRight > 0 {
if c.vector[c.posRight] == s.MatchByte {
return c, nil
}
c.posRight--
c.globRight--
}
} else {
for index := c.posLeft + 1; index < len(c.vector); index++ {
if c.vector[index] == s.MatchByte {
c.posRight = index
c.globRight += (c.posLeft + 1) - index
return c, nil
}
}
}
return c, fmt.Errorf("%w %c", ErrNoMatch, c.vector[c.posLeft])
}
func (c Chunk) Copy() Chunk {
return c
}
func (c Chunk) Left() byte {
return c.vector[c.posLeft]
}
// Sub returns an inclusive subchunk of [left;right]
func (c Chunk) Sub() Chunk {
return New(c.vector[c.posLeft : c.posRight+1])
}
// CookieCutter returns a subchunk of (left;right)
func (c Chunk) CookieCutter() Chunk {
return New(c.vector[c.posLeft+1 : c.posRight])
}
func (c Chunk) AtSpace() bool {
r, _ := utf8.DecodeRune(c.vector[c.posLeft:])
return unicode.IsSpace(r)
}
func (c Chunk) Seek() Chunk {
for c.posLeft < len(c.vector) && c.AtSpace() {
c.posLeft++
}
return c
}
func (c Chunk) ValueEnd() (Chunk, error) {
switch c.vector[c.posLeft] {
case '"', '{', '[':
return c.Match()
default:
for index := c.posLeft; index <= c.posRight; index++ {
if c.vector[index] == ',' {
return New(c.vector[c.posLeft:index]), nil
}
}
return c, nil
}
}
// Skip skips the current left position and then Seeks
func (c Chunk) Skip() Chunk {
if c.posLeft+1 < len(c.vector) {
c.posLeft++
}
return c.Seek()
}
type MatchRule struct {
MatchByte byte
StartFromRight bool
}
var (
matchers = map[byte]MatchRule{
'{': {
MatchByte: '}',
StartFromRight: true,
},
'[': {
MatchByte: ']',
StartFromRight: true,
},
'"': {
MatchByte: '"',
},
}
)
func New(v []byte) Chunk {
posRight := len(v) - 1
if len(v) == 0 {
posRight = 0
}
return Chunk{
vector: coll.Vector[byte](v),
posLeft: 0,
posRight: posRight,
}
}
func (c Chunk) Child(left, right int) Chunk {
sub := c.vector[left:right]
return Chunk{
vector: sub,
posLeft: 0,
posRight: len(sub),
globLeft: (c.globLeft - c.posLeft) + left,
globRight: (c.globRight - c.posRight) + right,
}
}
type ParseFunc[T any] func(T) (T, error)
func Parse[T any](c Chunk) ParseFunc[T] {
switch c.vector[c.posLeft] {
case '{':
return ParseMap[T](c.CookieCutter())
default:
panic("not implemented")
}
// return can be:
// * {}
// * []
// * ""
// * 123
// * true
// * false
// * null
}
type Row struct {
Name string
Value Chunk
}
func (c Chunk) String() string {
return string(c.vector[c.posLeft : c.posRight+1])
}
func (c Chunk) Row() (Row, error) {
c = c.Seek()
if c.vector[c.posLeft] != '"' {
return Row{}, fmt.Errorf("%w: %c", ErrMapMemberNotString, c.vector[c.posLeft])
}
name, err := c.Match()
if err != nil {
return Row{}, fmt.Errorf("match: %w", err)
}
postName := c.Copy()
postName.posLeft = name.posRight
postName = postName.Skip()
// Next we must get a :
if postName.vector[postName.posLeft] != ':' {
return Row{}, fmt.Errorf("%w '%c', expected ':'", ErrUnexpectedSymbol, postName.vector[postName.posLeft])
}
value, err := postName.Skip().ValueEnd()
if err != nil {
return Row{}, fmt.Errorf("value: %w", err)
}
return Row{
Name: name.String(),
Value: value,
}, nil
}
func (c Chunk) After(v Chunk) Chunk {
c.posLeft = v.posLeft
return c
}
func ParseMap[T any](c Chunk) ParseFunc[T] {
return func(t T) (T, error) {
// mapper := parse.GetMap(t)
// for {
// row, err := c.Row()
// if err != nil {
// return t, err
// }
// }
panic("todo")
}
}

View File

@ -0,0 +1,37 @@
package chunk_test
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
"sectorinf.com/emilis/flabk/pkg/ld/internal/parse/chunk"
)
func TestSpace(t *testing.T) {
that := require.New(t)
ch := chunk.New([]byte(" hello world"))
that.True(ch.AtSpace())
}
func TestParseMap(t *testing.T) {
type Hello struct {
Hello string
}
ch := chunk.New([]byte(`"Hello": "world"`))
h, err := chunk.ParseMap[Hello](ch)(Hello{})
if err != nil {
panic(err)
}
fmt.Println(h)
}
func TestChild(t *testing.T) {
type Hello struct {
Hello string
}
ch := chunk.New([]byte(`"Hello": "world"`))
child := ch.Child(5, 10)
fmt.Println(child)
}

View File

@ -0,0 +1,73 @@
package parse
import (
"fmt"
"reflect"
"strconv"
"strings"
"sectorinf.com/emilis/flabk/pkg/ld/internal/consts"
)
type LazyMapFunc func(name string, value string)
func GetMap(v any) LazyMapFunc {
val := reflect.ValueOf(v)
// typ := reflect.TypeOf(v)
switch val.Kind() {
case reflect.Map:
return func(name string, value string) {
// val.SetMapIndex(reflect.ValueOf(name), value)
}
case reflect.Struct:
// fields := GetStructFields(val, typ)
return func(name, value string) {
// val, ok := fields[name]
// if ok {
// val.Set(value)
// }
}
default:
panic("wrong")
}
}
func ParseAsValue(v string, valType reflect.Type) (any, error) {
// Might not be necessary to trim
v = strings.TrimSpace(v)
switch valType.Kind() {
case reflect.String:
return v[1 : len(v)-1], nil
case reflect.Bool:
b, err := strconv.ParseBool(strings.ToLower(v))
if err != nil {
fmt.Errorf("boolean: %w", err)
}
return b, nil
case reflect.Struct, reflect.Map:
panic("todo")
default:
panic("todo")
}
}
func GetStructFields(val reflect.Value, typ reflect.Type) map[string]reflect.Value {
total := val.NumField()
out := map[string]reflect.Value{}
for index := 0; index < total; index++ {
cName := StructName(typ.Field(index))
if cName != "-" {
out[cName] = val.Field(index)
}
}
return out
}
func StructName(v reflect.StructField) string {
tag := v.Tag.Get(consts.PkgTag)
if tag != "" {
return tag
}
// Default to field name
return v.Name
}

View File

@ -1,4 +1,8 @@
// // Package asld handles JSON-LD for asflab
// //
// // This will not go well
package asld
package ld
const (
pkgTag = "ld"
)

1
pkg/ld/unmarshal.go Normal file
View File

@ -0,0 +1 @@
package ld

View File

@ -1,4 +1,4 @@
package asld_test
package ld_test
// import (
// "encoding/json"