1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use json::{JsonValue, object};

/// 模型数据库字段模型
pub struct ModelTable {
    pub version: String,
    pub table: String,
    pub title: String,
    pub primary_key: String,
    pub auto: bool,
    pub unique: Vec<String>,
    pub index: Vec<Vec<String>>,
    pub fields: JsonValue,
}

impl ModelTable {
    pub fn to_string(self) -> String {
        let data = object! {
            version:self.version,
            table:self.table,
            title:self.title,
            primary_key:self.primary_key,
            auto:self.auto,
            unique:self.unique,
            index:self.index,
            fields:self.fields
        };
        data.to_string()
    }
    pub fn parse(mut data: JsonValue) -> ModelTable {
        let mut unique = vec![];
        for item in 0..data["unique"].len() {
            let str = data["unique"][item].clone();
            unique.push(str.to_string());
        }
        let mut index = vec![];
        for item in data["index"].members_mut() {
            let mut row = vec![];
            for col in item.members_mut() {
                row.push(col.to_string());
            }
            if row.len() > 0 {
                index.push(row);
            }
        }
        Self {
            version: data["version"].to_string(),
            table: data["table"].to_string(),
            title: data["title"].to_string(),
            primary_key: data["primary_key"].to_string(),
            auto: data["auto"].as_bool().unwrap(),
            unique,
            index,
            fields: data["fields"].clone(),
        }
    }
}