Skip to content

Commit 0af819c

Browse files
authored
Merge pull request #58 from golemcloud/fix_bugs
Return error when there is decode error for record
2 parents 5c375bc + 5922ffa commit 0af819c

File tree

2 files changed

+83
-10
lines changed

2 files changed

+83
-10
lines changed

docker-compose.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ services:
4242
- GOLEM__GRPC_PORT=9090
4343
- GOLEM__HTTP_PORT=8080
4444
volumes:
45-
- ./template_store:/template_store
46-
- ./golem_db:/app/golem_db
45+
- template_store:/template_store
46+
- golem_db:/app/golem_db
4747
ports:
4848
- "8080:8080"
4949
- "9090:9090"
@@ -84,4 +84,8 @@ services:
8484

8585
volumes:
8686
redis_data:
87-
template_store:
87+
driver: local
88+
template_store:
89+
driver: local
90+
golem_db:
91+
driver: local

golem-cloud-server-base/src/typechecker.rs

Lines changed: 76 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -960,15 +960,36 @@ fn get_record(
960960
let mut vals: Vec<VVal> = vec![];
961961

962962
for (name, tpe) in name_type_pairs {
963-
let json_value = json_map.get(name).unwrap_or(&Value::Null);
964-
965-
match validate_function_parameters(json_value, tpe.clone()) {
966-
Ok(result) => vals.push(VVal { val: Some(result) }),
967-
Err(errs) => errors.extend(errs),
963+
if let Some(json_value) = json_map.get(name) {
964+
match validate_function_parameters(json_value, tpe.clone()) {
965+
Ok(result) => vals.push(VVal { val: Some(result) }),
966+
Err(value_errors) => errors.extend(
967+
value_errors
968+
.iter()
969+
.map(|err| format!("Invalid value for the key {}. Error: {}", name, err))
970+
.collect::<Vec<_>>(),
971+
),
972+
}
973+
} else {
974+
match tpe {
975+
Type::Option(_) => {
976+
vals.push(VVal {
977+
val: Some(Val::Option(Box::new(ValOption {
978+
discriminant: 0,
979+
value: None,
980+
}))),
981+
});
982+
}
983+
_ => errors.push(format!("Key '{}' not found in json_map", name)),
984+
}
968985
}
969986
}
970987

971-
Ok(ValRecord { values: vals })
988+
if errors.is_empty() {
989+
Ok(ValRecord { values: vals })
990+
} else {
991+
Err(errors)
992+
}
972993
}
973994

974995
fn get_enum(input_json: &Value, names: Vec<String>) -> Result<ValEnum, Vec<String>> {
@@ -1064,7 +1085,7 @@ fn get_variant(
10641085

10651086
#[cfg(test)]
10661087
mod tests {
1067-
1088+
use serde_json::json;
10681089
use std::collections::HashSet;
10691090

10701091
use golem_api_grpc::proto::golem::template::{NameTypePair, TypePrimitive, TypeRecord};
@@ -1934,4 +1955,52 @@ mod tests {
19341955
}))
19351956
);
19361957
}
1958+
1959+
#[test]
1960+
fn test_get_record() {
1961+
// Test case where all keys are present
1962+
let input_json = json!({
1963+
"key1": "value1",
1964+
"key2": "value2",
1965+
});
1966+
1967+
let key1 = "key1".to_string();
1968+
let key2 = "key2".to_string();
1969+
1970+
let name_type_pairs: Vec<(&String, &Type)> = vec![
1971+
(
1972+
&key1,
1973+
&Type::Primitive(TypePrimitive {
1974+
primitive: PrimitiveType::Str as i32,
1975+
}),
1976+
),
1977+
(
1978+
&key2,
1979+
&Type::Primitive(TypePrimitive {
1980+
primitive: PrimitiveType::Str as i32,
1981+
}),
1982+
),
1983+
];
1984+
1985+
let result = get_record(&input_json, name_type_pairs.clone());
1986+
let expected_result = Ok(ValRecord {
1987+
values: vec![
1988+
VVal {
1989+
val: Some(Val::String("value1".to_string())),
1990+
},
1991+
VVal {
1992+
val: Some(Val::String("value2".to_string())),
1993+
},
1994+
],
1995+
});
1996+
assert_eq!(result, expected_result);
1997+
1998+
// Test case where a key is missing
1999+
let input_json = json!({
2000+
"key1": "value1",
2001+
});
2002+
2003+
let result = get_record(&input_json, name_type_pairs.clone());
2004+
assert_eq!(result.is_err(), true);
2005+
}
19372006
}

0 commit comments

Comments
 (0)