From 2b8232f563afb4ef9607154e16d7b2afa2a53960 Mon Sep 17 00:00:00 2001 From: DmitriyMusatkin Date: Fri, 2 Aug 2024 11:20:11 -0700 Subject: [PATCH 1/4] switch to more efficient json api --- include/aws/sdkutils/endpoints_rule_engine.h | 2 +- .../sdkutils/private/endpoints_types_impl.h | 2 +- source/endpoints_ruleset.c | 64 ++++++++----------- source/endpoints_types_impl.c | 2 +- 4 files changed, 28 insertions(+), 42 deletions(-) diff --git a/include/aws/sdkutils/endpoints_rule_engine.h b/include/aws/sdkutils/endpoints_rule_engine.h index bfd60b8..314c3c2 100644 --- a/include/aws/sdkutils/endpoints_rule_engine.h +++ b/include/aws/sdkutils/endpoints_rule_engine.h @@ -22,7 +22,7 @@ struct aws_hash_table; enum aws_endpoints_parameter_type { AWS_ENDPOINTS_PARAMETER_STRING, AWS_ENDPOINTS_PARAMETER_BOOLEAN, - AWS_ENDPOINTS_PARAMETER_STRING_ARRAY + AWS_ENDPOINTS_PARAMETER_STRING_ARRAY, }; enum aws_endpoints_resolved_endpoint_type { AWS_ENDPOINTS_RESOLVED_ENDPOINT, AWS_ENDPOINTS_RESOLVED_ERROR }; diff --git a/include/aws/sdkutils/private/endpoints_types_impl.h b/include/aws/sdkutils/private/endpoints_types_impl.h index 1b402bf..5ef6ead 100644 --- a/include/aws/sdkutils/private/endpoints_types_impl.h +++ b/include/aws/sdkutils/private/endpoints_types_impl.h @@ -73,7 +73,7 @@ enum aws_endpoints_fn_type { enum aws_endpoints_value_type { /* Special value to represent that any value type is expected from resolving an expresion. - Note a valid value for a value type. */ + Not a valid value for a value type. */ AWS_ENDPOINTS_VALUE_ANY, AWS_ENDPOINTS_VALUE_NONE, diff --git a/source/endpoints_ruleset.c b/source/endpoints_ruleset.c index 8947854..20c5654 100644 --- a/source/endpoints_ruleset.c +++ b/source/endpoints_ruleset.c @@ -226,7 +226,7 @@ static int s_try_parse_reference(const struct aws_json_value *node, struct aws_b AWS_ZERO_STRUCT(*out_reference); - struct aws_json_value *ref_node = aws_json_value_get_from_object(node, aws_byte_cursor_from_c_str("ref")); + struct aws_json_value *ref_node = aws_json_value_get_from_object_c_str(node, "ref"); if (ref_node != NULL && aws_json_value_get_string(ref_node, out_reference)) { AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to parse ref."); AWS_ZERO_STRUCT(*out_reference); @@ -329,7 +329,7 @@ static int s_parse_function( AWS_ZERO_STRUCT(*function); - struct aws_json_value *fn_node = aws_json_value_get_from_object(node, aws_byte_cursor_from_c_str("fn")); + struct aws_json_value *fn_node = aws_json_value_get_from_object_c_str(node, "fn"); if (fn_node == NULL) { AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Node is not a function."); goto on_error; @@ -358,7 +358,7 @@ static int s_parse_function( goto on_error; } - struct aws_json_value *argv_node = aws_json_value_get_from_object(node, aws_byte_cursor_from_c_str("argv")); + struct aws_json_value *argv_node = aws_json_value_get_from_object_c_str(node, "argv"); if (argv_node == NULL || !aws_json_value_is_array(argv_node)) { AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "No argv or unexpected type."); goto on_error; @@ -395,7 +395,7 @@ static int s_on_parameter_key( /* required fields */ struct aws_byte_cursor type_cur; - struct aws_json_value *type_node = aws_json_value_get_from_object(value, aws_byte_cursor_from_c_str("type")); + struct aws_json_value *type_node = aws_json_value_get_from_object_c_str(value, "type"); if (type_node == NULL || aws_json_value_get_string(type_node, &type_cur)) { AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to extract parameter type."); goto on_error; @@ -415,8 +415,7 @@ static int s_on_parameter_key( parameter->type = type; - struct aws_json_value *documentation_node = - aws_json_value_get_from_object(value, aws_byte_cursor_from_c_str("documentation")); + struct aws_json_value *documentation_node = aws_json_value_get_from_object_c_str(value, "documentation"); /* TODO: spec calls for documentation to be required, but several test-cases are missing docs on parameters */ @@ -428,7 +427,7 @@ static int s_on_parameter_key( } /* optional fields */ - struct aws_json_value *built_in_node = aws_json_value_get_from_object(value, aws_byte_cursor_from_c_str("builtIn")); + struct aws_json_value *built_in_node = aws_json_value_get_from_object_c_str(value, "builtIn"); if (built_in_node != NULL) { if (aws_json_value_get_string(built_in_node, ¶meter->built_in)) { AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Unexpected type for built-in parameter field."); @@ -436,8 +435,7 @@ static int s_on_parameter_key( } } - struct aws_json_value *required_node = - aws_json_value_get_from_object(value, aws_byte_cursor_from_c_str("required")); + struct aws_json_value *required_node = aws_json_value_get_from_object_c_str(value, "required"); if (required_node != NULL) { if (!aws_json_value_is_boolean(required_node)) { AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Unexpected type for required parameter field."); @@ -446,7 +444,7 @@ static int s_on_parameter_key( aws_json_value_get_boolean(required_node, ¶meter->is_required); } - struct aws_json_value *default_node = aws_json_value_get_from_object(value, aws_byte_cursor_from_c_str("default")); + struct aws_json_value *default_node = aws_json_value_get_from_object_c_str(value, "default"); parameter->has_default_value = default_node != NULL; if (default_node != NULL) { if (type == AWS_ENDPOINTS_PARAMETER_STRING && aws_json_value_is_string(default_node)) { @@ -488,19 +486,16 @@ static int s_on_parameter_key( } } - struct aws_json_value *deprecated_node = - aws_json_value_get_from_object(value, aws_byte_cursor_from_c_str("deprecated")); + struct aws_json_value *deprecated_node = aws_json_value_get_from_object_c_str(value, "deprecated"); if (deprecated_node != NULL) { - struct aws_json_value *deprecated_message_node = - aws_json_value_get_from_object(deprecated_node, aws_byte_cursor_from_c_str("message")); + struct aws_json_value *deprecated_message_node = aws_json_value_get_from_object_c_str(deprecated_node, "message"); if (deprecated_message_node != NULL && aws_json_value_get_string(deprecated_message_node, ¶meter->deprecated_message)) { AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Unexpected value for deprecated message."); goto on_error; } - struct aws_json_value *deprecated_since_node = - aws_json_value_get_from_object(deprecated_node, aws_byte_cursor_from_c_str("since")); + struct aws_json_value *deprecated_since_node = aws_json_value_get_from_object_c_str(deprecated_node, "since"); if (deprecated_since_node != NULL && aws_json_value_get_string(deprecated_since_node, ¶meter->deprecated_since)) { AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Unexpected value for deprecated since."); @@ -540,8 +535,7 @@ static int s_on_condition_element( goto on_error; } - struct aws_json_value *assign_node = - aws_json_value_get_from_object(condition_node, aws_byte_cursor_from_c_str("assign")); + struct aws_json_value *assign_node = aws_json_value_get_from_object_c_str(condition_node, "assign"); if (assign_node != NULL && aws_json_value_get_string(assign_node, &condition.assign)) { AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Unexpected value for assign."); goto on_error; @@ -619,7 +613,7 @@ static int s_parse_endpoints_rule_data_endpoint( AWS_PRECONDITION(data_rule); data_rule->allocator = allocator; - struct aws_json_value *url_node = aws_json_value_get_from_object(rule_node, aws_byte_cursor_from_c_str("url")); + struct aws_json_value *url_node = aws_json_value_get_from_object_c_str(rule_node, "url"); if (url_node == NULL || aws_json_value_is_string(url_node)) { data_rule->url.type = AWS_ENDPOINTS_EXPR_STRING; aws_json_value_get_string(url_node, &data_rule->url.e.string); @@ -642,8 +636,7 @@ static int s_parse_endpoints_rule_data_endpoint( } } - struct aws_json_value *properties_node = - aws_json_value_get_from_object(rule_node, aws_byte_cursor_from_c_str("properties")); + struct aws_json_value *properties_node = aws_json_value_get_from_object_c_str(rule_node, "properties"); if (properties_node != NULL) { aws_byte_buf_init(&data_rule->properties, allocator, 0); @@ -668,8 +661,7 @@ static int s_parse_endpoints_rule_data_endpoint( aws_hash_callback_string_destroy, s_callback_headers_destroy); - struct aws_json_value *headers_node = - aws_json_value_get_from_object(rule_node, aws_byte_cursor_from_c_str("headers")); + struct aws_json_value *headers_node = aws_json_value_get_from_object_c_str(rule_node, "headers"); if (headers_node != NULL) { if (s_init_members_from_json(allocator, headers_node, &data_rule->headers, s_on_headers_key)) { @@ -738,7 +730,7 @@ static int s_parse_endpoints_rule_data_tree( AWS_PRECONDITION(rule_node); AWS_PRECONDITION(rule_data); - struct aws_json_value *rules_node = aws_json_value_get_from_object(rule_node, aws_byte_cursor_from_c_str("rules")); + struct aws_json_value *rules_node = aws_json_value_get_from_object_c_str(rule_node, "rules"); if (rules_node == NULL || !aws_json_value_is_array(rules_node)) { AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Rules node is missing or unexpected type."); return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_PARSE_FAILED); @@ -769,7 +761,7 @@ static int s_on_rule_element( /* Required fields */ struct aws_byte_cursor type_cur; - struct aws_json_value *type_node = aws_json_value_get_from_object(value, aws_byte_cursor_from_c_str("type")); + struct aws_json_value *type_node = aws_json_value_get_from_object_c_str(value, "type"); if (type_node == NULL || aws_json_value_get_string(type_node, &type_cur)) { AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to extract rule type."); return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_PARSE_FAILED); @@ -791,8 +783,7 @@ static int s_on_rule_element( AWS_ZERO_STRUCT(rule); rule.type = type; - struct aws_json_value *conditions_node = - aws_json_value_get_from_object(value, aws_byte_cursor_from_c_str("conditions")); + struct aws_json_value *conditions_node = aws_json_value_get_from_object_c_str(value, "conditions"); if (conditions_node == NULL || !aws_json_value_is_array(conditions_node)) { AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Conditions node missing."); goto on_error; @@ -809,8 +800,7 @@ static int s_on_rule_element( switch (type) { case AWS_ENDPOINTS_RULE_ENDPOINT: { - struct aws_json_value *endpoint_node = - aws_json_value_get_from_object(value, aws_byte_cursor_from_c_str("endpoint")); + struct aws_json_value *endpoint_node = aws_json_value_get_from_object_c_str(value, "endpoint"); if (endpoint_node == NULL || s_parse_endpoints_rule_data_endpoint(wrapper->allocator, endpoint_node, &rule.rule_data.endpoint)) { AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to extract endpoint rule data."); @@ -819,8 +809,7 @@ static int s_on_rule_element( break; } case AWS_ENDPOINTS_RULE_ERROR: { - struct aws_json_value *error_node = - aws_json_value_get_from_object(value, aws_byte_cursor_from_c_str("error")); + struct aws_json_value *error_node = aws_json_value_get_from_object_c_str(value, "error"); if (error_node == NULL || s_parse_endpoints_rule_data_error(wrapper->allocator, error_node, &rule.rule_data.error)) { AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to extract error rule data."); @@ -840,8 +829,7 @@ static int s_on_rule_element( } /* Optional fields */ - struct aws_json_value *documentation_node = - aws_json_value_get_from_object(value, aws_byte_cursor_from_c_str("documentation")); + struct aws_json_value *documentation_node = aws_json_value_get_from_object_c_str(value, "documentation"); if (documentation_node != NULL) { if (aws_json_value_get_string(documentation_node, &rule.documentation)) { AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to extract parameter documentation."); @@ -875,7 +863,7 @@ static int s_init_ruleset_from_json( ruleset->json_root = root; - struct aws_json_value *version_node = aws_json_value_get_from_object(root, aws_byte_cursor_from_c_str("version")); + struct aws_json_value *version_node = aws_json_value_get_from_object_c_str(root, "version"); if (version_node == NULL || aws_json_value_get_string(version_node, &ruleset->version)) { AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to extract version."); aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_UNSUPPORTED_RULESET); @@ -890,8 +878,7 @@ static int s_init_ruleset_from_json( } #endif - struct aws_json_value *service_id_node = - aws_json_value_get_from_object(root, aws_byte_cursor_from_c_str("serviceId")); + struct aws_json_value *service_id_node = aws_json_value_get_from_object_c_str(root, "serviceId"); if (service_id_node != NULL && aws_json_value_get_string(service_id_node, &ruleset->service_id)) { AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to extract serviceId."); @@ -908,8 +895,7 @@ static int s_init_ruleset_from_json( NULL, s_callback_endpoints_parameter_destroy); - struct aws_json_value *parameters_node = - aws_json_value_get_from_object(root, aws_byte_cursor_from_c_str("parameters")); + struct aws_json_value *parameters_node = aws_json_value_get_from_object_c_str(root, "parameters"); if (parameters_node == NULL || s_init_members_from_json(allocator, parameters_node, &ruleset->parameters, s_on_parameter_key)) { AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to extract parameters."); @@ -917,7 +903,7 @@ static int s_init_ruleset_from_json( goto on_error; } - struct aws_json_value *rules_node = aws_json_value_get_from_object(root, aws_byte_cursor_from_c_str("rules")); + struct aws_json_value *rules_node = aws_json_value_get_from_object_c_str(root, "rules"); if (rules_node == NULL || !aws_json_value_is_array(rules_node)) { AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Unexpected type for rules node."); aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_PARSE_FAILED); diff --git a/source/endpoints_types_impl.c b/source/endpoints_types_impl.c index 6e0a387..e20b812 100644 --- a/source/endpoints_types_impl.c +++ b/source/endpoints_types_impl.c @@ -262,7 +262,7 @@ int aws_endpoints_deep_copy_parameter_value( AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Unexpected value type."); return aws_raise_error(AWS_ERROR_INVALID_STATE); } - + aws_byte_cursor_is_valid return AWS_OP_SUCCESS; on_error: From 6747c48c2e49c2834e5ada656a539687f44c3115 Mon Sep 17 00:00:00 2001 From: DmitriyMusatkin Date: Fri, 2 Aug 2024 11:26:03 -0700 Subject: [PATCH 2/4] minor cleanup --- include/aws/sdkutils/endpoints_rule_engine.h | 2 +- source/endpoints_rule_engine.c | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/include/aws/sdkutils/endpoints_rule_engine.h b/include/aws/sdkutils/endpoints_rule_engine.h index 314c3c2..65710ab 100644 --- a/include/aws/sdkutils/endpoints_rule_engine.h +++ b/include/aws/sdkutils/endpoints_rule_engine.h @@ -244,7 +244,7 @@ AWS_SDKUTILS_API int aws_endpoints_request_context_add_string_array( struct aws_allocator *allocator, struct aws_endpoints_request_context *context, struct aws_byte_cursor name, - struct aws_byte_cursor *values, + struct aws_byte_cursor *value_array, size_t len); /* diff --git a/source/endpoints_rule_engine.c b/source/endpoints_rule_engine.c index bdb40a3..05b4b42 100644 --- a/source/endpoints_rule_engine.c +++ b/source/endpoints_rule_engine.c @@ -191,7 +191,6 @@ static int s_init_top_level_scope( val->value = value->default_value; val->value.is_ref = true; break; - break; default: AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Unexpected parameter type."); goto on_error; From 25cea570018e8257763e65f86e4d8141366a0ecd Mon Sep 17 00:00:00 2001 From: DmitriyMusatkin Date: Fri, 2 Aug 2024 11:26:53 -0700 Subject: [PATCH 3/4] lint --- source/endpoints_ruleset.c | 3 ++- source/endpoints_types_impl.c | 3 +-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/source/endpoints_ruleset.c b/source/endpoints_ruleset.c index 20c5654..866f865 100644 --- a/source/endpoints_ruleset.c +++ b/source/endpoints_ruleset.c @@ -488,7 +488,8 @@ static int s_on_parameter_key( struct aws_json_value *deprecated_node = aws_json_value_get_from_object_c_str(value, "deprecated"); if (deprecated_node != NULL) { - struct aws_json_value *deprecated_message_node = aws_json_value_get_from_object_c_str(deprecated_node, "message"); + struct aws_json_value *deprecated_message_node = + aws_json_value_get_from_object_c_str(deprecated_node, "message"); if (deprecated_message_node != NULL && aws_json_value_get_string(deprecated_message_node, ¶meter->deprecated_message)) { AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Unexpected value for deprecated message."); diff --git a/source/endpoints_types_impl.c b/source/endpoints_types_impl.c index e20b812..b1359d9 100644 --- a/source/endpoints_types_impl.c +++ b/source/endpoints_types_impl.c @@ -262,8 +262,7 @@ int aws_endpoints_deep_copy_parameter_value( AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Unexpected value type."); return aws_raise_error(AWS_ERROR_INVALID_STATE); } - aws_byte_cursor_is_valid - return AWS_OP_SUCCESS; + aws_byte_cursor_is_valid return AWS_OP_SUCCESS; on_error: aws_endpoints_value_clean_up(to); From cfbc9124511fec8d2205b740f152bdb1bf193535 Mon Sep 17 00:00:00 2001 From: DmitriyMusatkin Date: Fri, 2 Aug 2024 11:28:21 -0700 Subject: [PATCH 4/4] typo --- source/endpoints_types_impl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/endpoints_types_impl.c b/source/endpoints_types_impl.c index b1359d9..3f16cf1 100644 --- a/source/endpoints_types_impl.c +++ b/source/endpoints_types_impl.c @@ -262,7 +262,7 @@ int aws_endpoints_deep_copy_parameter_value( AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Unexpected value type."); return aws_raise_error(AWS_ERROR_INVALID_STATE); } - aws_byte_cursor_is_valid return AWS_OP_SUCCESS; + return AWS_OP_SUCCESS; on_error: aws_endpoints_value_clean_up(to);