/**
  * Apply mapping for a single @Field annotation
  *
  * @param xContentBuilder
  * @param field
  * @param fieldAnnotation
  * @throws IOException
  */
 private static void addSingleFieldMapping(
     XContentBuilder xContentBuilder, java.lang.reflect.Field field, Field fieldAnnotation)
     throws IOException {
   xContentBuilder.startObject(field.getName());
   xContentBuilder.field(FIELD_STORE, fieldAnnotation.store());
   if (FieldType.Auto != fieldAnnotation.type()) {
     xContentBuilder.field(FIELD_TYPE, fieldAnnotation.type().name().toLowerCase());
     if (FieldType.Date == fieldAnnotation.type() && DateFormat.none != fieldAnnotation.format()) {
       xContentBuilder.field(
           FIELD_FORMAT,
           DateFormat.custom == fieldAnnotation.format()
               ? fieldAnnotation.pattern()
               : fieldAnnotation.format());
     }
   }
   if (FieldIndex.not_analyzed == fieldAnnotation.index()) {
     xContentBuilder.field(FIELD_INDEX, fieldAnnotation.index().name().toLowerCase());
   }
   if (isNotBlank(fieldAnnotation.searchAnalyzer())) {
     xContentBuilder.field(FIELD_SEARCH_ANALYZER, fieldAnnotation.searchAnalyzer());
   }
   if (isNotBlank(fieldAnnotation.indexAnalyzer())) {
     xContentBuilder.field(FIELD_INDEX_ANALYZER, fieldAnnotation.indexAnalyzer());
   }
   xContentBuilder.endObject();
 }
Exemplo n.º 2
0
  /** Returns an object of the same type as {@code o}, or null if it is not retained. */
  private Object retainAll(Schema schema, MarkSet markSet, ProtoType type, Object o) {
    if (!markSet.contains(type)) {
      return null; // Prune this type.

    } else if (o instanceof Map) {
      ImmutableMap.Builder<ProtoMember, Object> builder = ImmutableMap.builder();
      for (Map.Entry<?, ?> entry : ((Map<?, ?>) o).entrySet()) {
        ProtoMember protoMember = (ProtoMember) entry.getKey();
        if (!markSet.contains(protoMember)) continue; // Prune this field.
        Field field = schema.getField(protoMember);
        Object retainedValue = retainAll(schema, markSet, field.type(), entry.getValue());
        if (retainedValue != null) {
          builder.put(protoMember, retainedValue); // This retained field is non-empty.
        }
      }
      ImmutableMap<ProtoMember, Object> map = builder.build();
      return !map.isEmpty() ? map : null;

    } else if (o instanceof List) {
      ImmutableList.Builder<Object> builder = ImmutableList.builder();
      for (Object value : ((List) o)) {
        Object retainedValue = retainAll(schema, markSet, type, value);
        if (retainedValue != null) {
          builder.add(retainedValue); // This retained value is non-empty.
        }
      }
      ImmutableList<Object> list = builder.build();
      return !list.isEmpty() ? list : null;

    } else {
      return o;
    }
  }
Exemplo n.º 3
0
  /**
   * Parses a field type definition
   *
   * @param docField
   * @return
   */
  protected static Field ParseField(FieldDoc docField) {
    assert (docField != null);

    Field xmlField = new Field();

    xmlField.name = docField.name();
    xmlField.comment = docField.commentText();
    xmlField.type = ParseType(docField.type());
    xmlField.isFinal = docField.isFinal();
    if (xmlField.isFinal) {
      xmlField.finalExpression = docField.constantValueExpression();
    } else if (docField.constantValueExpression() != null) {
      // how would a non-final field have a constant value expression?
      // my understanding is that this field is only != null when is not final
      assert (false);
    }
    xmlField.isStatic = docField.isStatic();
    xmlField.isVolatile = docField.isVolatile();
    xmlField.isTransient = docField.isTransient();
    xmlField.scope = DetermineScope(docField);

    // parse annotations from the field
    xmlField.annotationInstances =
        ParseAnnotationInstances(docField.annotations(), docField.qualifiedName());

    return xmlField;
  }
