1   /*
2    * Copyright 2017 LINE Corporation
3    *
4    * LINE Corporation licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
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   * Provides a function converting back and forth between {@link Entry} and
31   * {@link com.linecorp.centraldogma.common.Entry}.
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                  // FIXME(trustin): Inefficiency
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       * Converts {@link com.linecorp.centraldogma.common.EntryType} to {@link EntryType}.
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       * Converts {@link EntryType} to {@link com.linecorp.centraldogma.common.EntryType}.
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 }