View Javadoc
1   /*
2    * Copyright (c) 2001-2017, Zoltan Farkas All Rights Reserved.
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17   *
18   * Additionally licensed with:
19   *
20   * Licensed under the Apache License, Version 2.0 (the "License");
21   * you may not use this file except in compliance with the License.
22   * You may obtain a copy of the License at
23   *
24   *      http://www.apache.org/licenses/LICENSE-2.0
25   *
26   * Unless required by applicable law or agreed to in writing, software
27   * distributed under the License is distributed on an "AS IS" BASIS,
28   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29   * See the License for the specific language governing permissions and
30   * limitations under the License.
31   */
32  package org.spf4j.avro;
33  
34  
35  import com.fasterxml.jackson.core.JsonGenerator;
36  import com.fasterxml.jackson.databind.JsonNode;
37  import java.io.IOException;
38  import java.util.function.Function;
39  import javax.annotation.Nonnull;
40  import javax.annotation.Nullable;
41  import org.apache.avro.Schema;
42  
43  /**
44   * @author Zoltan Farkas
45   */
46  public interface SchemaResolver {
47  
48    /**
49     * Lower level resolver implementation, that will write a schema in a "custom way"
50     * @param schema
51     * @param gen
52     * @return
53     * @throws IOException
54     */
55    default boolean customWrite(Schema schema, JsonGenerator gen) throws IOException {
56      String ref = getId(schema);
57      if (ref != null) {
58          gen.writeStartObject();
59          gen.writeFieldName(getJsonAttrName());
60          gen.writeString(ref);
61          gen.writeEndObject();
62          return true;
63      } else {
64        return false;
65      }
66    }
67  
68    /**
69     * Lowere level resolver implementation that will read a schema from a "custom way"
70     * @param object
71     * @return the resolved schema, or null if nothing to resolve.
72     */
73    @Nullable
74    default Schema customRead(Function<String, JsonNode> object) {
75      JsonNode refVal = object.apply(getJsonAttrName());
76      if (refVal != null) {
77        return resolveSchema(refVal.asText());
78      } else {
79        return null;
80      }
81    }
82  
83    SchemaResolver NONE = new SchemaResolver() {
84      @Override
85      public boolean customWrite(final Schema schema, final JsonGenerator gen) {
86        return false;
87      }
88  
89      @Override
90      public Schema customRead(final Function<String, JsonNode> object) {
91        return null;
92      }
93  
94      @Override
95      public Schema resolveSchema(final String id) {
96        throw new UnsupportedOperationException();
97      }
98  
99      @Override
100     public String getId(final Schema schema) {
101       return null;
102     }
103   };
104 
105   default String getJsonAttrName() {
106     return "$ref";
107   }
108 
109   @Nonnull
110   Schema resolveSchema(String id);
111 
112   @Nullable
113   String getId(Schema schema);
114 
115 
116 }