From 9e3173f49cf365cce54eb0b085d147e7ee41b510 Mon Sep 17 00:00:00 2001 From: DmitriyMusatkin Date: Tue, 30 Jul 2024 08:39:59 -0700 Subject: [PATCH] format --- include/aws/sdkutils/endpoints_rule_engine.h | 7 +++-- source/endpoints_rule_engine.c | 31 ++++++++------------ source/endpoints_ruleset.c | 17 ++++++----- source/endpoints_standard_lib.c | 4 +-- source/endpoints_types_impl.c | 23 +++++++++++++-- tests/endpoints_rule_engine_tests.c | 7 +++-- tests/resources/test-cases/string_array.json | 2 +- 7 files changed, 54 insertions(+), 37 deletions(-) diff --git a/include/aws/sdkutils/endpoints_rule_engine.h b/include/aws/sdkutils/endpoints_rule_engine.h index 4a1e149..bfd60b8 100644 --- a/include/aws/sdkutils/endpoints_rule_engine.h +++ b/include/aws/sdkutils/endpoints_rule_engine.h @@ -19,7 +19,11 @@ struct aws_endpoints_resolved_endpoint; struct aws_endpoints_request_context; struct aws_hash_table; -enum aws_endpoints_parameter_type { AWS_ENDPOINTS_PARAMETER_STRING, AWS_ENDPOINTS_PARAMETER_BOOLEAN, AWS_ENDPOINTS_PARAMETER_STRING_ARRAY }; +enum aws_endpoints_parameter_type { + AWS_ENDPOINTS_PARAMETER_STRING, + AWS_ENDPOINTS_PARAMETER_BOOLEAN, + AWS_ENDPOINTS_PARAMETER_STRING_ARRAY +}; enum aws_endpoints_resolved_endpoint_type { AWS_ENDPOINTS_RESOLVED_ENDPOINT, AWS_ENDPOINTS_RESOLVED_ERROR }; AWS_EXTERN_C_BEGIN @@ -243,7 +247,6 @@ AWS_SDKUTILS_API int aws_endpoints_request_context_add_string_array( struct aws_byte_cursor *values, size_t len); - /* * Resolve an endpoint given request context. * Resolved endpoint is returned through out_resolved_endpoint. diff --git a/source/endpoints_rule_engine.c b/source/endpoints_rule_engine.c index 168d8e4..2d04768 100644 --- a/source/endpoints_rule_engine.c +++ b/source/endpoints_rule_engine.c @@ -188,20 +188,12 @@ static int s_init_top_level_scope( switch (value->type) { case AWS_ENDPOINTS_PARAMETER_STRING: case AWS_ENDPOINTS_PARAMETER_BOOLEAN: + case AWS_ENDPOINTS_PARAMETER_STRING_ARRAY: val->value = value->default_value; + val->value.is_shallow = true; + AWS_LOGF_DEBUG(0, "assigned default"); + break; break; - case AWS_ENDPOINTS_PARAMETER_STRING_ARRAY: - { - size_t len = aws_array_list_length(&value->default_value.v.array); - aws_array_list_init_dynamic(&val->value.v.array, allocator, len, sizeof(struct aws_endpoints_value)); - for (int i = 0; i < len; ++i) { - struct aws_endpoints_value *elem; - aws_array_list_get_at_ptr(&value->default_value.v.array, (void**)&elem, i); - aws_array_list_set_at(&val->value.v.array, elem, i); - } - val->value.type = AWS_ENDPOINTS_VALUE_ARRAY; - } - break; default: AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Unexpected parameter type."); goto on_error; @@ -333,7 +325,7 @@ static int s_resolve_expr( goto on_error; } aws_array_list_set_at(&out_value->v.array, &val, i); - } + } } break; } @@ -348,7 +340,7 @@ static int s_resolve_expr( out_value->type = AWS_ENDPOINTS_VALUE_NONE; } else { struct aws_endpoints_scope_value *aws_endpoints_scope_value = element->value; - + *out_value = aws_endpoints_scope_value->value; out_value->is_shallow = true; } @@ -491,6 +483,7 @@ int aws_endpoints_path_through_array( } *out_value = *val; + out_value->is_shallow = true; return AWS_OP_SUCCESS; @@ -609,7 +602,10 @@ static int s_resolve_templated_value_with_pathing( goto on_error; } } else { - AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Invalid value type for pathing through. type %d", scope_value->value.type); + AWS_LOGF_ERROR( + AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, + "Invalid value type for pathing through. type %d", + scope_value->value.type); goto on_error; } @@ -759,12 +755,11 @@ int aws_endpoints_request_context_add_string_array( struct aws_endpoints_scope_value *val = aws_endpoints_scope_value_new(allocator, name); val->value.type = AWS_ENDPOINTS_VALUE_ARRAY; aws_array_list_init_dynamic(&val->value.v.array, allocator, len, sizeof(struct aws_endpoints_value)); - + for (size_t i = 0; i < len; ++i) { struct aws_endpoints_value elem = { .type = AWS_ENDPOINTS_VALUE_STRING, - .v.owning_cursor_object = aws_endpoints_owning_cursor_from_cursor(allocator, values[i]) - }; + .v.owning_cursor_object = aws_endpoints_owning_cursor_from_cursor(allocator, values[i])}; aws_array_list_set_at(&val->value.v.array, &elem, i); } diff --git a/source/endpoints_ruleset.c b/source/endpoints_ruleset.c index e529904..116226c 100644 --- a/source/endpoints_ruleset.c +++ b/source/endpoints_ruleset.c @@ -454,19 +454,21 @@ static int s_on_parameter_key( aws_json_value_get_string(default_node, &cur); parameter->default_value.type = AWS_ENDPOINTS_VALUE_STRING; parameter->default_value.v.owning_cursor_string = aws_endpoints_non_owning_cursor_create(cur); - AWS_LOGF_DEBUG(0, "foo " PRInSTR, AWS_BYTE_CURSOR_PRI(cur)); } else if (type == AWS_ENDPOINTS_PARAMETER_BOOLEAN && aws_json_value_is_boolean(default_node)) { - parameter->default_value.type = AWS_ENDPOINTS_VALUE_BOOLEAN; + parameter->default_value.type = AWS_ENDPOINTS_VALUE_BOOLEAN; aws_json_value_get_boolean(default_node, ¶meter->default_value.v.boolean); - AWS_LOGF_DEBUG(0, "bar %d", parameter->default_value.v.boolean); } else if (type == AWS_ENDPOINTS_PARAMETER_STRING_ARRAY && aws_json_value_is_array(default_node)) { + parameter->default_value.type = AWS_ENDPOINTS_VALUE_ARRAY; size_t len = aws_json_get_array_size(default_node); - aws_array_list_init_dynamic(¶meter->default_value.v.array, wrapper->allocator, len, sizeof(struct aws_endpoints_value)); + aws_array_list_init_dynamic( + ¶meter->default_value.v.array, wrapper->allocator, len, sizeof(struct aws_endpoints_value)); for (size_t i = 0; i < len; ++i) { struct aws_json_value *element = aws_json_get_array_element(default_node, i); if (!aws_json_value_is_string(element)) { - AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, - "Unexpected type for default parameter value. String array parameter must have string elements"); + AWS_LOGF_ERROR( + AWS_LS_SDKUTILS_ENDPOINTS_PARSING, + "Unexpected type for default parameter value. String array parameter must have string " + "elements"); goto on_error; } @@ -475,8 +477,7 @@ static int s_on_parameter_key( struct aws_endpoints_value val = { .type = AWS_ENDPOINTS_VALUE_STRING, - .v.owning_cursor_string = aws_endpoints_non_owning_cursor_create(cur) - }; + .v.owning_cursor_string = aws_endpoints_non_owning_cursor_create(cur)}; aws_array_list_set_at(¶meter->default_value.v.array, &val, i); } diff --git a/source/endpoints_standard_lib.c b/source/endpoints_standard_lib.c index a191018..2d8a4d4 100644 --- a/source/endpoints_standard_lib.c +++ b/source/endpoints_standard_lib.c @@ -83,7 +83,6 @@ static int s_resolve_fn_get_attr( struct aws_byte_cursor path_cur = argv_path.v.owning_cursor_string.cur; AWS_LOGF_DEBUG(0, "foo type %d", argv_value.type); - if (argv_value.type == AWS_ENDPOINTS_VALUE_OBJECT) { if (aws_endpoints_path_through_object(allocator, &argv_value, path_cur, out_value)) { AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to path through object."); @@ -97,7 +96,8 @@ static int s_resolve_fn_get_attr( goto on_done; } } else { - AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Invalid value type for pathing through. type: %d", argv_value.type); + AWS_LOGF_ERROR( + AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Invalid value type for pathing through. type: %d", argv_value.type); result = aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED); goto on_done; } diff --git a/source/endpoints_types_impl.c b/source/endpoints_types_impl.c index efdba6b..e971137 100644 --- a/source/endpoints_types_impl.c +++ b/source/endpoints_types_impl.c @@ -204,10 +204,12 @@ void aws_endpoints_value_clean_up(struct aws_endpoints_value *aws_endpoints_valu AWS_PRECONDITION(aws_endpoints_value); if (aws_endpoints_value->is_shallow) { - return; + goto on_done; } if (aws_endpoints_value->type == AWS_ENDPOINTS_VALUE_STRING) { + AWS_LOGF_DEBUG( + 0, "aaaa " PRInSTR " bbbb", AWS_BYTE_CURSOR_PRI(aws_endpoints_value->v.owning_cursor_string.cur)); aws_string_destroy(aws_endpoints_value->v.owning_cursor_string.string); } @@ -216,15 +218,17 @@ void aws_endpoints_value_clean_up(struct aws_endpoints_value *aws_endpoints_valu } if (aws_endpoints_value->type == AWS_ENDPOINTS_VALUE_ARRAY) { + AWS_LOGF_DEBUG(0, "foo %d ", aws_array_list_length(&aws_endpoints_value->v.array)); aws_array_list_deep_clean_up(&aws_endpoints_value->v.array, aws_endpoints_value_clean_up_cb); } +on_done: AWS_ZERO_STRUCT(*aws_endpoints_value); } void aws_endpoints_value_clean_up_cb(void *value) { struct aws_endpoints_value *aws_endpoints_value = value; - AWS_LOGF_DEBUG(0, "haha"); + AWS_LOGF_DEBUG(0, "haha %d", aws_endpoints_value->is_shallow); aws_endpoints_value_clean_up(aws_endpoints_value); } @@ -234,11 +238,24 @@ int aws_endpoints_deep_copy_parameter_value( struct aws_endpoints_value *to) { to->type = from->type; + AWS_LOGF_DEBUG(0, "haha boo %d", from->type); if (to->type == AWS_ENDPOINTS_VALUE_STRING) { - to->v.owning_cursor_string = aws_endpoints_owning_cursor_create(allocator, from->v.owning_cursor_string.string); + to->v.owning_cursor_string = + aws_endpoints_owning_cursor_from_cursor(allocator, from->v.owning_cursor_string.cur); } else if (to->type == AWS_ENDPOINTS_VALUE_BOOLEAN) { to->v.boolean = from->v.boolean; + } else if (to->type == AWS_ENDPOINTS_VALUE_ARRAY) { + size_t len = aws_array_list_length(&from->v.array); + aws_array_list_init_dynamic(&to->v.array, allocator, len, sizeof(struct aws_endpoints_value)); + for (size_t i = 0; i < len; ++i) { + struct aws_endpoints_value val; + aws_array_list_get_at(&from->v.array, &val, i); + + struct aws_endpoints_value to_val; + aws_endpoints_deep_copy_parameter_value(allocator, &val, &to_val); + aws_array_list_set_at(&to->v.array, &to_val, i); + } } else { AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Unexpected value type."); return aws_raise_error(AWS_ERROR_INVALID_STATE); diff --git a/tests/endpoints_rule_engine_tests.c b/tests/endpoints_rule_engine_tests.c index 3571dd8..19010bb 100644 --- a/tests/endpoints_rule_engine_tests.c +++ b/tests/endpoints_rule_engine_tests.c @@ -141,13 +141,14 @@ static int s_on_parameter_key( } else if (aws_json_value_is_array(value)) { struct aws_byte_cursor strings[MAX_STRING_ARRAY_ELEMENTS]; size_t len = aws_json_get_array_size(value); - ASSERT_TRUE(len < MAX_STRING_ARRAY_ELEMENTS); + ASSERT_TRUE(len <= MAX_STRING_ARRAY_ELEMENTS); for (size_t i = 0; i < len; ++i) { struct aws_json_value *str = aws_json_get_array_element(value, i); ASSERT_SUCCESS(aws_json_value_get_string(str, &strings[i])); } - ASSERT_SUCCESS(aws_endpoints_request_context_add_string_array(wrapper->allocator, - wrapper->context, *key, strings, len)); + ASSERT_SUCCESS( + aws_endpoints_request_context_add_string_array(wrapper->allocator, wrapper->context, *key, strings, len)); + return AWS_OP_SUCCESS; } AWS_LOGF_DEBUG(0, "bar"); diff --git a/tests/resources/test-cases/string_array.json b/tests/resources/test-cases/string_array.json index 8cd4197..c9305cb 100644 --- a/tests/resources/test-cases/string_array.json +++ b/tests/resources/test-cases/string_array.json @@ -33,7 +33,7 @@ { "documentation": "bound value from input", "params": { - "stringArrayParam": ["key1"] + "stringArrayParam": ["key1", "key2", "key3", "key4"] }, "expect": { "endpoint": {