Exemplo n.º 4
0
 @Test
 public void testNestedObjectParsing() throws Exception {
   Map value =
       new ObjectMapper().readValue(getClass().getResourceAsStream("nested.json"), Map.class);
   Field fl = Field.parseField(value);
   assertEquals("artiststimestamp", fl.name());
   Field[] properties = fl.properties();
   Field first = properties[0];
   assertEquals("date", first.name());
   assertEquals(FieldType.DATE, first.type());
   Field second = properties[1];
   assertEquals(FieldType.OBJECT, second.type());
   assertEquals("links", second.name());
   Field[] secondProps = second.properties();
   assertEquals("url", secondProps[0].name());
   assertEquals(FieldType.STRING, secondProps[0].type());
 }
Exemplo n.º 5
0
  @Test
  public void testFieldInclude() throws Exception {
    Map value =
        new ObjectMapper().readValue(getClass().getResourceAsStream("nested.json"), Map.class);
    Field fl = Field.parseField(value);

    Field filtered =
        MappingUtils.filter(fl, Collections.singleton("*a*e"), Collections.<String>emptyList());

    assertThat(fl.name(), is(filtered.name()));
    assertThat(fl.type(), is(filtered.type()));

    Field[] props = filtered.properties();

    assertThat(props.length, is(2));
    assertThat(props[0].name(), is("date"));
    assertThat(props[1].name(), is("name"));
  }
Exemplo n.º 6
0
  private Object canonicalizeValue(Linker linker, Field context, Object value) {
    if (value instanceof OptionElement) {
      ImmutableMap.Builder<ProtoMember, Object> result = ImmutableMap.builder();
      OptionElement option = (OptionElement) value;
      Field field = linker.dereference(context, option.name());
      if (field == null) {
        linker.addError("unable to resolve option %s on %s", option.name(), context.type());
      } else {
        ProtoMember protoMember = ProtoMember.get(context.type(), field);
        result.put(protoMember, canonicalizeValue(linker, field, option.value()));
      }
      return coerceValueForField(context, result.build());
    }

    if (value instanceof Map) {
      ImmutableMap.Builder<ProtoMember, Object> result = ImmutableMap.builder();
      for (Map.Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) {
        String name = (String) entry.getKey();
        Field field = linker.dereference(context, name);
        if (field == null) {
          linker.addError("unable to resolve option %s on %s", name, context.type());
        } else {
          ProtoMember protoMember = ProtoMember.get(context.type(), field);
          result.put(protoMember, canonicalizeValue(linker, field, entry.getValue()));
        }
      }
      return coerceValueForField(context, result.build());
    }

    if (value instanceof List) {
      ImmutableList.Builder<Object> result = ImmutableList.builder();
      for (Object element : (List<?>) value) {
        result.addAll((List) canonicalizeValue(linker, context, element));
      }
      return coerceValueForField(context, result.build());
    }

    if (value instanceof String) {
      return coerceValueForField(context, value);
    }

    throw new IllegalArgumentException("Unexpected option value: " + value);
  }
Exemplo n.º 7
0
 @Test
 public void testMultiFieldParsing() throws Exception {
   Map value =
       new ObjectMapper().readValue(getClass().getResourceAsStream("multi_field.json"), Map.class);
   Field fl = Field.parseField(value);
   assertEquals("tweet", fl.name());
   assertEquals(1, fl.properties().length);
   Field nested = fl.properties()[0];
   assertEquals("name", nested.name());
   assertEquals(FieldType.STRING, nested.type());
 }
Exemplo n.º 8
0
 @Test(expected = EsHadoopIllegalArgumentException.class)
 public void testMultiFieldWithoutDefaultFieldAndMultiTypesParsing() throws Exception {
   Map value =
       new ObjectMapper()
           .readValue(
               getClass().getResourceAsStream("multi_field_no_default_multi_types.json"),
               Map.class);
   Field fl = Field.parseField(value);
   assertEquals("tweet", fl.name());
   assertEquals(1, fl.properties().length);
   Field nested = fl.properties()[0];
   assertEquals("name", nested.name());
   assertEquals(FieldType.STRING, nested.type());
 }
