Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add better YAML supports #616

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@
import com.linecorp.centraldogma.common.Revision;
import com.linecorp.centraldogma.common.RevisionNotFoundException;
import com.linecorp.centraldogma.common.ShuttingDownException;
import com.linecorp.centraldogma.internal.Jackson;
import com.linecorp.centraldogma.internal.Util;
import com.linecorp.centraldogma.internal.jackson.Jackson;
import com.linecorp.centraldogma.internal.thrift.AuthorConverter;
import com.linecorp.centraldogma.internal.thrift.CentralDogmaService;
import com.linecorp.centraldogma.internal.thrift.ChangeConverter;
Expand Down Expand Up @@ -241,11 +241,11 @@ private static <T> Entry<T> toEntry(Query<T> query, Revision normRev, QueryType
return entryAsText(query, normRev, content);
case IDENTITY_JSON:
case JSON_PATH:
if (receivedEntryType != com.linecorp.centraldogma.internal.thrift.EntryType.JSON) {
throw new CentralDogmaException("invalid entry type. entry type: " + receivedEntryType +
" (expected: " + queryType + ')');
}
validateEntryType(receivedEntryType, com.linecorp.centraldogma.internal.thrift.EntryType.JSON);
return entryAsJson(query, normRev, content);
case IDENTITY_YAML:
validateEntryType(receivedEntryType, com.linecorp.centraldogma.internal.thrift.EntryType.YAML);
return entryAsYaml(query, normRev, content);
case IDENTITY:
switch (receivedEntryType) {
case JSON:
Expand All @@ -254,14 +254,32 @@ private static <T> Entry<T> toEntry(Query<T> query, Revision normRev, QueryType
return entryAsText(query, normRev, content);
case DIRECTORY:
return unsafeCast(Entry.ofDirectory(normRev, query.path()));
case YAML:
return entryAsYaml(query, normRev, content);
}
}
throw new Error(); // Should never reach here.
}

private static void validateEntryType(com.linecorp.centraldogma.internal.thrift.EntryType actual,
com.linecorp.centraldogma.internal.thrift.EntryType expected) {
if (actual != expected) {
throw new CentralDogmaException("invalid entry type. entry type: " + actual +
" (expected: " + expected + ')');
}
}

private static <T> Entry<T> entryAsJson(Query<T> query, Revision normRev, String content) {
try {
return unsafeCast(Entry.ofJson(normRev, query.path(), Jackson.readTree(content)));
return unsafeCast(Entry.ofJson(normRev, query.path(), Jackson.ofJson().readTree(content)));
} catch (IOException e) {
throw new CentralDogmaException("failed to parse the query result: " + query, e);
}
}

