1
This commit is contained in:
parent
d1e57513bb
commit
feec7f9c34
@ -1,113 +1,7 @@
|
||||
use std::rc::Rc;
|
||||
use std::rc::{Rc, Weak};
|
||||
use std::cell::RefCell;
|
||||
use protobuf::Message;
|
||||
use protobuf::descriptor::FieldDescriptorProto_Type;
|
||||
|
||||
use r9;
|
||||
|
||||
mod test;
|
||||
mod mt;
|
||||
mod metawrap;
|
||||
mod metamgr;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum MessagesUnion
|
||||
{
|
||||
MtwAliKeyConf(Rc::<metawrap::MtwAliKeyConf>)
|
||||
}
|
||||
|
||||
impl r9::ProtoMsg for MessagesUnion {
|
||||
|
||||
fn deserialize(&self,
|
||||
obj: &serde_json::Map<String, serde_json::Value>,
|
||||
id_key: &mut i64,
|
||||
name_key: &mut String) {
|
||||
if let MessagesUnion::MtwAliKeyConf(meta) = self {
|
||||
let pb = &mut meta.p.borrow_mut();
|
||||
let desc = pb.descriptor();
|
||||
let mut v = Vec::<u8>::new();
|
||||
{
|
||||
v.resize(1024 * 1000, 0);
|
||||
let mut os = ::protobuf::CodedOutputStream::bytes(&mut v);
|
||||
for field in desc.fields() {
|
||||
let field_desc = field.proto();
|
||||
match obj.get(field.name()) {
|
||||
Some(v) => {
|
||||
match field_desc.get_field_type() {
|
||||
FieldDescriptorProto_Type::TYPE_STRING => {
|
||||
os.write_string(field_desc.get_number() as u32, v.as_str().unwrap());
|
||||
},
|
||||
_ => {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
None => {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut slice: &[u8] = v.as_slice();
|
||||
let is = &mut ::protobuf::CodedInputStream::new(&mut slice);
|
||||
pb.merge_from(is);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type XValue = r9::XValue::<test::App>;
|
||||
type MetaMgr = r9::MetaMgr::<MessagesUnion>;
|
||||
|
||||
macro_rules! getMeta {
|
||||
($metamgr:expr, $ty:ident, $idx:expr) => {
|
||||
{
|
||||
let mut ret = Option::None;
|
||||
if let Some(v) = $metamgr.get_byid(0, $idx) {
|
||||
if let MessagesUnion::$ty(v1) = &(**v) {
|
||||
ret = Some(v1);
|
||||
}
|
||||
}
|
||||
ret
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct TestA {
|
||||
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut metamgr = MetaMgr::new();
|
||||
let p = metawrap::MtwAliKeyConf{
|
||||
p: RefCell::new(crate::mt::AliKeyConf::new())
|
||||
};
|
||||
let dummy = Rc::new(p);
|
||||
metamgr.register_meta(
|
||||
"/var/data/conf_test/analyseapi/ali_key.json".to_string(),
|
||||
0,
|
||||
"".to_string(),
|
||||
"".to_string(),
|
||||
MessagesUnion::MtwAliKeyConf(dummy)
|
||||
);
|
||||
metamgr.load();
|
||||
let b = getMeta!(metamgr, MtwAliKeyConf, 0);
|
||||
match b {
|
||||
Some(v) => {
|
||||
println!("{}", v.p.borrow().get_access_keyid());
|
||||
println!("{}", v.p.borrow().get_access_secret());
|
||||
}
|
||||
None => {
|
||||
|
||||
}
|
||||
}
|
||||
{
|
||||
let mut testA = TestA{};
|
||||
let mut a = RefCell::new(crate::mt::AliKeyConf::new());
|
||||
{
|
||||
let p = RefCell::new(crate::mt::AliKeyConf::new());
|
||||
a = p;
|
||||
let testB = TestA{};
|
||||
testA = testB;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,48 +0,0 @@
|
||||
use std::rc::Rc;
|
||||
use std::any::Any;
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub struct MetaClass {
|
||||
file_name: String,
|
||||
idx: i32,
|
||||
prim_key: String,
|
||||
raw_list: Vec<Box<dyn Any>>,
|
||||
id_hash: HashMap<i64, Box<dyn Any>>,
|
||||
name_hash: HashMap<String, Box<dyn Any>>,
|
||||
}
|
||||
|
||||
pub struct MetaMgr {
|
||||
meta_classes: Vec<Box<MetaClass>>
|
||||
}
|
||||
|
||||
impl MetaMgr {
|
||||
|
||||
pub fn register_meta_class(&mut self, file_name: String, idx: i32, prim_key: String, sec_key: String) {
|
||||
let cls = MetaClass{
|
||||
file_name: String::from("hello"),
|
||||
idx: 1,
|
||||
prim_key: String::from("key"),
|
||||
raw_list: Vec::new(),
|
||||
id_hash: HashMap::new(),
|
||||
name_hash: HashMap::new()
|
||||
};
|
||||
self.meta_classes.push(Box::new(cls));
|
||||
}
|
||||
|
||||
pub fn get_byid<T: 'static>(&mut self, idx: i32, id: i64) -> Option<&Rc<T>> {
|
||||
if (idx as usize) < self.meta_classes.len() {
|
||||
match self.meta_classes[idx as usize].id_hash.get(&id) {
|
||||
Some(v) => {
|
||||
if let Some(item) = v.downcast_ref::<Rc<T>>() {
|
||||
return Some(item)
|
||||
}
|
||||
}
|
||||
None => {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
return None;
|
||||
}
|
||||
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
use std::cell::RefCell;
|
||||
|
||||
pub struct MtwAliKeyConf {
|
||||
pub p: RefCell<crate::mt::AliKeyConf>
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
pub struct App {
|
||||
}
|
||||
|
||||
pub fn hello(val: &crate::XValue) -> i64 {
|
||||
return val.get_i64();
|
||||
}
|
2
third_party/r9
vendored
2
third_party/r9
vendored
@ -1 +1 @@
|
||||
Subproject commit 9f8a1d68897f7c86a606b3cfa70f5fd0612e26f7
|
||||
Subproject commit b3b93c650eea2a9564acd680f647ba19cd6c6a66
|
Loading…
x
Reference in New Issue
Block a user