diff --git a/mediapipe/framework/testdata/BUILD b/mediapipe/framework/testdata/BUILD index 8720e39ee..93e416eaa 100644 --- a/mediapipe/framework/testdata/BUILD +++ b/mediapipe/framework/testdata/BUILD @@ -35,6 +35,12 @@ mediapipe_proto_library( ], ) +mediapipe_proto_library( + name = "proto3_options_proto", + srcs = ["proto3_options.proto"], + visibility = ["//visibility:public"], +) + mediapipe_proto_library( name = "zoo_mutator_proto", srcs = ["zoo_mutator.proto"], diff --git a/mediapipe/framework/testdata/proto3_options.proto b/mediapipe/framework/testdata/proto3_options.proto new file mode 100644 index 000000000..c76894819 --- /dev/null +++ b/mediapipe/framework/testdata/proto3_options.proto @@ -0,0 +1,25 @@ +// Copyright 2023 The MediaPipe Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// Forked from mediapipe/framework/tool/source.proto. +// The forked proto must remain identical to the original proto and should be +// ONLY used by mediapipe open source project. + +syntax = "proto3"; + +package mediapipe; + +message Proto3Options { + double test_value = 1; +} diff --git a/mediapipe/framework/tool/BUILD b/mediapipe/framework/tool/BUILD index b13dba9b9..77e3ab16d 100644 --- a/mediapipe/framework/tool/BUILD +++ b/mediapipe/framework/tool/BUILD @@ -192,9 +192,8 @@ cc_test( "//mediapipe/framework:calculator_framework", "//mediapipe/framework/port:gtest_main", "//mediapipe/framework/port:parse_text_proto", - "//mediapipe/framework/port:status", "//mediapipe/framework/testdata:night_light_calculator_cc_proto", - "//mediapipe/framework/testdata:night_light_calculator_options_lib", + "//mediapipe/framework/testdata:proto3_options_cc_proto", ], ) diff --git a/mediapipe/framework/tool/options_map.h b/mediapipe/framework/tool/options_map.h index 2b69f4fb6..4950669c6 100644 --- a/mediapipe/framework/tool/options_map.h +++ b/mediapipe/framework/tool/options_map.h @@ -128,7 +128,8 @@ class OptionsMap { return *options_.Get(); } T* result = options_.Get(); - if (node_config_->has_options()) { + if (node_config_->has_options() && + HasExtension(node_config_->options())) { GetExtension(node_config_->options(), result); } else { GetNodeOptions(*node_config_, result); @@ -141,8 +142,9 @@ class OptionsMap { if (options_.Has()) { return true; } - if (node_config_->has_options()) { - return HasExtension(node_config_->options()); + if (node_config_->has_options() && + HasExtension(node_config_->options())) { + return true; } #if defined(MEDIAPIPE_PROTO_LITE) && defined(MEDIAPIPE_PROTO_THIRD_PARTY) // protobuf::Any is unavailable with third_party/protobuf:protobuf-lite. @@ -170,7 +172,8 @@ class MutableOptionsMap : public OptionsMap { template void Set(const T& value) const { *options_.Get() = value; - if (node_config_->has_options()) { + if (node_config_->has_options() && + HasExtension(node_config_->options())) { *GetExtension(*node_config_->mutable_options()) = value; } else { SetNodeOptions(*node_config_, value); @@ -182,7 +185,8 @@ class MutableOptionsMap : public OptionsMap { if (options_.Has()) { return options_.Get(); } - if (node_config_->has_options()) { + if (node_config_->has_options() && + HasExtension(node_config_->options())) { return GetExtension(*node_config_->mutable_options()); } T* result = options_.Get(); diff --git a/mediapipe/framework/tool/options_map_test.cc b/mediapipe/framework/tool/options_map_test.cc index 529fd5770..8efd1cb94 100644 --- a/mediapipe/framework/tool/options_map_test.cc +++ b/mediapipe/framework/tool/options_map_test.cc @@ -17,14 +17,11 @@ #include -#include - #include "mediapipe/framework/calculator_framework.h" #include "mediapipe/framework/port/gtest.h" #include "mediapipe/framework/port/parse_text_proto.h" -#include "mediapipe/framework/port/status.h" -#include "mediapipe/framework/port/status_macros.h" #include "mediapipe/framework/testdata/night_light_calculator.pb.h" +#include "mediapipe/framework/testdata/proto3_options.pb.h" namespace mediapipe { namespace tool { @@ -40,9 +37,10 @@ TEST(OptionsMapTest, QueryNotFound) { OptionsMap options; options.Initialize(node); EXPECT_FALSE(options.Has()); + EXPECT_FALSE(options.Has()); } -TEST(OptionsMapTest, QueryFound) { +TEST(OptionsMapTest, Proto2QueryFound) { CalculatorGraphConfig::Node node = ParseTextProtoOrDie(R"pb( calculator: "NightLightCalculator" @@ -64,7 +62,7 @@ TEST(OptionsMapTest, QueryFound) { 123); } -TEST(MutableOptionsMapTest, InsertAndQueryFound) { +TEST(MutableOptionsMapTest, InsertProto2AndQueryFound) { CalculatorGraphConfig::Node node = ParseTextProtoOrDie(R"pb( calculator: "NightLightCalculator" @@ -83,6 +81,83 @@ TEST(MutableOptionsMapTest, InsertAndQueryFound) { 123); } +TEST(OptionsMapTest, Proto3QueryFound) { + CalculatorGraphConfig::Node node = + ParseTextProtoOrDie(R"pb( + calculator: "NightLightCalculator" + input_side_packet: "input_value" + output_stream: "values" + node_options { + [type.googleapis.com/mediapipe.Proto3Options] { test_value: 123 } + } + )pb"); + OptionsMap options; + options.Initialize(node); + EXPECT_TRUE(options.Has()); + EXPECT_EQ(options.Get().test_value(), 123); +} + +TEST(MutableOptionsMapTest, InsertProto3AndQueryFound) { + CalculatorGraphConfig::Node node = + ParseTextProtoOrDie(R"pb( + calculator: "NightLightCalculator" + input_side_packet: "input_value" + output_stream: "values" + )pb"); + MutableOptionsMap options; + options.Initialize(node); + EXPECT_FALSE(options.Has()); + mediapipe::Proto3Options proto3_options; + proto3_options.set_test_value(123); + options.Set(proto3_options); + EXPECT_TRUE(options.Has()); + EXPECT_EQ(options.Get().test_value(), 123); +} + +TEST(OptionsMapTest, BothProto2AndProto3QueriesFound) { + CalculatorGraphConfig::Node node = + ParseTextProtoOrDie(R"pb( + calculator: "NightLightCalculator" + input_side_packet: "input_value" + output_stream: "values" + options { + [mediapipe.NightLightCalculatorOptions.ext] { jitter: 321 } + } + node_options { + [type.googleapis.com/mediapipe.Proto3Options] { test_value: 123 } + } + )pb"); + OptionsMap options; + options.Initialize(node); + EXPECT_TRUE(options.Has()); + EXPECT_EQ(options.Get().test_value(), 123); + EXPECT_TRUE(options.Has()); + EXPECT_EQ(options.Get().jitter(), + 321); +} + +TEST(OptionsMapTest, PrefersOptionsOverNodeOptions) { + CalculatorGraphConfig::Node node = + ParseTextProtoOrDie(R"pb( + calculator: "NightLightCalculator" + input_side_packet: "input_value" + output_stream: "values" + options { + [mediapipe.NightLightCalculatorOptions.ext] { jitter: 111 } + } + node_options { + [type.googleapis.com/mediapipe.NightLightCalculatorOptions] { + jitter: 222 + } + } + )pb"); + OptionsMap options; + options.Initialize(node); + EXPECT_TRUE(options.Has()); + EXPECT_EQ(options.Get().jitter(), + 111); +} + } // namespace } // namespace tool } // namespace mediapipe