private static <T> Entry<T> entryAsYaml(Query<T> query, Revision normRev, String content) {
try {
return unsafeCast(Entry.ofYaml(normRev, query.path(), Jackson.ofYaml().readTree(content)));
} catch (IOException e) {
throw new CentralDogmaException("failed to parse the query result: " + query, e);
}
Expand Down Expand Up @@ -305,11 +323,12 @@ public <T> CompletableFuture<MergedEntry<T>> mergeFiles(String projectName, Stri
assert entryType != null;
switch (entryType) {
case JSON:
case YAML:
try {
@SuppressWarnings("unchecked")
final MergedEntry<T> converted = (MergedEntry<T>) MergedEntry.of(
RevisionConverter.TO_MODEL.convert(entry.revision),
entryType, Jackson.readTree(entry.content), entry.paths);
entryType, Jackson.of(entryType).readTree(entry.content), entry.paths);
return converted;
} catch (IOException e) {
throw new CentralDogmaException(
Expand Down Expand Up @@ -377,6 +396,12 @@ public <T> CompletableFuture<Change<T>> getDiff(String projectName, String repos
case APPLY_TEXT_PATCH:
converted = unsafeCast(Change.ofTextPatch(query.path(), r.getContent()));
break;
case UPSERT_YAML:
converted = unsafeCast(Change.ofYamlUpsert(query.path(), r.getContent()));
break;
case APPLY_YAML_PATCH:
converted = unsafeCast(Change.ofYamlPatch(query.path(), r.getContent()));
break;
default:
throw new Error("unknown change type: " + r.getType());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
import com.linecorp.centraldogma.common.PushResult;
import com.linecorp.centraldogma.common.Query;
import com.linecorp.centraldogma.common.Revision;
import com.linecorp.centraldogma.internal.Jackson;
import com.linecorp.centraldogma.internal.jackson.Jackson;
import com.linecorp.centraldogma.internal.thrift.CentralDogmaService;
import com.linecorp.centraldogma.internal.thrift.ChangeType;
import com.linecorp.centraldogma.internal.thrift.Comment;
Expand Down Expand Up @@ -375,7 +375,7 @@ void mergeFiles() throws Exception {
MergeSource.ofRequired("/b.json"))))
.get())
.isEqualTo(com.linecorp.centraldogma.common.MergedEntry.of(
new Revision(1), EntryType.JSON, Jackson.readTree("{\"foo\": \"bar\"}"),
new Revision(1), EntryType.JSON, Jackson.ofJson().readTree("{\"foo\": \"bar\"}"),
ImmutableList.of("/a.json", "/b.json")));
verify(iface).mergeFiles(eq("project"), eq("repo"), any(), any(), any());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@
import com.linecorp.centraldogma.common.Revision;
import com.linecorp.centraldogma.common.RevisionNotFoundException;
import com.linecorp.centraldogma.common.ShuttingDownException;
import com.linecorp.centraldogma.internal.Jackson;
import com.linecorp.centraldogma.internal.Util;
import com.linecorp.centraldogma.internal.api.v1.WatchTimeout;
import com.linecorp.centraldogma.internal.jackson.Jackson;

final class ArmeriaCentralDogma extends AbstractCentralDogma {

Expand Down Expand Up @@ -961,7 +961,7 @@ private static String encodeParam(String param) {
*/
private static byte[] toBytes(JsonNode content) {
try {
return Jackson.writeValueAsBytes(content);
return Jackson.ofJson().writeValueAsBytes(content);
} catch (JsonProcessingException e) {
// Should never reach here.
throw new Error(e);
Expand Down Expand Up @@ -995,7 +995,7 @@ private static JsonNode toJson(AggregatedHttpResponse res, @Nullable JsonNodeTyp
final String content = toString(res);
final JsonNode node;
try {
node = Jackson.readTree(content);
node = Jackson.ofJson().readTree(content);
} catch (JsonParseException e) {
throw new CentralDogmaException("failed to parse the response JSON", e);
}
Expand Down Expand Up @@ -1028,15 +1028,17 @@ private static <T> Entry<T> toEntry(Revision revision, JsonNode node, QueryType
return entryAsText(revision, node, entryPath);
case IDENTITY_JSON:
case JSON_PATH:
if (receivedEntryType != EntryType.JSON) {
throw new CentralDogmaException("invalid entry type. entry type: " + receivedEntryType +
" (expected: " + queryType + ')');
}
validateEntryType(receivedEntryType, EntryType.JSON);
return entryAsJson(revision, node, entryPath);
case IDENTITY_YAML:
validateEntryType(receivedEntryType, EntryType.YAML);
return entryAsYaml(revision, node, entryPath);
case IDENTITY:
switch (receivedEntryType) {
case JSON:
return entryAsJson(revision, node, entryPath);
case YAML:
return entryAsYaml(revision, node, entryPath);
case TEXT:
return entryAsText(revision, node, entryPath);
case DIRECTORY:
Expand All @@ -1050,7 +1052,12 @@ private static <T> Entry<T> entryAsText(Revision revision, JsonNode node, String
final JsonNode content = getField(node, "content");
final String content0;
if (content.isContainerNode()) {
content0 = content.toString();
try {
content0 = Jackson.of(EntryType.guessFromPath(entryPath)).writeValueAsString(content);
} catch (JsonProcessingException e) {
// Should never happen because it's a JSON or YAML tree already.
throw new Error(e);
}
} else {
content0 = content.asText();
}
Expand All @@ -1061,6 +1068,10 @@ private static <T> Entry<T> entryAsJson(Revision revision, JsonNode node, String
return unsafeCast(Entry.ofJson(revision, entryPath, getField(node, "content")));
}

private static <T> Entry<T> entryAsYaml(Revision revision, JsonNode node, String entryPath) {
return unsafeCast(Entry.ofYaml(revision, entryPath, getField(node, "content")));
}

private static Commit toCommit(JsonNode node) {
final Revision revision = new Revision(getField(node, "revision").asInt());
final JsonNode authorNode = getField(node, "author");
Expand All @@ -1081,6 +1092,8 @@ private static <T> Change<T> toChange(JsonNode node) {
switch (type) {
case UPSERT_JSON:
return unsafeCast(Change.ofJsonUpsert(actualPath, getField(node, "content")));
case UPSERT_YAML:
return unsafeCast(Change.ofYamlUpsert(actualPath, getField(node, "content").asText()));
case UPSERT_TEXT:
return unsafeCast(Change.ofTextUpsert(actualPath, getField(node, "content").asText()));
case REMOVE:
Expand All @@ -1089,6 +1102,8 @@ private static <T> Change<T> toChange(JsonNode node) {
return unsafeCast(Change.ofRename(actualPath, getField(node, "content").asText()));
case APPLY_JSON_PATCH:
return unsafeCast(Change.ofJsonPatch(actualPath, getField(node, "content")));
case APPLY_YAML_PATCH:
return unsafeCast(Change.ofYamlPatch(actualPath, getField(node, "content")));
case APPLY_TEXT_PATCH:
return unsafeCast(Change.ofTextPatch(actualPath, getField(node, "content").asText()));
}
Expand Down Expand Up @@ -1138,4 +1153,11 @@ private static <T> T handleErrorResponse(AggregatedHttpResponse res) {

throw new CentralDogmaException("unexpected response: " + res.headers() + ", " + res.contentUtf8());
}

private static void validateEntryType(EntryType actual, EntryType expected) {
if (actual != expected) {
throw new CentralDogmaException("invalid entry type. entry type: " + actual +
" (expected: " + expected + ')');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.google.common.collect.ImmutableSet;

import com.linecorp.centraldogma.internal.Jackson;
import com.linecorp.centraldogma.internal.jackson.Jackson;

final class ClientProfile {

Expand Down Expand Up @@ -66,7 +66,7 @@ Set<Entry> hosts() {
@Override
public String toString() {
try {
return Jackson.writeValueAsPrettyString(this);
return Jackson.ofJson().writeValueAsPrettyString(this);
} catch (JsonProcessingException e) {
// Should never reach here.
throw new Error(e);
Expand Down Expand Up @@ -108,7 +108,7 @@ int port() {
@Override
public String toString() {
try {
return Jackson.writeValueAsPrettyString(this);
return Jackson.ofJson().writeValueAsPrettyString(this);
} catch (JsonProcessingException e) {
// Should never reach here.
throw new Error(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import com.google.common.base.Converter;

import com.linecorp.centraldogma.common.ChangeFormatException;
import com.linecorp.centraldogma.internal.Jackson;
import com.linecorp.centraldogma.internal.jackson.Jackson;

/**
* Provides a function converting back and forth between {@link Change} and
Expand All @@ -43,8 +43,9 @@ protected Change doForward(com.linecorp.centraldogma.common.Change<?> value) {
switch (change.getType()) {
case UPSERT_JSON:
case APPLY_JSON_PATCH:
case APPLY_YAML_PATCH:
try {
change.setContent(Jackson.writeValueAsString(value.content()));
change.setContent(Jackson.ofJson().writeValueAsString(value.content()));
} catch (JsonProcessingException e) {
throw new ChangeFormatException("failed to read a JSON tree", e);
}
Expand All @@ -56,6 +57,13 @@ protected Change doForward(com.linecorp.centraldogma.common.Change<?> value) {
break;
case REMOVE:
break;
case UPSERT_YAML:
try {
change.setContent(Jackson.ofYaml().writeValueAsString(value.content()));
} catch (JsonProcessingException e) {
throw new ChangeFormatException("failed to read a YAML tree", e);
}
break;
}
return change;
}
Expand All @@ -78,6 +86,12 @@ protected com.linecorp.centraldogma.common.Change<?> doBackward(Change c) {
case APPLY_TEXT_PATCH:
return com.linecorp.centraldogma.common.Change.ofTextPatch(c.getPath(),
c.getContent());
case UPSERT_YAML:
return com.linecorp.centraldogma.common.Change.ofYamlUpsert(c.getPath(),
c.getContent());
case APPLY_YAML_PATCH:
return com.linecorp.centraldogma.common.Change.ofYamlPatch(c.getPath(),
c.getContent());
}

throw new Error();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;

import com.linecorp.centraldogma.internal.Jackson;
import com.linecorp.centraldogma.internal.jackson.Jackson;

/**
* Provides a function converting back and forth between {@link Entry} and
Expand All @@ -36,9 +36,10 @@ public static Entry convert(com.linecorp.centraldogma.common.Entry<?> entry) {
final Entry file = new Entry(entry.path(), convertEntryType(entry.type()));
switch (entry.type()) {
case JSON:
case YAML:
// FIXME(trustin): Inefficiency
try {
file.setContent(Jackson.writeValueAsString(entry.content()));
file.setContent(Jackson.of(entry.type()).writeValueAsString(entry.content()));
} catch (JsonProcessingException e) {
throw new UncheckedIOException(e);
}
Expand All @@ -59,7 +60,7 @@ public static com.linecorp.centraldogma.common.Entry<?> convert(
switch (entry.getType()) {
case JSON:
try {
final JsonNode value = Jackson.readTree(entry.getContent());
final JsonNode value = Jackson.ofJson().readTree(entry.getContent());
return com.linecorp.centraldogma.common.Entry.ofJson(revision, entry.getPath(), value);
} catch (IOException e) {
throw new UncheckedIOException(e);
Expand All @@ -69,6 +70,13 @@ public static com.linecorp.centraldogma.common.Entry<?> convert(
entry.getContent());
case DIRECTORY:
return com.linecorp.centraldogma.common.Entry.ofDirectory(revision, entry.getPath());
case YAML:
try {
final JsonNode value = Jackson.ofYaml().readTree(entry.getContent());
return com.linecorp.centraldogma.common.Entry.ofYaml(revision, entry.getPath(), value);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
default:
throw new IllegalArgumentException("unsupported entry type: " + entry.getType());
}
Expand All @@ -86,6 +94,8 @@ public static EntryType convertEntryType(com.linecorp.centraldogma.common.EntryT
switch (type) {
case JSON:
return EntryType.JSON;
case YAML:
return EntryType.YAML;
case TEXT:
return EntryType.TEXT;
case DIRECTORY:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ protected Query doForward(com.linecorp.centraldogma.common.Query<?> query) {
return new Query(query.path(), QueryType.IDENTITY_JSON, Collections.emptyList());
case JSON_PATH:
return new Query(query.path(), QueryType.JSON_PATH, query.expressions());
case IDENTITY_YAML:
return new Query(query.path(), QueryType.IDENTITY_YAML, Collections.emptyList());
}

throw new Error();
Expand All @@ -59,6 +61,8 @@ protected com.linecorp.centraldogma.common.Query<?> doBackward(Query query) {
return com.linecorp.centraldogma.common.Query.ofText(query.getPath());
case IDENTITY_JSON:
return com.linecorp.centraldogma.common.Query.ofJson(query.getPath());
case IDENTITY_YAML:
return com.linecorp.centraldogma.common.Query.ofYaml(query.getPath());
case JSON_PATH:
return com.linecorp.centraldogma.common.Query.ofJsonPath(query.getPath(),
query.getExpressions());
Expand Down
4 changes: 4 additions & 0 deletions common-legacy/src/main/thrift/CentralDogma.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ enum EntryType {
JSON = 1,
TEXT = 2,
DIRECTORY = 3,
YAML = 4,
}

enum ChangeType {
Expand All @@ -56,6 +57,8 @@ enum ChangeType {
RENAME = 4,
APPLY_JSON_PATCH = 5,
APPLY_TEXT_PATCH = 6,
UPSERT_YAML = 7,
APPLY_YAML_PATCH = 8,
}

enum PropertyType {
Expand Down Expand Up @@ -192,6 +195,7 @@ enum QueryType {
JSON_PATH = 2,
IDENTITY_TEXT = 3,
IDENTITY_JSON = 4,
IDENTITY_YAML = 5,
}

struct Query {
Expand Down
3 changes: 3 additions & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ dependencies {
// Jackson JSR310
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310'

// Jackson YAML
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml'

// Guava
implementation 'com.google.guava:guava'

Expand Down
Loading