Repository navigation
Expand file tree
/
Copy pathpython_type.rs
More file actions
129 lines (113 loc) · 4.38 KB
/
Copy pathpython_type.rs
File metadata and controls
129 lines (113 loc) · 4.38 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use std::{collections::BTreeSet, convert::Infallible};
#[cfg(test)]
use crate::mock;
use crate::{options::TypeOverrideOptions, proto::Column};
#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize, PartialOrd, Ord)]
pub struct PythonType {
pub declaration: Option<String>,
pub constructor: String,
#[serde(default)]
pub annotation: String,
#[serde(default)]
pub import: Option<String>,
#[serde(default)]
pub encode: Option<String>,
#[serde(default)]
pub decode: Option<String>,
}
macro_rules! new_type {
($name:literal) => {
new_type!($name, None)
};
($name:literal,$import:tt) => {{
let import: Option<&'static str> = Option::from($import);
crate::python_type::PythonType {
declaration: None,
constructor: $name.into(),
annotation: $name.into(),
import: import.map(String::from),
encode: None,
decode: None,
}
}};
}
impl PythonType {
pub fn from_pg_type(name: &str) -> Self {
match name {
"anyarray" | "anyenum" => new_type!("list[typing.Any]", "import typing"),
"anyrange" => new_type!("asyncpg.Range", "import asyncpg"),
"anymultirange" => new_type!("list[asyncpg.Range]", "import asyncpg"),
"record" => new_type!("asyncpg.Record", "import asyncpg"),
"bit" | "varbit" => new_type!("asyncpg.BitString", "import asyncpg"),
"bool" | "boolean" => new_type!("bool"),
"box" => new_type!("asyncpg.Box", "import asyncpg"),
"bytea" => new_type!("bytes"),
"macaddr" | "char" | "name" | "varchar" | "text" | "xml" => new_type!("str"),
"cidr" => new_type!(
"ipaddress.IPv4Network | ipaddress.IPv6Network",
"import ipaddress"
),
"circle" => new_type!("asyncpg.types.Circle", "import asyncpg.types"),
"date" => new_type!("datetime.date", "import datetime"),
"time" | "time with time zone" | "timetz" => {
new_type!("datetime.time", "import datetime")
}
"timestamp" | "timestamp with time zone" | "timestamptz" => {
new_type!("datetime.datetime", "import datetime")
}
"interval" => new_type!("datetime.timedelta", "import datetime"),
"float" | "double" | "precision" => new_type!("float"),
"smallint" | "integer" | "bigint" => new_type!("int"),
"numeric" => new_type!("decimal.Decimal", "import decimal"),
"json" | "jsonb" | "money" => new_type!("str"),
"line" => new_type!("asyncpg.Line", "import asyncpg"),
"lseg" => new_type!("asyncpg.LineSegment", "import asyncpg"),
"path" => new_type!("asyncpg.types.Path", "import asyncpg.types"),
"point" => new_type!("asyncpg.types.Point", "import asyncpg.types"),
"polygon" => new_type!("asyncpg.types.Polygon", "import asyncpg.types"),
"uuid" => new_type!("uuid.UUID", "import uuid"),
"tid" => new_type!("tuple"),
_ => new_type!("str"),
}
}
}
impl From<TypeOverrideOptions> for PythonType {
fn from(value: TypeOverrideOptions) -> Self {
PythonType {
<<<<<<< HEAD
constructor: value.name.clone(),
declaration: None,
annotation: value.name,
=======
constructor: value.python_type.clone(),
declaration: None,
annotation: value.python_type,
>>>>>>> main
encode: value.encode.clone(),
decode: value.decode.clone(),
import: value.import,
}
}
}
#[test]
fn parse_type_from_json() {
let json = r#"{ "name": "dict" }"#;
let type_option = serde_json::from_str::<TypeOverrideOptions>(json).unwrap();
let _ = PythonType::from(type_option);
}
#[test]
fn test_type_imports() {
let json = r#"{ "name": "uuid.UUID", "import": "import uuid" }"#;
let type_option = serde_json::from_str::<TypeOverrideOptions>(json).unwrap();
let type_ = PythonType::from(type_option);
<<<<<<< HEAD
assert!(type_.imports().any(|import| import == "import uuid"));
=======
assert_eq!(type_.import.as_deref(), Some("import uuid"));
>>>>>>> main
}
#[test]
fn test_type_new() {
let r#type = new_type!("uuid.UUID", "import, uuid");
assert_eq!(r#type.constructor, "uuid.UUID")
}