diff options
| author | Wayne-Cole <77279425+Wacky404@users.noreply.github.com> | 2026-06-13 20:26:39 -0500 |
|---|---|---|
| committer | Wayne-Cole <77279425+Wacky404@users.noreply.github.com> | 2026-06-13 20:26:39 -0500 |
| commit | 1cfd8a143107ab4bc50092daa45407196df0b75d (patch) | |
| tree | c25097cdcfe39a875dad3eb483c59a95c5e8b1be /internal/lurchql.go | |
| parent | bb0d5fecf8d839efa0e89c33d310c5202c6f8919 (diff) | |
| download | lurchers-1cfd8a143107ab4bc50092daa45407196df0b75d.tar.xz lurchers-1cfd8a143107ab4bc50092daa45407196df0b75d.zip | |
update: dove deep into unix api and made the base orchestration system
Diffstat (limited to 'internal/lurchql.go')
| -rw-r--r-- | internal/lurchql.go | 157 |
1 files changed, 157 insertions, 0 deletions
diff --git a/internal/lurchql.go b/internal/lurchql.go new file mode 100644 index 0000000..b4fad40 --- /dev/null +++ b/internal/lurchql.go @@ -0,0 +1,157 @@ +package internal + +/* TODO: Keep any actual logic/compute out of this file */ + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "net" + "reflect" +) + +// {"method": "query","function": "get User","params": {"id": 1, "user": "wcole"}} + +type ( + LurchMsgBytes = []byte + LurchRespBytes = []byte + + lurchLogic = map[string][]any + lurchArgs = map[string]any + lurchCallback func(args map[string]any) error +) + +type LurchPackage interface { + Encode() []byte + Decode() error +} + +type LurchMsg struct { + Method string `json:"method"` + Function string `json:"function"` + Params lurchArgs `json:"params"` +} + +func (m *LurchMsg) Encode() ([]byte, error) { + return json.Marshal(&m) +} + +func (m *LurchMsg) Decode(data LurchMsgBytes) error { + return json.Unmarshal(data, m) +} + +// arb data; don't know the shape of api data response yet, tbd +// {"data": {"id": 1, "user": "wcole"}} +// {"method": "query","function": "get User","params": {"id": 1, "user": "wcole"}} + +type LurchResp struct { + Ok bool `json:"ok"` + Data map[string][]any `json:"data"` + Meta map[string]string `json:"meta"` +} + +func (r *LurchResp) Encode() ([]byte, error) { + return json.Marshal(&r) +} + +func (r *LurchResp) Decode(data LurchRespBytes) error { + return json.Unmarshal(data, r) +} + +func Encode(lp interface{}) ([]byte, error) { + switch v := lp.(type) { + case LurchMsg: + return json.Marshal(v) + case *LurchMsg: + return json.Marshal(*v) + case LurchResp: + return json.Marshal(v) + case *LurchResp: + return json.Marshal(*v) + default: + return nil, errors.New("encode: package value is nil") + } +} + +func Decode(lp interface{}, data []byte) error { + switch v := lp.(type) { + case LurchMsg: + return json.Unmarshal(data, &v) + case *LurchMsg: + return json.Unmarshal(data, v) + case LurchResp: + return json.Unmarshal(data, &v) + case *LurchResp: + return json.Unmarshal(data, v) + default: + return errors.New("decode: failed to determine type of package") + } +} + +// {"method": "query","function": "get User","params": {"id": 1, "user": "wcole"}} +var fns = lurchLogic{ + // using the base rep of the type + "spawnWorker": []any{spawnWorkerArgs, spawnWorker}, + "status": []any{statusArgs, status}, +} + +func Compute(ctx context.Context, m *LurchMsg, conn net.Conn) error { + d := ctx.Value("daemon") + if d != nil { + return errors.New("compute: daemon reference not found in context") + } + + a := func(args lurchArgs, pArgs lurchArgs) (lurchArgs, bool) { + buf := lurchArgs{} + for n, v := range args { + if primitive, ok := pArgs[n]; ok { + if reflect.TypeOf(v) != reflect.TypeOf(primitive) { + buf[n] = v + } + } + } + + if len(buf) != 0 { + return buf, false + } + + args["daemon"] = d + return args, true + } + f := func(fn string) error { + if signature, ok := fns[fn]; ok { + // function signature is in lurch functions; sign is value + if callee, ok := signature[1].(lurchCallback); ok { + // args validation + // COULD PANIC, DON'T GOOF UP THE FNS INF + if params, ok := a(m.Params, signature[0].(lurchArgs)); ok { + err := callee(params) + return err + } else { + return errors.New("parse: malformed lurch message - args") + } + } + } + + return errors.New("parse: signature not in lurch logic") + } + + switch m.Method { + case "query": + fmt.Fprintln(conn, "query received") + // some query system logic here + return f(m.Function) + case "mutation": + // mutation logic + fmt.Fprintln(conn, "mutation received") + return f(m.Function) + case "subscription": + // subscription logic + fmt.Fprintln(conn, "subscription received") + return f(m.Function) + default: + fmt.Fprintf(conn, "unknown command: %s\n", m.Method) + return errors.New("parse: malformed lurch message - method") + } +} |
