Closed IngwiePhoenix closed 1 year ago
RpcMsg<MiniTuple<string, T>>
This nested generic structure definition is not currently supported.
I see... So I have to do that differently. Alright, I'll see what comes to mind.
Thanks!
That said, might want to introduce an error for this situation, perhaps?
Can you include your main.v
so this can be tested?
The following compiles successfully with the new generic syntax and the latest commit:
module rpc
import rand
// Module: messages.rpc
struct MiniTuple[A, B] {
left A
right B
}
fn (t MiniTuple[A, B]) string() string {
return '[' + t.left + ', ' + t.right + ']'
}
struct RpcMsg[T] {
id string
method string
params T
}
// Generators
fn handle_simple(method string) RpcMsg[[]string] {
return RpcMsg[[]string]{
id: rand.uuid_v4()
method: method
params: []
}
}
fn handle_auth[T](method string, data T) RpcMsg[T] {
return RpcMsg[T]{
id: rand.uuid_v4()
method: method
params: data
}
}
fn handle_data1(method string, target string) RpcMsg[string] {
return RpcMsg[string]{
id: rand.uuid_v4()
method: method
params: target
}
}
fn handle_data2[T](method string, target string, data T) RpcMsg[MiniTuple[string, T]] {
return RpcMsg[ MiniTuple[string, T] ]{
id: rand.uuid_v4()
method: method
params: MiniTuple[string, T]{target, data}
}
}
// Methods
// via: https://github.com/orgs/surrealdb/discussions/104#discussioncomment-3675580
pub fn ping() RpcMsg[[]string] {
return handle_simple('ping')
}
pub fn info() RpcMsg[[]string] {
return handle_simple('info')
}
pub fn use(ns string, db string) RpcMsg[[]string] {
return RpcMsg[[]string]{
id: rand.uuid_v4()
method: 'use'
params: [ns, db]
}
}
pub fn signin[T](data T) RpcMsg[T] {
return handle_auth[T]('signin', data)
}
pub fn signup[T](data T) RpcMsg[T] {
return handle_auth[T]('signup', data)
}
pub fn authenticate(jwt string) RpcMsg[string] {
return handle_data1('authenticate', jwt)
}
pub fn invalidate() RpcMsg[[]string] {
return handle_simple('invalidate')
}
pub fn kill(uuid string) RpcMsg[string] {
return handle_data1('kill', uuid)
}
pub fn live(table string) RpcMsg[string] {
return handle_data1('live', table)
}
pub fn let[T](key string, value T) RpcMsg[MiniTuple[string, T]] {
return handle_data2[T]('let', key, value)
}
pub fn set[T](key string, value T) RpcMsg[MiniTuple[string, T]] {
return let[T](key, value)
}
pub fn query[T](sql string, kvs map[string]T) RpcMsg[MiniTuple[string, map[string]T]] {
return handle_data2('query', sql, kvs)
}
pub fn do_select(target string) RpcMsg[string] {
return handle_data1('select', target)
}
pub fn create[T](target string, data T) RpcMsg[MiniTuple[string, T]] {
return handle_data2[T]('create', target, data)
}
pub fn update[T](target string, data T) RpcMsg[MiniTuple[string, T]] {
return handle_data2[T]('update', target, data)
}
pub fn change[T](target string, data T) RpcMsg[MiniTuple[string, T]] {
return handle_data2[T]('change', target, data)
}
pub fn modify[T](target string, data T) RpcMsg[MiniTuple[string, T]] {
return handle_data2[T]('modify', target, data)
}
Hello!
So, first my info:
What did you do? I tried to compile this module based on the SurrealDB RPC. To test it, I wrote a super simple hello world program that just includes the module, so that I could see if it even compiles.
rpc.v
:What did you expect to see? Only the warning about the unused module, nothing more.
What did you see instead?
My bet is right here:
This seems like it's part of the translation of
MiniTuple<A, B>
from the invocationRpcMsg<MiniTuple<string, T>>{ ... }
.So I am not sure if this goes to cgen, or checker - so I listed both because it is probably somewhere in that area and I don't know how to work around that.
The idea of
RpcMsg
was to have.params
be dynamic in most cases. It will be JSON encoded and sent via WebSockets, so this field needs a little more dynamicness. My idea was to use a generic here to speed this up a little instead of having to write multiple structs with their own.string()
to sit out pieces of JSON. This... might've backfired. ^^;Kind regards, Ingwie