Flutter Engine
 
Loading...
Searching...
No Matches
catalog.h
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef FLUTTER_TOOLS_LICENSES_CPP_SRC_CATALOG_H_
6#define FLUTTER_TOOLS_LICENSES_CPP_SRC_CATALOG_H_
7
8#include "flutter/third_party/re2/re2/re2.h"
9#include "flutter/third_party/re2/re2/set.h"
10#include "third_party/abseil-cpp/absl/container/flat_hash_map.h"
11#include "third_party/abseil-cpp/absl/status/statusor.h"
12
13#include <iosfwd>
14#include <optional>
15
16/// A storage of licenses that can be matched against.
17/// The in memory representation of the `data/headers` and `data/licenses`
18/// directories. This represents a 2 tiered search, first the sector is used
19/// to determine what matcher should be used, then a match is performend on
20/// that. This approach was chosen to minimize the size of the RE2::Set.
21class Catalog {
22 public:
23 /// VisibleForTesting
24 struct Entry {
25 std::string name;
26 std::string unique;
27 std::string matcher;
28 };
29
30 class Match {
31 public:
32 static Match MakeWithString(std::string_view matcher,
33 std::string matched_text) {
34 return Match(matcher, std::move(matched_text));
35 }
36
37 static Match MakeWithView(std::string_view matcher,
38 std::string_view matched_text) {
39 return Match(matcher, matched_text);
40 }
41
42 std::string_view GetMatcher() const { return matcher_; }
43 std::string_view GetMatchedText() const {
44 if (matched_text_.empty()) {
45 return owned_matched_text_;
46 } else {
47 return matched_text_;
48 }
49 }
50
51 private:
52 Match(std::string_view matcher, std::string_view matched_text)
53 : matcher_(matcher), matched_text_(matched_text) {}
54 Match(std::string_view matcher, std::string matched_text)
55 : matcher_(matcher), owned_matched_text_(std::move(matched_text)) {}
56
57 std::string_view matcher_;
58 std::string_view matched_text_;
59 std::string owned_matched_text_;
60 };
61
62 static absl::StatusOr<Catalog> Open(std::string_view data_dir);
63
64 /// Make a Catalog for testing.
65 static absl::StatusOr<Catalog> Make(const std::vector<Entry>& entries);
66
67 /// @brief Tries to identify a match for the `query` across the `Catalog`.
68 /// @param query The text that will be matched against.
69 /// @return absl::StatusCode::kNotFound when a match can't be found.
70 /// absl::StatusCode::kInvalidArgument if more than one match comes up from
71 /// the selector.
72 absl::StatusOr<std::vector<Match>> FindMatch(std::string_view query) const;
73
74 /// VisibleForTesting
75 static absl::StatusOr<Entry> ParseEntry(std::istream& is);
76
77 private:
78 explicit Catalog(RE2::Set selector,
79 std::vector<std::unique_ptr<RE2>> matchers,
80 std::vector<std::string> names);
81 RE2::Set selector_;
82 std::vector<std::unique_ptr<RE2>> matchers_;
83 std::vector<std::string> names_;
84};
85
86#endif // FLUTTER_TOOLS_LICENSES_CPP_SRC_CATALOG_H_
static Match MakeWithView(std::string_view matcher, std::string_view matched_text)
Definition catalog.h:37
std::string_view GetMatcher() const
Definition catalog.h:42
std::string_view GetMatchedText() const
Definition catalog.h:43
static Match MakeWithString(std::string_view matcher, std::string matched_text)
Definition catalog.h:32
static absl::StatusOr< Entry > ParseEntry(std::istream &is)
VisibleForTesting.
Definition catalog.cc:243
static absl::StatusOr< Catalog > Open(std::string_view data_dir)
Definition catalog.cc:98
absl::StatusOr< std::vector< Match > > FindMatch(std::string_view query) const
Tries to identify a match for the query across the Catalog.
Definition catalog.cc:183
static absl::StatusOr< Catalog > Make(const std::vector< Entry > &entries)
Make a Catalog for testing.
Definition catalog.cc:151
Definition ref_ptr.h:261
VisibleForTesting.
Definition catalog.h:24
std::string unique
Definition catalog.h:26
std::string name
Definition catalog.h:25
std::string matcher
Definition catalog.h:27