Exemplo n.º 9
0
  Map<ProtoMember, Object> canonicalizeOption(
      Linker linker, ProtoType extensionType, OptionElement option) {
    Type type = linker.get(extensionType);
    if (!(type instanceof MessageType)) {
      return null; // No known extensions for the given extension type.
    }
    MessageType messageType = (MessageType) type;

    String[] path;
    Field field = messageType.field(option.name());
    if (field != null) {
      // This is an option declared by descriptor.proto.
      path = new String[] {option.name()};
    } else {
      // This is an option declared by an extension.
      Map<String, Field> extensionsForType = messageType.extensionFieldsMap();

      path = resolveFieldPath(option.name(), extensionsForType.keySet());
      String packageName = linker.packageName();
      if (path == null && packageName != null) {
        // If the path couldn't be resolved, attempt again by prefixing it with the package name.
        path = resolveFieldPath(packageName + "." + option.name(), extensionsForType.keySet());
      }
      if (path == null) {
        return null; // Unable to find the root of this field path.
      }

      field = extensionsForType.get(path[0]);
    }

    Map<ProtoMember, Object> result = new LinkedHashMap<>();
    Map<ProtoMember, Object> last = result;
    ProtoType lastProtoType = messageType.type();
    for (int i = 1; i < path.length; i++) {
      Map<ProtoMember, Object> nested = new LinkedHashMap<>();
      last.put(ProtoMember.get(lastProtoType, field), nested);
      lastProtoType = field.type();
      last = nested;
      field = linker.dereference(field, path[i]);
      if (field == null) {
        return null; // Unable to dereference this path segment.
      }
    }

    last.put(
        ProtoMember.get(lastProtoType, field), canonicalizeValue(linker, field, option.value()));
    return result;
  }
Exemplo n.º 10
0
  /** Build of output data rec for user procedures */
  static void generateUserOutputRecs(Table table, PrintWriter outData) {
    for (int i = 0; i < table.procs.size(); i++) {
      Proc proc = (Proc) table.procs.elementAt(i);
      if (proc.isData || proc.isStd || proc.hasNoData()) continue;
      if (proc.isStdExtended()) continue;
      String work = "(object)";
      String superName = "";
      if (proc.outputs.size() > 0) {
        for (int j = 0; j < proc.comments.size(); j++)
          outData.println("##" + (String) proc.comments.elementAt(j));
        String typeChar = "D";
        if (proc.hasDiscreteInput()) typeChar = "O";
        work = "(" + typeChar + table.useName() + proc.upperFirst() + ")";
        superName = typeChar + table.useName() + proc.upperFirst();
        outData.println("## \\class " + typeChar + table.useName() + proc.upperFirst());
        outData.println("class " + typeChar + table.useName() + proc.upperFirst() + "(object):");
        outData.println(
            "    def _make(self): return " + typeChar + table.useName() + proc.upperFirst() + "()");
        if (proc.hasDiscreteInput())
          outData.println(
              "    def _name(self): return ('"
                  + typeChar
                  + table.useName()
                  + proc.upperFirst()
                  + "')");
        else
          outData.println(
              "    def _name(self): return ('D"
                  + table.useName()
                  + proc.upperFirst()
                  + "', 'O"
                  + table.useName()
                  + proc.upperFirst()
                  + "')");
        generateDataFields(
            proc.outputs,
            "",
            typeChar + table.useName() + proc.upperFirst(),
            table.useName(),
            outData);
        outData.println();
      }
      if (proc.hasDiscreteInput()) {
        outData.println("## \\class D" + table.useName() + proc.upperFirst());
        outData.println("class D" + table.useName() + proc.upperFirst() + work + ":");
        outData.println(
            "    def _make(self): return D" + table.useName() + proc.upperFirst() + "()");
        outData.println(
            "    def _name(self): return ('D" + table.useName() + proc.upperFirst() + "')");
        Vector<Field> discreteInputs = new Vector<Field>();
        for (int j = 0; j < proc.inputs.size(); j++) {
          Field field = (Field) proc.inputs.elementAt(j);
          if (!proc.hasOutput(field.name)) discreteInputs.addElement(field);
        }

        for (int j = 0; j < proc.dynamics.size(); j++) {
          Field f = new Field();
          Integer length = (Integer) proc.dynamicSizes.elementAt(j);
          f.name = (String) proc.dynamics.elementAt(j);
          f.type = Field.CHAR;
          f.length = length.intValue();
          discreteInputs.addElement(f);
        }
        generateDataFields(
            discreteInputs,
            superName,
            "D" + table.useName() + proc.upperFirst(),
            table.useName(),
            outData);
        outData.println();
      } else if (proc.outputs.size() > 0) {
        outData.println("## \\class O" + table.useName() + proc.upperFirst());
        outData.println("## \\field see:O" + table.useName() + proc.upperFirst());
        outData.println(
            "O"
                + table.useName()
                + proc.upperFirst()
                + " = D"
                + table.useName()
                + proc.upperFirst());
        outData.println();
      }
      if (pymodName.length() > 0) {
        coverFunctions = new Vector<String>();
        outData.println(
            "class "
                + table.useName()
                + proc.upperFirst()
                + "(D"
                + table.useName()
                + proc.upperFirst()
                + "):");
        outData.println(
            "    def __init__(self): D" + table.useName() + proc.upperFirst() + ".__init__(self)");
        if (proc.isMultipleInput) generateBulkAction(table, proc, outData);
        else if (proc.isInsert && proc.hasReturning) generateAction(table, proc, outData);
        else if (proc.outputs.size() > 0)
          if (proc.isSingle) generateSingle(table, proc, outData);
          else generateMultiple(table, proc, outData);
        else generateAction(table, proc, outData);
        outData.println();
        for (int j = 0; j < coverFunctions.size(); j++)
          outData.println((String) coverFunctions.elementAt(j));
      }
    }
  }
