Commit a399e18d authored by Sam Bowen's avatar Sam Bowen Committed by Commit Bot

Add JSON-LD id field to entity in schema.org extractor

Bug: 1065555
Change-Id: I45b4a8b788ea585f4fc6a655e435c9b54573b7d0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2127433
Commit-Queue: Sam Bowen <sgbowen@google.com>
Reviewed-by: default avatarBecca Hughes <beccahughes@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Cr-Commit-Position: refs/heads/master@{#755066}
parent dfc6142e
...@@ -36,6 +36,11 @@ struct Property { ...@@ -36,6 +36,11 @@ struct Property {
// Tree structure of entities is possible. // Tree structure of entities is possible.
// Ref: https://developers.google.com/schemas/formats/json-ld // Ref: https://developers.google.com/schemas/formats/json-ld
struct Entity { struct Entity {
string type; // Correspond to the "@type" key, defined in JSON-LD. // Corresponds to the "@type" key, defined in JSON-LD.
// See: https://w3c.github.io/json-ld-syntax/#specifying-the-type
string type;
// Corresponds to the "@id" key, defined in JSON-LD.
// See: https://w3c.github.io/json-ld-syntax/#node-identifiers
string id;
array<Property> properties; array<Property> properties;
}; };
...@@ -43,6 +43,7 @@ constexpr size_t kMaxNumFields = 25; ...@@ -43,6 +43,7 @@ constexpr size_t kMaxNumFields = 25;
constexpr size_t kMaxRepeatedSize = 100; constexpr size_t kMaxRepeatedSize = 100;
constexpr char kJSONLDKeyType[] = "@type"; constexpr char kJSONLDKeyType[] = "@type";
constexpr char kJSONLDKeyId[] = "@id";
using improved::mojom::Entity; using improved::mojom::Entity;
using improved::mojom::EntityPtr; using improved::mojom::EntityPtr;
...@@ -191,6 +192,11 @@ void ExtractEntity(const base::DictionaryValue& val, ...@@ -191,6 +192,11 @@ void ExtractEntity(const base::DictionaryValue& val,
} }
entity->type = type; entity->type = type;
std::string id;
if (val.GetString(kJSONLDKeyId, &id)) {
entity->id = id;
}
for (const auto& entry : val.DictItems()) { for (const auto& entry : val.DictItems()) {
if (entity->properties.size() >= kMaxNumFields) { if (entity->properties.size() >= kMaxNumFields) {
break; break;
......
...@@ -133,12 +133,13 @@ TEST_F(SchemaOrgExtractorTest, Empty) { ...@@ -133,12 +133,13 @@ TEST_F(SchemaOrgExtractorTest, Empty) {
} }
TEST_F(SchemaOrgExtractorTest, Basic) { TEST_F(SchemaOrgExtractorTest, Basic) {
EntityPtr extracted = EntityPtr extracted = Extract(
Extract("{\"@type\": \"VideoObject\", \"name\": \"a video!\"}"); "{\"@type\": \"VideoObject\", \"@id\": \"1\", \"name\": \"a video!\"}");
ASSERT_FALSE(extracted.is_null()); ASSERT_FALSE(extracted.is_null());
EntityPtr expected = Entity::New(); EntityPtr expected = Entity::New();
expected->type = "VideoObject"; expected->type = "VideoObject";
expected->id = "1";
expected->properties.push_back(CreateStringProperty("name", "a video!")); expected->properties.push_back(CreateStringProperty("name", "a video!"));
EXPECT_EQ(expected, extracted); EXPECT_EQ(expected, extracted);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment