From f1c31a59fa61f48e6dcd21c393bf93a8363b3115 Mon Sep 17 00:00:00 2001 From: Mingwei Samuel Date: Tue, 29 Oct 2019 16:51:41 -0700 Subject: [PATCH] fix usage of enums in endpoints --- src/consts/queue_type.rs | 6 ++--- src/endpoints.rs | 6 ++--- srcgen/dotUtils.js | 48 +++++++++++++++++++++++++++------------- 3 files changed, 39 insertions(+), 21 deletions(-) diff --git a/src/consts/queue_type.rs b/src/consts/queue_type.rs index 7114ae6..ff9cb5a 100644 --- a/src/consts/queue_type.rs +++ b/src/consts/queue_type.rs @@ -23,16 +23,16 @@ mod test { #[test] fn check_as_ref() { - assert_eq!("RANKED_SOLO_5x5", QueueType::RankedSolo5x5.as_ref()); + assert_eq!("RANKED_SOLO_5x5", QueueType::RANKED_SOLO_5x5.as_ref()); } #[test] fn check_to_string() { - assert_eq!("RANKED_SOLO_5x5", QueueType::RankedSolo5x5.to_string()); + assert_eq!("RANKED_SOLO_5x5", QueueType::RANKED_SOLO_5x5.to_string()); } #[test] fn check_from_string() { - assert_eq!(Some(QueueType::RankedSolo5x5), "RANKED_SOLO_5x5".parse().ok()); + assert_eq!(Some(QueueType::RANKED_SOLO_5x5), "RANKED_SOLO_5x5".parse().ok()); } } diff --git a/src/endpoints.rs b/src/endpoints.rs index a969ed9..4f897fc 100644 --- a/src/endpoints.rs +++ b/src/endpoints.rs @@ -387,9 +387,9 @@ impl<'a> MatchV4<'a> { -> impl Future>> + 'a { let mut query_params = Serializer::new(String::new()); - if let Some(champion) = champion { query_params.extend_pairs(champion.iter().map(|w| ("champion", w.to_string()))); }; - if let Some(queue) = queue { query_params.extend_pairs(queue.iter().map(|w| ("queue", w.to_string()))); }; - if let Some(season) = season { query_params.extend_pairs(season.iter().map(|w| ("season", w.to_string()))); }; + if let Some(champion) = champion { query_params.extend_pairs(champion.iter().map(|w| ("champion", Into::::into(*w).to_string()))); }; + if let Some(queue) = queue { query_params.extend_pairs(queue.iter().map(|w| ("queue", Into::::into(*w).to_string()))); }; + if let Some(season) = season { query_params.extend_pairs(season.iter().map(|w| ("season", Into::::into(*w).to_string()))); }; if let Some(end_time) = end_time { query_params.append_pair("endTime", &*end_time.to_string()); }; if let Some(begin_time) = begin_time { query_params.append_pair("beginTime", &*begin_time.to_string()); }; if let Some(end_index) = end_index { query_params.append_pair("endIndex", &*end_index.to_string()); }; diff --git a/srcgen/dotUtils.js b/srcgen/dotUtils.js index 89ac6d8..65c2748 100644 --- a/srcgen/dotUtils.js +++ b/srcgen/dotUtils.js @@ -1,5 +1,14 @@ const changeCase = require('change-case'); +const enumTypeLookup = { + champion: 'i16', + gameMode: 'u8', + gameType: 'u8', + map: 'u8', + queue: 'u16', + season: 'u8', +}; + // flatMap: https://gist.github.com/samgiles/762ee337dff48623e729 // [B](f: (A) ⇒ [B]): [B] ; Although the types in the arrays aren't strict (: Array.prototype.flatMap = function(lambda) { @@ -85,26 +94,35 @@ function formatJsonProperty(name) { return `#[serde(rename = "${name}")]`; } -function formatQueryParamStringify(name, prop, ownedOk = false) { +function formatQueryParamStringify(name, prop, useOwned = false) { + const own = useOwned ? '' : '&*'; + if (prop['x-enum']) { switch (prop.type) { - case 'boolean': return `${name} ? "true" : "false"`; - case 'string': return name; - default: return (ownedOk ? '' : '&*') + `${name}.to_string()`; + case 'integer': + return `${own}Into::<${enumTypeLookup[prop['x-enum']]}>::into(*${name}).to_string()`; + default: throw new Error(`Enum not supported: ${JSON.stringify(prop)}.`) } + } + switch (prop.type) { + case 'array': throw new Error(`Cannot formart array: ${JSON.stringify(prop)}.`); + case 'boolean': return `${name} ? "true" : "false"`; + case 'string': return name; + default: return `${own}${name}.to_string()`; + } } function formatAddQueryParam(param) { - let k = `"${param.name}"`; - let name = changeCase.snakeCase(param.name); - let nc = param.required ? '' : `if let Some(${name}) = ${name} `; - let prop = param.schema; - switch (prop.type) { - case 'array': return `${nc}{ query_params.extend_pairs(${name}.iter()` - + `.map(|w| (${k}, ${formatQueryParamStringify("w", prop, true)}))); }`; - case 'object': throw 'unsupported'; - default: - return `${nc}{ query_params.append_pair(${k}, ${formatQueryParamStringify(name, prop)}); }`; - } + let k = `"${param.name}"`; + let name = changeCase.snakeCase(param.name); + let nc = param.required ? '' : `if let Some(${name}) = ${name} `; + let prop = param.schema; + switch (prop.type) { + case 'array': return `${nc}{ query_params.extend_pairs(${name}.iter()` + + `.map(|w| (${k}, ${formatQueryParamStringify("w", prop.items, true)}))); }`; + case 'object': throw 'unsupported'; + default: + return `${nc}{ query_params.append_pair(${k}, ${formatQueryParamStringify(name, prop)}); }`; + } } function formatRouteArgument(route, pathParams = []) {