Exemplo n.º 11
0
 private static void generateImplementationStructPairs(
     Proc proc, String mainName, PrintWriter outData) {
   if (proc.outputs.size() > 0) {
     outData.println("@implementation O" + mainName + "Rec");
     for (int i = 0; i < proc.outputs.size(); i++) {
       Field field = (Field) proc.outputs.elementAt(i);
       if (field.type == Field.BLOB) {
         outData.println("@synthesize " + useLowerName(field) + "_len;");
         outData.println("@synthesize " + useLowerName(field) + "_data;");
       } else outData.println("@synthesize " + useLowerName(field) + ";");
       if (isNull(field) == true)
         outData.println("@synthesize " + useLowerName(field) + "IsNull;");
     }
     outData.println("- (id)init");
     outData.println("  {");
     outData.println("    self = [super init];");
     outData.println("    return self;");
     outData.println("  }");
     outData.println("- (void)clear");
     outData.println("  {");
     for (int i = 0; i < proc.outputs.size(); i++) {
       Field field = (Field) proc.outputs.elementAt(i);
       clearCall(field, outData);
       if (isNull(field) == true) // && field.isEmptyOrAnsiAsNull() == false)
       outData.println("    self." + useLowerName(field) + "IsNull = NO;");
     }
     outData.println("  }");
     outData.println("- (void)write:(Writer*)writer");
     outData.println("  {");
     for (int i = 0; i < proc.outputs.size(); i++) {
       Field field = (Field) proc.outputs.elementAt(i);
       writeCall(field, outData);
       if (isNull(field) == true) // && field.isEmptyOrAnsiAsNull() == false)
       outData.println(
             "    [writer putBoolean:self." + useLowerName(field) + "IsNull];[writer filler:6];");
     }
     outData.println("  }");
     outData.println("- (void)read:(Reader*)reader");
     outData.println("  {");
     for (int i = 0; i < proc.outputs.size(); i++) {
       Field field = (Field) proc.outputs.elementAt(i);
       readCall(field, outData);
       if (isNull(field) == true) // && field.isEmptyOrAnsiAsNull() == false)
       outData.println(
             "    self." + useLowerName(field) + "IsNull = [reader getBoolean];[reader skip:6];");
     }
     outData.println("  }");
     outData.println("- (void)dealloc");
     outData.println("  {");
     for (int i = 0; i < proc.outputs.size(); i++) {
       Field field = (Field) proc.outputs.elementAt(i);
       freeCall(field, outData);
     }
     outData.println("    [super dealloc];");
     outData.println("  }");
     outData.println("@end");
     outData.println();
   }
   if (proc.inputs.size() + proc.dynamics.size() > 0) {
     outData.println("@implementation D" + mainName + "Rec");
     for (int i = 0; i < proc.inputs.size(); i++) {
       Field field = (Field) proc.inputs.elementAt(i);
       if (proc.hasOutput(field.name)) continue;
       if (field.type == Field.BLOB) {
         outData.println("@synthesize " + useLowerName(field) + "_len;");
         outData.println("@synthesize " + useLowerName(field) + "_data;");
       } else outData.println("@synthesize " + useLowerName(field) + ";");
       if (isNull(field) == true)
         outData.println("@synthesize " + useLowerName(field) + "IsNull;");
     }
     for (int i = 0; i < proc.dynamics.size(); i++) {
       String name = (String) proc.dynamics.elementAt(i);
       Field field = new Field();
       field.name = name;
       outData.println("@synthesize " + useLowerName(field) + ";");
     }
     outData.println("- (id)init");
     outData.println("  {");
     outData.println("    self = [super init];");
     outData.println("    return self;");
     outData.println("  }");
     outData.println("- (void)clear");
     outData.println("  {");
     if (proc.outputs.size() > 0) outData.println("    [super clear];");
     for (int i = 0; i < proc.inputs.size(); i++) {
       Field field = (Field) proc.inputs.elementAt(i);
       clearCall(field, outData);
       if (isNull(field) == true) // && field.isEmptyOrAnsiAsNull() == false)
       outData.println("    self." + useLowerName(field) + "IsNull = NO;");
     }
     for (int i = 0; i < proc.dynamics.size(); i++) {
       Field field = new Field();
       field.name = (String) proc.dynamics.elementAt(i);
       field.length = ((Integer) proc.dynamicSizes.elementAt(i)).intValue();
       field.type = Field.CHAR;
       clearCall(field, outData);
     }
     outData.println("  }");
     outData.println("- (void)write:(Writer*)writer");
     outData.println("  {");
     if (proc.outputs.size() > 0) outData.println("    [super write:writer];");
     for (int i = 0; i < proc.inputs.size(); i++) {
       Field field = (Field) proc.inputs.elementAt(i);
       writeCall(field, outData);
       if (isNull(field) == true) // && field.isEmptyOrAnsiAsNull() == false)
       outData.println(
             "    [writer putBoolean:self." + useLowerName(field) + "IsNull];[writer filler:6];");
     }
     for (int i = 0; i < proc.dynamics.size(); i++) {
       Field field = new Field();
       field.name = (String) proc.dynamics.elementAt(i);
       field.length = ((Integer) proc.dynamicSizes.elementAt(i)).intValue();
       field.type = Field.CHAR;
       writeCall(field, outData);
     }
     outData.println("  }");
     outData.println("- (void)read:(Reader*)reader");
     outData.println("  {");
     if (proc.outputs.size() > 0) outData.println("    [super read:reader];");
     for (int i = 0; i < proc.inputs.size(); i++) {
       Field field = (Field) proc.inputs.elementAt(i);
       readCall(field, outData);
       if (isNull(field) == true) // && field.isEmptyOrAnsiAsNull() == false)
       outData.println(
             "    self." + useLowerName(field) + "IsNull = [reader getBoolean];[reader skip:6];");
     }
     for (int i = 0; i < proc.dynamics.size(); i++) {
       Field field = new Field();
       field.name = (String) proc.dynamics.elementAt(i);
       field.length = ((Integer) proc.dynamicSizes.elementAt(i)).intValue();
       field.type = Field.CHAR;
       readCall(field, outData);
     }
     outData.println("  }");
     outData.println("- (void)dealloc");
     outData.println("  {");
     for (int i = 0; i < proc.dynamics.size(); i++) {
       Field field = new Field();
       field.name = (String) proc.dynamics.elementAt(i);
       outData.println("    [" + useLowerName(field) + " release];");
     }
     outData.println("    [super dealloc];");
     outData.println("  }");
     outData.println("@end");
     outData.println();
   }
 }
