Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions env/env_yaml.cc
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,9 @@ absl::StatusOr<Config::FunctionOverloadConfig> ParseFunctionOverloadConfig(
function_name, "\""));
}
overload_config.is_member_function = parsed_signature.is_member;
if (overload_config.overload_id.empty()) {
overload_config.overload_id = signature;
}
if (!parsed_signature.signature_type.has_function()) {
return absl::InternalError(absl::StrCat(
"Function overload signature has no function type: ", signature));
Expand Down Expand Up @@ -1101,11 +1104,8 @@ void EmitFunctionOverloadConfig(
const Config::FunctionOverloadConfig& overload_config, YAML::Emitter& out,
const EnvConfigToYamlOptions& options) {
out << YAML::BeginMap;
if (!overload_config.overload_id.empty()) {
out << YAML::Key << "id";
out << YAML::Value << YAML::DoubleQuoted << overload_config.overload_id;
}
bool signature_generated = false;
std::string signature_str;
if (options.use_type_signatures) {
bool param_type_spec_generated = true;
std::vector<TypeSpec> params;
Expand All @@ -1123,12 +1123,21 @@ void EmitFunctionOverloadConfig(
common_internal::MakeOverloadSignature(
function_name, params, overload_config.is_member_function);
if (signature.ok()) {
out << YAML::Key << "signature";
out << YAML::Value << YAML::DoubleQuoted << *signature;
signature_str = std::move(*signature);
signature_generated = true;
}
}
}
if (!overload_config.overload_id.empty()) {
if (!signature_generated || overload_config.overload_id != signature_str) {
out << YAML::Key << "id";
out << YAML::Value << YAML::DoubleQuoted << overload_config.overload_id;
}
}
if (signature_generated) {
out << YAML::Key << "signature";
out << YAML::Value << YAML::DoubleQuoted << signature_str;
}
if (!signature_generated) {
if (overload_config.is_member_function) {
out << YAML::Key << "target" << YAML::Value;
Expand Down
50 changes: 50 additions & 0 deletions env/env_yaml_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -545,12 +545,15 @@ std::vector<ParseFunctionTestCase> GetParseFunctionTestCases() {
.overload_configs =
{
Config::FunctionOverloadConfig{
.overload_id =
"google.protobuf.StringValue.isEmpty()",
.examples = {"''.isEmpty() // true"},
.is_member_function = true,
.parameters = {{.name = "string_wrapper"}},
.return_type = {.name = "bool"},
},
Config::FunctionOverloadConfig{
.overload_id = "list<~T>.isEmpty()",
.examples = {"[].isEmpty() // true",
"[1].isEmpty() // false"},
.is_member_function = true,
Expand Down Expand Up @@ -635,6 +638,7 @@ std::vector<ParseFunctionTestCase> GetParseFunctionTestCases() {
.overload_configs =
{
Config::FunctionOverloadConfig{
.overload_id = "contains(list<~T>, ~T)",
.examples = {"contains([1, 2, 3], 2) // true"},
.is_member_function = false,
.parameters =
Expand Down Expand Up @@ -1740,6 +1744,45 @@ std::vector<ExportTestCase> GetExportTestCases() {
- type_name: "int"
)yaml",
},
ExportTestCase{
.config = []() -> absl::StatusOr<Config> {
Config config;
CEL_RETURN_IF_ERROR(config.AddFunctionConfig(
{.name = "foo",
.overload_configs = {
{.overload_id = "timestamp.foo(A<~B>)",
.is_member_function = true,
.parameters = {{.name = "timestamp"},
{.name = "A",
.params = {{.name = "B",
.is_type_param = true}}}},
.return_type = {.name = "int"}},
}}));
return config;
}(),
.expected_yaml = R"yaml(
functions:
- name: "foo"
overloads:
- signature: "timestamp.foo(A<~B>)"
return: "int"
)yaml",
.expected_alt_yaml = R"yaml(
functions:
- name: "foo"
overloads:
- id: "timestamp.foo(A<~B>)"
target:
type_name: "timestamp"
args:
- type_name: "A"
params:
- type_name: "B"
is_type_param: true
return:
type_name: "int"
)yaml",
},
};
};

Expand Down Expand Up @@ -1888,6 +1931,13 @@ std::vector<std::string> GetSignatureRoundTripTestCases() {
signature: "foo(timestamp,A<~B>)"
return: "list<int>"
)yaml",
R"yaml(
functions:
- name: "foo"
overloads:
- signature: "timestamp.foo(A<~B>)"
return: "int"
)yaml",
};
}

Expand Down
Loading