1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.linecorp.centraldogma.internal.thrift;
18
19 import java.io.IOException;
20 import java.io.UncheckedIOException;
21
22 import javax.annotation.Nullable;
23
24 import com.fasterxml.jackson.core.JsonProcessingException;
25 import com.fasterxml.jackson.databind.JsonNode;
26
27 import com.linecorp.centraldogma.internal.Jackson;
28
29
30
31
32
33 public final class EntryConverter {
34
35 public static Entry convert(com.linecorp.centraldogma.common.Entry<?> entry) {
36 final Entry file = new Entry(entry.path(), convertEntryType(entry.type()));
37 switch (entry.type()) {
38 case JSON:
39
40 try {
41 file.setContent(Jackson.writeValueAsString(entry.content()));
42 } catch (JsonProcessingException e) {
43 throw new UncheckedIOException(e);
44 }
45 break;
46 case TEXT:
47 file.setContent((String) entry.content());
48 break;
49 case DIRECTORY:
50 break;
51 default:
52 throw new IllegalArgumentException("unsupported entry type: " + entry.type());
53 }
54 return file;
55 }
56
57 public static com.linecorp.centraldogma.common.Entry<?> convert(
58 com.linecorp.centraldogma.common.Revision revision, Entry entry) {
59 switch (entry.getType()) {
60 case JSON:
61 try {
62 final JsonNode value = Jackson.readTree(entry.getContent());
63 return com.linecorp.centraldogma.common.Entry.ofJson(revision, entry.getPath(), value);
64 } catch (IOException e) {
65 throw new UncheckedIOException(e);
66 }
67 case TEXT:
68 return com.linecorp.centraldogma.common.Entry.ofText(revision, entry.getPath(),
69 entry.getContent());
70 case DIRECTORY:
71 return com.linecorp.centraldogma.common.Entry.ofDirectory(revision, entry.getPath());
72 default:
73 throw new IllegalArgumentException("unsupported entry type: " + entry.getType());
74 }
75 }
76
77
78
79
80 @Nullable
81 public static EntryType convertEntryType(com.linecorp.centraldogma.common.EntryType type) {
82 if (type == null) {
83 return null;
84 }
85
86 switch (type) {
87 case JSON:
88 return EntryType.JSON;
89 case TEXT:
90 return EntryType.TEXT;
91 case DIRECTORY:
92 return EntryType.DIRECTORY;
93 }
94
95 throw new Error();
96 }
97
98
99
100
101 @Nullable
102 public static com.linecorp.centraldogma.common.EntryType convertEntryType(EntryType type) {
103 if (type == null) {
104 return null;
105 }
106
107 switch (type) {
108 case JSON:
109 return com.linecorp.centraldogma.common.EntryType.JSON;
110 case TEXT:
111 return com.linecorp.centraldogma.common.EntryType.TEXT;
112 case DIRECTORY:
113 return com.linecorp.centraldogma.common.EntryType.DIRECTORY;
114 }
115
116 throw new Error();
117 }
118
119 private EntryConverter() {}
120 }