fix usage of enums in endpoints

pull/5/head
Mingwei Samuel 2019-10-29 16:51:41 -07:00
parent 541ec9762c
commit f1c31a59fa
3 changed files with 39 additions and 21 deletions

View File

@ -23,16 +23,16 @@ mod test {
#[test] #[test]
fn check_as_ref() { 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] #[test]
fn check_to_string() { 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] #[test]
fn check_from_string() { 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());
} }
} }

View File

@ -387,9 +387,9 @@ impl<'a> MatchV4<'a> {
-> impl Future<Output = Result<Option<match_v4::Matchlist>>> + 'a -> impl Future<Output = Result<Option<match_v4::Matchlist>>> + 'a
{ {
let mut query_params = Serializer::new(String::new()); 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(champion) = champion { query_params.extend_pairs(champion.iter().map(|w| ("champion", Into::<i16>::into(*w).to_string()))); };
if let Some(queue) = queue { query_params.extend_pairs(queue.iter().map(|w| ("queue", w.to_string()))); }; if let Some(queue) = queue { query_params.extend_pairs(queue.iter().map(|w| ("queue", Into::<u16>::into(*w).to_string()))); };
if let Some(season) = season { query_params.extend_pairs(season.iter().map(|w| ("season", w.to_string()))); }; if let Some(season) = season { query_params.extend_pairs(season.iter().map(|w| ("season", Into::<u8>::into(*w).to_string()))); };
if let Some(end_time) = end_time { query_params.append_pair("endTime", &*end_time.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(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()); }; if let Some(end_index) = end_index { query_params.append_pair("endIndex", &*end_index.to_string()); };

View File

@ -1,5 +1,14 @@
const changeCase = require('change-case'); 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 // flatMap: https://gist.github.com/samgiles/762ee337dff48623e729
// [B](f: (A) ⇒ [B]): [B] ; Although the types in the arrays aren't strict (: // [B](f: (A) ⇒ [B]): [B] ; Although the types in the arrays aren't strict (:
Array.prototype.flatMap = function(lambda) { Array.prototype.flatMap = function(lambda) {
@ -85,26 +94,35 @@ function formatJsonProperty(name) {
return `#[serde(rename = "${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) { switch (prop.type) {
case 'boolean': return `${name} ? "true" : "false"`; case 'integer':
case 'string': return name; return `${own}Into::<${enumTypeLookup[prop['x-enum']]}>::into(*${name}).to_string()`;
default: return (ownedOk ? '' : '&*') + `${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) { function formatAddQueryParam(param) {
let k = `"${param.name}"`; let k = `"${param.name}"`;
let name = changeCase.snakeCase(param.name); let name = changeCase.snakeCase(param.name);
let nc = param.required ? '' : `if let Some(${name}) = ${name} `; let nc = param.required ? '' : `if let Some(${name}) = ${name} `;
let prop = param.schema; let prop = param.schema;
switch (prop.type) { switch (prop.type) {
case 'array': return `${nc}{ query_params.extend_pairs(${name}.iter()` case 'array': return `${nc}{ query_params.extend_pairs(${name}.iter()`
+ `.map(|w| (${k}, ${formatQueryParamStringify("w", prop, true)}))); }`; + `.map(|w| (${k}, ${formatQueryParamStringify("w", prop.items, true)}))); }`;
case 'object': throw 'unsupported'; case 'object': throw 'unsupported';
default: default:
return `${nc}{ query_params.append_pair(${k}, ${formatQueryParamStringify(name, prop)}); }`; return `${nc}{ query_params.append_pair(${k}, ${formatQueryParamStringify(name, prop)}); }`;
} }
} }
function formatRouteArgument(route, pathParams = []) { function formatRouteArgument(route, pathParams = []) {