1use crate::Field;
2use json::{object, JsonValue};
3
4pub struct Dict {
14 pub require: bool,
15 pub field: String,
16 pub mode: String,
17 pub title: String,
18 pub table: String,
19 pub fields: String,
20 pub api: String,
21 pub def: String,
22 pub show: bool,
23 pub describe: String,
24 pub multiple: bool,
25 pub multiple_count: i32,
26 pub example: JsonValue,
27}
28
29impl Dict {
30 pub fn new(
31 require: bool,
32 field: &str,
33 title: &str,
34 table: &str,
35 fields: &str,
36 api: &str,
37 ) -> Self {
38 Self {
39 require,
40 field: field.to_string(),
41 mode: "dict".to_string(),
42 title: title.to_string(),
43 table: table.to_string(),
44 api: api.to_string(),
45 show: true,
46 fields: fields.to_string(),
47 describe: "".to_string(),
48 def: "".to_string(),
49 multiple: false,
50 multiple_count: 1,
51 example: JsonValue::Null,
52 }
53 }
54 pub fn multiple(&mut self, count: i32) -> &mut Dict {
56 self.multiple = count > 1;
57 self.multiple_count = count;
58 self
59 }
60 pub fn default(&mut self, def: &str) -> &mut Dict {
62 self.def = def.to_string();
63 self
64 }
65}
66
67impl Field for Dict {
68 fn sql(&mut self, model: &str) -> String {
69 let not_null = if self.require { " not null" } else { "" };
70 let length = if self.multiple {
71 64 * self.multiple_count + self.multiple_count
72 } else {
73 64
74 };
75 match model {
76 "sqlite" => {
77 format!(
78 "{} varchar({}){} default '{}'",
79 self.field, length, not_null, self.def
80 )
81 }
82 "pgsql" => {
83 format!(
84 r#""{}" varchar({}){} default '{}'"#,
85 self.field, length, not_null, self.def
86 )
87 }
88 _ => {
89 let sql = format!(
90 "`{}` varchar({}){} default '{}'",
91 self.field, length, not_null, self.def
92 );
93 format!(
94 "{} comment '{}|{}|{}|{}|{}|{}'",
95 sql.clone(),
96 self.mode,
97 self.title,
98 self.require,
99 self.multiple,
100 self.multiple_count,
101 self.def
102 )
103 }
104 }
105 }
106 fn hide(&mut self) -> &mut Self {
107 self.show = false;
108 self
109 }
110 fn describe(&mut self, text: &str) -> &mut Self {
111 self.describe = text.to_string();
112 self
113 }
114
115 fn field(&mut self) -> JsonValue {
116 let mut field = object! {};
117 field
118 .insert("require", JsonValue::from(self.require))
119 .unwrap();
120 field
121 .insert("field", JsonValue::from(self.field.clone()))
122 .unwrap();
123 field
124 .insert("mode", JsonValue::from(self.mode.clone()))
125 .unwrap();
126 field
127 .insert("title", JsonValue::from(self.title.clone()))
128 .unwrap();
129 field
130 .insert("table", JsonValue::from(self.table.clone()))
131 .unwrap();
132 field
133 .insert("api", JsonValue::from(self.api.clone()))
134 .unwrap();
135 field
136 .insert("fields", JsonValue::from(self.fields.clone()))
137 .unwrap();
138 field
139 .insert("def", JsonValue::from(self.def.clone()))
140 .unwrap();
141 field
142 .insert("multiple", JsonValue::from(self.multiple))
143 .unwrap();
144 field
145 .insert("multiple_count", JsonValue::from(self.multiple_count))
146 .unwrap();
147
148 field.insert("show", JsonValue::from(self.show)).unwrap();
149 field
150 .insert("describe", JsonValue::from(self.describe.clone()))
151 .unwrap();
152 field.insert("example", self.example.clone()).unwrap();
153 field
154 }
155
156 fn swagger(&mut self) -> JsonValue {
157 object! {
158 "type": self.mode.clone(),
159 "example": self.example.clone(),
160 }
161 }
162
163 fn example(&mut self, data: JsonValue) -> &mut Self {
164 self.example = data.clone();
165 self
166 }
167}