Exemplo n.º 12
0
 private static void generateInterfaceStructPairs(
     Proc proc, String mainName, PrintWriter outData) {
   if (proc.outputs.size() > 0) {
     outData.println("@interface O" + mainName + "Rec:NSObject");
     outData.println("{");
     outData.println(" @protected");
     for (int i = 0; i < proc.outputs.size(); i++) {
       Field field = (Field) proc.outputs.elementAt(i);
       outData.println("  " + fieldDef(field));
       if (isNull(field) == true) // && field.isEmptyOrAnsiAsNull() == false)
       outData.println("  BOOL " + useLowerName(field) + "IsNull;");
     }
     outData.println("}");
     for (int i = 0; i < proc.outputs.size(); i++) {
       Field field = (Field) proc.outputs.elementAt(i);
       if (field.type == Field.BLOB) {
         outData.println("@property (readwrite) int32 " + useLowerName(field) + "_len;");
         outData.println("@property (retain) NSMutableData* " + useLowerName(field) + "_data;");
       } else outData.println("@property (" + fieldProp(field) + ") " + fieldDef(field));
       if (isNull(field) == true) // && field.isEmptyOrAnsiAsNull() == false)
       outData.println("@property (readwrite) BOOL " + useLowerName(field) + "IsNull;");
     }
     outData.println("- (id)init;");
     outData.println("- (void)clear;");
     outData.println("- (void)write:(Writer*)writer;");
     outData.println("- (void)read:(Reader*)reader;");
     outData.println("- (void)dealloc;");
     outData.println("@end");
     if (proc.inputs.size() + proc.dynamics.size() == 0)
       outData.println("#define D" + mainName + "Rec O" + mainName + "Rec");
     outData.println();
   }
   if (proc.inputs.size() + proc.dynamics.size() > 0) {
     if (proc.outputs.size() > 0)
       outData.println("@interface D" + mainName + "Rec:O" + mainName + "Rec");
     else outData.println("@interface D" + mainName + "Rec:NSObject");
     outData.println("{");
     outData.println(" @protected");
     for (int j = 0; j < proc.inputs.size(); j++) {
       Field field = (Field) proc.inputs.elementAt(j);
       if (proc.hasOutput(field.name)) continue;
       outData.println("  " + fieldDef(field));
       if (isNull(field) == true) outData.println("  BOOL " + useLowerName(field) + "IsNull;");
     }
     for (int j = 0; j < proc.dynamics.size(); j++) {
       String s = (String) proc.dynamics.elementAt(j);
       Integer n = (Integer) proc.dynamicSizes.elementAt(j);
       Field field = new Field();
       field.name = s;
       field.type = Field.CHAR;
       field.length = n.intValue();
       outData.println("  " + fieldDef(field));
     }
     outData.println("}");
     for (int j = 0; j < proc.inputs.size(); j++) {
       Field field = (Field) proc.inputs.elementAt(j);
       if (proc.hasOutput(field.name)) continue;
       if (field.type == Field.BLOB) {
         outData.println("@property (readwrite) int32 " + useLowerName(field) + "_len;");
         outData.println("@property (retain) NSMutableData* " + useLowerName(field) + "_data;");
       } else outData.println("@property (" + fieldProp(field) + ") " + fieldDef(field));
       if (isNull(field) == true)
         outData.println("@property (readwrite) BOOL " + useLowerName(field) + "IsNull;");
     }
     for (int j = 0; j < proc.dynamics.size(); j++) {
       String s = (String) proc.dynamics.elementAt(j);
       Field field = new Field();
       field.name = s;
       outData.println("@property (retain) NSString* " + useLowerName(field) + ";");
     }
     outData.println("- (id)init;");
     outData.println("- (void)clear;");
     outData.println("- (void)write:(Writer*)writer;");
     outData.println("- (void)read:(Reader*)reader;");
     outData.println("- (void)dealloc;");
     outData.println("@end");
     if (proc.outputs.size() == 0)
       outData.println("#define O" + mainName + "Rec D" + mainName + "Rec");
     outData.println();
   }
 }