Package com.linkedin.data.schema

Examples of com.linkedin.data.schema.UnionDataSchema


      MapDataSchema mapSchema = (MapDataSchema)schema;
      recordType(mapSchema.getValues(), foundTypes, typeOrder);
    }
    else if (schema instanceof UnionDataSchema)
    {
      UnionDataSchema unionSchema = (UnionDataSchema)schema;
      for(DataSchema type : unionSchema.getTypes())
      {
        recordType(type, foundTypes, typeOrder);
      }
    }
  }
View Full Code Here


            _path.removeLast();
          }
          result = dataMap;
          break;
        case UNION:
          UnionDataSchema unionDataSchema = (UnionDataSchema) dereferencedDataSchema;
          Map.Entry<DataSchema, Schema> memberSchemas = findUnionMemberSchema(value, unionDataSchema, avroSchema);
          if (memberSchemas == null)
          {
            result = BAD_RESULT;
            break;
View Full Code Here

            _path.removeLast();
          }
          result = avroRecord;
          break;
        case UNION:
          UnionDataSchema unionDataSchema = (UnionDataSchema) dereferencedDataSchema;
          String key;
          Object memberValue;
          if (value == Data.NULL)
          {
            key = DataSchemaConstants.NULL_TYPE;
            memberValue = Data.NULL;
          }
          else
          {
            map = (DataMap) value;
            Map.Entry<String, Object> entry = map.entrySet().iterator().next();
            key = entry.getKey();
            memberValue = entry.getValue();
          }
          DataSchema memberDataSchema = unionDataSchema.getType(key);
          Map.Entry<String, Schema> memberAvroEntry = findUnionMember(memberDataSchema, avroSchema);
          if (memberAvroEntry == null)
          {
            result = BAD_RESULT;
            break;
View Full Code Here

        DataSchema fieldSchema = field.getType();
        // check if union
        boolean isUnion = fieldSchema.getDereferencedType() == DataSchema.Type.UNION;
        field.setOptional(false);
        if (isUnion) {
          UnionDataSchema unionSchema = (UnionDataSchema) fieldSchema;
          int nullIndex= unionSchema.index(NULL_DATA_SCHEMA.getUnionMemberKey());
          // check if union with null
          if (nullIndex != -1)
          {
            List<DataSchema> types = unionSchema.getTypes();
            if (types.size() == 2)
            {
              DataSchema newFieldSchema = unionSchema.getTypes().get((nullIndex + 1) % 2);
              field.setType(newFieldSchema);
            }
            else
            {
              ArrayList<DataSchema> newTypes = new ArrayList<DataSchema>(types);
              newTypes.remove(nullIndex);
              StringBuilder errorMessages = null; // not expecting errors
              unionSchema.setTypes(newTypes, errorMessages);
            }
            // set to optional
            field.setOptional(true);
          }
        }
View Full Code Here

  @Override
  protected void encodeFieldType(RecordDataSchema.Field field) throws IOException
  {
    boolean optional = field.getOptional();
    DataSchema fieldSchema = field.getType();
    UnionDataSchema unionDataSchema =
      (fieldSchema.getDereferencedType() == DataSchema.Type.UNION ?
        (UnionDataSchema) fieldSchema.getDereferencedDataSchema() :
        null);
    _builder.writeFieldName(TYPE_KEY);

    if (optional == false && unionDataSchema == null)
    {
      encode(fieldSchema);
    }
    else
    {
      // special handling for unions
      // output will be an union if the field is optional or its type is a union

      // whether to add null to translated union,
      // set to true for optional non-union type or optional union without null member
      boolean addNullMemberType;
      // DataSchema of default value, null if there is no default value.
      DataSchema defaultValueSchema;
      // members of the union (excluding null introduced by optional)
      List<DataSchema> resultMemberTypes;

      Object defaultValue = field.getDefault();
      if (optional)
      {
        if (unionDataSchema == null)
        {
          addNullMemberType = true;
          resultMemberTypes = new ArrayList<DataSchema>(1);
          resultMemberTypes.add(fieldSchema);
          defaultValueSchema = (
            defaultValue != null && _options.getOptionalDefaultMode() == OptionalDefaultMode.TRANSLATE_DEFAULT ?
              fieldSchema :
              DataSchemaConstants.NULL_DATA_SCHEMA);
        }
        else
        {
          addNullMemberType = unionDataSchema.getType(DataSchemaConstants.NULL_TYPE) == null;
          resultMemberTypes = unionDataSchema.getTypes();
          defaultValueSchema = (
            defaultValue != null && _options.getOptionalDefaultMode() == OptionalDefaultMode.TRANSLATE_DEFAULT ?
              unionValueDataSchema(unionDataSchema, defaultValue) :
              DataSchemaConstants.NULL_DATA_SCHEMA);
        }
        assert(_options.getOptionalDefaultMode() != OptionalDefaultMode.TRANSLATE_TO_NULL ||
               defaultValueSchema == DataSchemaConstants.NULL_DATA_SCHEMA);
      }
      else
      {
        // must be union
        addNullMemberType = false;
        resultMemberTypes = unionDataSchema.getTypes();
        defaultValueSchema = unionValueDataSchema(unionDataSchema, defaultValue);
      }

      // encode the member types
      // add null member type if addNullMemberType is present
View Full Code Here

            {
              newSchema = field.getType();
            }
            break;
          case UNION:
            UnionDataSchema unionSchema = (UnionDataSchema) _currentSchema;
            newSchema = unionSchema.getType(key);
            break;
          case MAP:
            MapDataSchema mapSchema = (MapDataSchema) _currentSchema;
            newSchema = mapSchema.getValues();
            break;
View Full Code Here

          dataMap.put("mapField_" + _random.nextInt(), item);
        }
        data = dataMap;
        break;
      case UNION:
        final UnionDataSchema unionSchema = (UnionDataSchema) derefSchema;
        final List<DataSchema> types = removeAlreadyTraversedSchemasFromUnionMemberList(parentSchemas, unionSchema.getTypes());
        final int unionIndex = _random.nextInt(types.size());
        final DataSchema unionItemSchema = types.get(unionIndex);
        data = buildData(parentSchemas, unionItemSchema, fieldName, spec);

        if (data != null)
View Full Code Here

  }

  @Test
  public void testUnionSchema()
  {
    final UnionDataSchema schema = (UnionDataSchema) DataTemplateUtil.getSchema(UnionTest.UnionWithNull.class);
    final Set<String> memberKeys = new HashSet<String>();
    for (DataSchema memberSchema: schema.getTypes())
    {
      memberKeys.add(memberSchema.getUnionMemberKey());
    }
    final String nullMemberKey = DataSchemaConstants.NULL_DATA_SCHEMA.getUnionMemberKey();
View Full Code Here

TOP

Related Classes of com.linkedin.data.schema.UnionDataSchema

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.