Skip to content

Commit 316d88e

Browse files
committed
simplify primitives and fix explicit cfg::Config key
1 parent cb926b3 commit 316d88e

File tree

7 files changed

+84
-32
lines changed

7 files changed

+84
-32
lines changed

gel-config/src/schema2/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ pub enum ConfigSchemaPrimitiveType {
6868
Memory,
6969
}
7070

71+
impl ConfigSchemaPrimitiveType {
72+
pub fn is_literal(&self) -> bool {
73+
match self {
74+
Self::Str | Self::Bool | Self::Int16 | Self::Int32 | Self::Int64 => true,
75+
_ => false,
76+
}
77+
}
78+
}
79+
7180
impl FromStr for ConfigSchemaPrimitiveType {
7281
type Err = ();
7382
fn from_str(s: &str) -> Result<Self, Self::Err> {

gel-config/src/schema2/ops.rs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,28 +86,43 @@ pub struct SchemaNamedValue {
8686

8787
#[derive(Debug, Clone)]
8888
pub enum SchemaValue {
89-
Unitary(String),
90-
Array(Vec<String>),
89+
Unitary(SchemaPrimitive),
90+
Array(Vec<SchemaPrimitive>),
9191
Object(IndexMap<String, SchemaNamedValue>),
9292
}
9393

94+
#[derive(Debug, Clone)]
95+
pub enum SchemaPrimitive {
96+
String(String),
97+
Bool(bool),
98+
Integer(isize),
99+
}
100+
101+
impl SchemaPrimitive {
102+
pub fn to_ddl(&self, property_type: &str) -> String {
103+
match self {
104+
SchemaPrimitive::String(s) => format!("<{property_type}>{}", quote_string(s)),
105+
SchemaPrimitive::Bool(b) => format!("<{property_type}>{}", b),
106+
SchemaPrimitive::Integer(i) => format!("<{property_type}>{}", i),
107+
}
108+
}
109+
}
110+
94111
impl SchemaValue {
95112
pub fn to_ddl(&self, property_type: &str) -> String {
96113
self.to_ddl_with_type(property_type, None)
97114
}
98115

99116
pub fn to_ddl_with_type(&self, property_type: &str, object_type: Option<&str>) -> String {
100117
match self {
101-
SchemaValue::Unitary(val) => format!("<{}>{}\n", property_type, quote_string(val))
102-
.trim()
103-
.to_string(),
118+
SchemaValue::Unitary(val) => val.to_ddl(property_type),
104119
SchemaValue::Array(vals) => {
105120
let mut result = String::from("{");
106121
for (i, v) in vals.iter().enumerate() {
107122
if i > 0 {
108123
result.push_str(", ");
109124
}
110-
result.push_str(&format!("<{}>'{}'\n", property_type, v).trim());
125+
result.push_str(&v.to_ddl(property_type));
111126
}
112127
result.push_str("}");
113128
result
@@ -189,7 +204,7 @@ mod tests {
189204
set: vec![SchemaNamedValue {
190205
name: "test_property".to_string(),
191206
property_type: "settype".to_string(),
192-
value: SchemaValue::Unitary("string".to_string()),
207+
value: SchemaValue::Unitary(SchemaPrimitive::String("string".to_string())),
193208
object_type: None,
194209
}],
195210
insert: IndexMap::from_iter([(
@@ -202,7 +217,9 @@ mod tests {
202217
SchemaNamedValue {
203218
name: "test1".to_string(),
204219
property_type: "type1".to_string(),
205-
value: SchemaValue::Unitary("test".to_string()),
220+
value: SchemaValue::Unitary(SchemaPrimitive::String(
221+
"test".to_string(),
222+
)),
206223
object_type: None,
207224
},
208225
),
@@ -211,7 +228,9 @@ mod tests {
211228
SchemaNamedValue {
212229
name: "test2".to_string(),
213230
property_type: "type2".to_string(),
214-
value: SchemaValue::Unitary("test2".to_string()),
231+
value: SchemaValue::Unitary(SchemaPrimitive::String(
232+
"test2".to_string(),
233+
)),
215234
object_type: None,
216235
},
217236
),

gel-config/src/schema2/parser.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,17 @@ fn apply_config(
9696
for (key, value) in value.as_table().ok_or_else(|| {
9797
ParserError::InvalidValueType(key.clone(), ConfigPropertyType::Object(Default::default()))
9898
})? {
99+
if key == "cfg::Config" {
100+
let table = schema.get_root_table();
101+
let Some(toml_table) = value.as_table() else {
102+
return Err(ParserError::ExpectedTableOrArray(key.clone()));
103+
};
104+
let properties = parse_properties(toml_table, table)?;
105+
for (_, property) in properties {
106+
ops.set.push(property);
107+
}
108+
continue;
109+
}
99110
let tables = schema.get_tables_by_path_or_name(key);
100111
if (value.is_array() || value.is_table()) && !tables.is_empty() {
101112
if value.is_array() && !tables[0].multi {
@@ -242,26 +253,30 @@ pub fn parse_property(
242253
let (property_type, value, object_type) = match (value, &property.property_type) {
243254
(toml::Value::String(value), ConfigPropertyType::Primitive(primitive_type)) => (
244255
primitive_type.to_schema_type().name,
245-
SchemaValue::Unitary(value.clone()),
256+
SchemaValue::Unitary(ops::SchemaPrimitive::String(value.clone())),
246257
None,
247258
),
248259
(toml::Value::String(value), ConfigPropertyType::Enum(name, _)) => {
249260
// TODO: check enum values
250-
(name.clone(), SchemaValue::Unitary(value.clone()), None)
261+
(
262+
name.clone(),
263+
SchemaValue::Unitary(ops::SchemaPrimitive::String(value.clone())),
264+
None,
265+
)
251266
}
252267
(toml::Value::Integer(value), ConfigPropertyType::Primitive(primitive_type)) => (
253268
primitive_type.to_schema_type().name,
254-
SchemaValue::Unitary(value.to_string()),
269+
SchemaValue::Unitary(ops::SchemaPrimitive::Integer(*value as isize)),
255270
None,
256271
),
257272
(toml::Value::Float(value), ConfigPropertyType::Primitive(primitive_type)) => (
258273
primitive_type.to_schema_type().name,
259-
SchemaValue::Unitary(value.to_string()),
274+
SchemaValue::Unitary(ops::SchemaPrimitive::String(value.to_string())),
260275
None,
261276
),
262277
(toml::Value::Boolean(value), ConfigPropertyType::Primitive(primitive_type)) => (
263278
primitive_type.to_schema_type().name,
264-
SchemaValue::Unitary(value.to_string()),
279+
SchemaValue::Unitary(ops::SchemaPrimitive::Bool(*value)),
265280
None,
266281
),
267282
(toml::Value::Array(value), ConfigPropertyType::Array(array_type)) => (
@@ -274,7 +289,7 @@ pub fn parse_property(
274289
.ok_or_else(|| {
275290
ParserError::InvalidValueType(key.to_string(), *array_type.clone())
276291
})
277-
.map(|s| s.to_owned())
292+
.map(|s| ops::SchemaPrimitive::String(s.to_owned()))
278293
})
279294
.collect::<Result<Vec<_>, _>>()?,
280295
),
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
1-
configure instance set http_max_connections := <std::int64>'100';
1+
configure instance set http_max_connections := <std::int64>100;
22
configure instance reset auth;
33
configure instance insert cfg::Auth {
4-
priority := <std::int64>'100',
4+
priority := <std::int64>100,
55
user := {<std::str>'gel'},
66
method := (insert cfg::JWT {
77
transports := {<cfg::ConnectionTransport>'HTTP'}
88
})
99
};
1010
configure instance insert cfg::Auth {
11-
priority := <std::int64>'200',
11+
priority := <std::int64>200,
1212
user := {<std::str>'admin', <std::str>'gel'},
1313
method := (insert cfg::SCRAM {
1414
transports := {<cfg::ConnectionTransport>'TCP', <cfg::ConnectionTransport>'HTTP'}
1515
})
1616
};
17-
configure current database set cfg::Config::allow_user_specified_id := <std::bool>'true';
17+
configure current database set allow_user_specified_id := <std::bool>true;
1818
configure current database set query_execution_timeout := <std::duration>'1 minute';
1919
configure current database set session_idle_transaction_timeout := <std::duration>'30 seconds';
2020
configure current database reset email_providers;
2121
configure current database insert cfg::SMTPProviderConfig {
2222
name := <std::str>'some-other-smtp-provider',
23-
port := <std::int32>'2525',
24-
validate_certs := <std::bool>'false',
23+
port := <std::int32>2525,
24+
validate_certs := <std::bool>false,
2525
timeout_per_email := <std::duration>'5 minutes',
2626
timeout_per_attempt := <std::duration>'1 minute'
2727
};
2828
configure current database insert cfg::SMTPProviderConfig {
2929
name := <std::str>'mailtrap-sandbox',
30-
port := <std::int32>'2525',
31-
validate_certs := <std::bool>'false',
30+
port := <std::int32>2525,
31+
validate_certs := <std::bool>false,
3232
timeout_per_email := <std::duration>'5 minutes',
3333
timeout_per_attempt := <std::duration>'1 minute'
3434
};

gel-config/tests/client/full.ddl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
configure instance set http_max_connections := <std::int64>'100';
1+
configure instance set http_max_connections := <std::int64>100;
22
configure current database set allow_bare_ddl := <cfg::AllowBareDDL>'NeverAllow';
3-
configure current database set allow_user_specified_id := <std::bool>'false';
4-
configure current database set auto_rebuild_query_cache := <std::bool>'false';
3+
configure current database set allow_user_specified_id := <std::bool>false;
4+
configure current database set auto_rebuild_query_cache := <std::bool>false;
55
configure current database set auto_rebuild_query_cache_timeout := <std::duration>'30 seconds';
66
configure current database set cors_allow_origins := {<std::str>'http://localhost:8000', <std::str>'http://127.0.0.1:8000'};
77
configure current database set current_email_provider_name := <std::str>'mailtrap_sandbox';
@@ -15,16 +15,16 @@ configure current database set ext::auth::AuthConfig::logo_url := <std::str>'htt
1515
configure current database set ext::auth::AuthConfig::token_time_to_live := <std::duration>'1 hour';
1616
configure current database set query_execution_timeout := <std::duration>'1 minute';
1717
configure current database set session_idle_transaction_timeout := <std::duration>'30 seconds';
18-
configure current database set warn_old_scoping := <std::bool>'false';
18+
configure current database set warn_old_scoping := <std::bool>false;
1919
configure current database reset email_providers;
2020
configure current database insert cfg::SMTPProviderConfig {
2121
name := <std::str>'mailtrap_sandbox',
2222
sender := <std::str>'hello@example.com',
2323
host := <std::str>'sandbox.smtp.mailtrap.io',
24-
port := <std::int32>'2525',
24+
port := <std::int32>2525,
2525
username := <std::str>'YOUR_USERNAME',
2626
password := <std::str>'YOUR_PASSWORD',
27-
validate_certs := <std::bool>'false',
27+
validate_certs := <std::bool>false,
2828
timeout_per_email := <std::duration>'5 minutes',
2929
timeout_per_attempt := <std::duration>'1 minute'
3030
};
@@ -73,7 +73,7 @@ configure current database insert ext::auth::DiscordOAuthProvider {
7373
secret := <std::str>'YOUR_DISCORD_SECRET'
7474
};
7575
configure current database insert ext::auth::EmailPasswordProviderConfig {
76-
require_verification := <std::bool>'false'
76+
require_verification := <std::bool>false
7777
};
7878
configure current database insert ext::auth::GitHubOAuthProvider {
7979
additional_scope := <std::str>'read:user user:email',
@@ -95,7 +95,7 @@ configure current database insert ext::auth::SlackOAuthProvider {
9595
};
9696
configure current database insert ext::auth::WebAuthnProviderConfig {
9797
relying_party_origin := <std::str>'https://example.com',
98-
require_verification := <std::bool>'true'
98+
require_verification := <std::bool>true
9999
};
100100
configure current database reset ext::auth::AuthConfig::ui;
101101
configure current database insert ext::auth::UIConfig {

gel-config/tests/client/object.ddl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
configure current database reset ext::auth::AuthConfig::providers;
22
configure current database insert ext::auth::EmailPasswordProviderConfig {
3-
require_verification := <std::bool>'false'
3+
require_verification := <std::bool>false
44
};

gel-config/tests/test_client.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ fn test_complex() {
1111

1212
let ops = parse_toml(&schema, &toml).unwrap();
1313
eprintln!("{}", ops.to_ddl());
14+
if std::env::var("UPDATE_EXPECTED").is_ok() {
15+
std::fs::write("tests/client/complex.ddl", ops.to_ddl()).unwrap();
16+
}
1417
assert_eq!(
1518
std::fs::read_to_string("tests/client/complex.ddl").unwrap(),
1619
ops.to_ddl(),
@@ -25,6 +28,9 @@ fn test_full() {
2528
let toml = toml::Table::deserialize(toml).unwrap();
2629
let ops = parse_toml(&schema, &toml).unwrap();
2730
eprintln!("{}", ops.to_ddl());
31+
if std::env::var("UPDATE_EXPECTED").is_ok() {
32+
std::fs::write("tests/client/full.ddl", ops.to_ddl()).unwrap();
33+
}
2834
assert_eq!(
2935
std::fs::read_to_string("tests/client/full.ddl").unwrap(),
3036
ops.to_ddl(),
@@ -39,6 +45,9 @@ fn test_object() {
3945
let toml = toml::Table::deserialize(toml).unwrap();
4046
let ops = parse_toml(&schema, &toml).unwrap();
4147
eprintln!("{}", ops.to_ddl());
48+
if std::env::var("UPDATE_EXPECTED").is_ok() {
49+
std::fs::write("tests/client/object.ddl", ops.to_ddl()).unwrap();
50+
}
4251
assert_eq!(
4352
std::fs::read_to_string("tests/client/object.ddl").unwrap(),
4453
ops.to_ddl(),

0 commit comments

Comments
 (0)