Search through config for schema of specified flatbuffer type

Allow user to call function that returns a schema reflection in
the config for flatbuffer type instead of having to find a
matching schema manually.

Signed-off-by: Nathan Leong <100028864@mvla.net>
Change-Id: I40e836e8f379ac48f32633d49d8c01a1530697df
Signed-off-by: Nathan Leong <100028864@mvla.net>
diff --git a/aos/configuration.cc b/aos/configuration.cc
index d5ac2a1..ad6fb54 100644
--- a/aos/configuration.cc
+++ b/aos/configuration.cc
@@ -68,7 +68,6 @@
 }
 
 }  // namespace
-
 // Define the compare and equal operators for Channel and Application so we can
 // insert them in the btree below.
 bool operator<(const FlatbufferDetachedBuffer<Channel> &lhs,
@@ -1594,5 +1593,30 @@
   return channel->num_readers() + channel->num_senders();
 }
 
+// Searches through configurations for schemas that include a certain type
+const reflection::Schema *GetSchema(const Configuration *config,
+                                    std::string_view schema_type) {
+  if (config->has_channels()) {
+    std::vector<flatbuffers::Offset<Channel>> channel_offsets;
+    for (const Channel *c : *config->channels()) {
+      if (schema_type == c->type()->string_view()) {
+        return c->schema();
+      }
+    }
+  }
+  return nullptr;
+}
+
+// Copy schema reflection into detached flatbuffer
+std::optional<FlatbufferDetachedBuffer<reflection::Schema>>
+GetSchemaDetachedBuffer(const Configuration *config,
+                        std::string_view schema_type) {
+  const reflection::Schema *found_schema = GetSchema(config, schema_type);
+  if (found_schema == nullptr) {
+    return std::nullopt;
+  }
+  return RecursiveCopyFlatBuffer(found_schema);
+}
+
 }  // namespace configuration
 }  // namespace aos