Cosmos pathsยค
This module implements the external encoding for Cosmos paths. A path is a string with one of thses forms:
- "e_0/e_1/e_2..." where the e_i are path elements
- ".stream/{GUID}" where GUID is a stream ID
Internally, the path is represented by a struct.
module cosmos_path(number_t,pathElement_t,streamID_t) = {
instance simple_path : path(number_t,pathElement_t)
type kind_t = {stream_kind, path_kind}
type t = struct {
cp_kind : kind_t,
cp_path : simple_path.t,
cp_stream : streamID_t
}
<<< encode `t`
std::ostream &operator <<(std::ostream &s, const `t` &a) {
if (a.cp_kind == `stream_kind`) {
std::ostringstream guid;
guid << a.cp_stream; // this gets the GUID in quotes
int len = guid.str().size();
s << "\".streamid/{" << guid.str().substr(1,len-2).c_str() << "}\"";
} else
s << a.cp_path;
return s;
}
template <>
`t` _arg<`t`>(std::vector<ivy_value> &args, unsigned idx, int bound) {
ivy_value &arg = args[idx];
if (arg.fields.size())
throw out_of_bounds(idx);
`t` a;
int len = arg.atom.size();
if (arg.atom.substr(0,11) == ".streamid/{" && arg.atom.substr(len-1,1) == "}") {
a.cp_kind = `stream_kind`;
a.cp_stream = arg.atom.substr(11,len-12);
} else {
a.cp_kind = `path_kind`;
a.cp_path = _arg<`simple_path.t`>(args,idx,bound);
}
return a;
}
template <>
void __ser<`t`>(ivy_ser &res, const `t` &t){
std::ostringstream sts;
sts << t;
std::string foo = sts.str().substr(1,sts.str().size()-2);
res.set(foo);
}
template <>
void __deser<`t`>(ivy_deser &inp, `t` &res){
std::vector<ivy_value> args;
args.resize(1);
inp.get(args[0].atom);
res = _arg<`t`>(args,0,0);
}
>>>
}