/** * Returns the fields as a list of parameter declarations for inclusion in Java source. E.g.: * * <code> * org.example.Record field1, List<Integer> field2, Map<String, Integer> field3, Integer field4 * </code> * * @param fields provides the fields to construct a parameter declaration list for. * @return the fields as a list of parameter declarations. */ public String fieldAndTypeList(List<RecordTemplateSpec.Field> fields) { StringBuilder sb = new StringBuilder(); Iterator<RecordTemplateSpec.Field> iter = fields.iterator(); while(iter.hasNext()) { RecordTemplateSpec.Field field = iter.next(); sb.append(toOptionalType(field.getType(), field.getSchemaField().getOptional())); sb.append(" "); sb.append(escapeKeyword(field.getSchemaField().getName())); if (iter.hasNext()) sb.append(", "); } return sb.toString(); }
if (DataSchemaUtil.containsPath(currSchema, pathString)) if (!DataSchemaUtil.getField(currSchema, pathString).getOptional() && annotationClass.equals(ReadOnly.class))
path.remove(path.size() - 1); else if (field.getOptional())
protected void encodeFieldType(RecordDataSchema.Field field) throws IOException boolean optional = field.getOptional(); DataSchema fieldSchema = field.getType(); UnionDataSchema unionDataSchema =
boolean isOptional = field.getOptional(); boolean isTranslatedUnionMember = Boolean.TRUE == field.getProperties().get(SchemaTranslator.TRANSLATED_UNION_MEMBER_PROPERTY); if (isOptional)
write(escapeIdentifier(field.getName())); write(": "); if (field.getOptional())
break; case REMOVE_OPTIONAL_IF_NULL: if (field.getOptional())
fieldDef.getDataClass(), value, fieldDef.getField().getOptional()? SetMode.IGNORE_NULL : SetMode.DISALLOW_NULL);
if (!(spec.isRequiredFieldsOnly() && field.getOptional()))
if (!explicit && field.getSchemaField().getOptional()) { return "nil"; } else {
else if (field.getOptional() == false && mode == GetMode.STRICT)
boolean propagateDefault = !field.getOptional() || _options.getOptionalDefaultMode() == OptionalDefaultMode.TRANSLATE_DEFAULT;
/** * Encode a field's default to an Avro-compliant schema. * * Special handling is required if the translated default value * is a union value whose value is not null. In this case, * the default value includes the member type discriminator, * special handling required to comply with Avro specification * requires that (a) the member type to be the 1st member of the * union's member type list and (b) the discriminator not be * included in the default value. * * @param field providing the default value to encode. * @throws IOException if there is an error while encoding. */ @Override protected void encodeFieldDefault(RecordDataSchema.Field field) throws IOException { FieldOverride defaultValueOverride = _fieldOverridesProvider.getDefaultValueOverride(field); // if field is optional, it must have a default value - either Data.NULL or translated value assert(!field.getOptional() || (defaultValueOverride != null && defaultValueOverride.getValue() != null)); boolean isTranslatedUnionMember = (Boolean.TRUE == field.getProperties().get(SchemaTranslator.TRANSLATED_UNION_MEMBER_PROPERTY)); // Set the default value only for fields that are not translated from Pegasus union members. if ((defaultValueOverride != null && defaultValueOverride.getValue() != null) && !isTranslatedUnionMember) { _builder.writeFieldName(DEFAULT_KEY); _builder.writeData(defaultValueOverride.getValue()); } }
private boolean isOptionalityCompatible(RecordDataSchema.Field field, Object leader, Object follower) { /* compatible: leader : follower : is optional null : null : true null : not null : true not null : not null : true not null : not null : false incompatible: leader : follower : is optional not null : null : true null : null : false (caught by validator) null : not null : false (caught by validator) not null : null : false (caught by validator) */ final boolean isLeaderNull = (leader == null); final boolean isFollowerNull = (follower == null); final boolean isCompatible = !(!isLeaderNull && isFollowerNull && field.getOptional()); if (isCompatible && isLeaderNull != isFollowerNull) { _infoMap.addRestSpecInfo(CompatibilityInfo.Type.OPTIONAL_VALUE, _infoPath, field.getName()); } return isCompatible; }
@Override protected Object translateField(List<Object> path, Object fieldValue, RecordDataSchema.Field field) { DataSchema fieldDataSchema = field.getType(); boolean isOptional = field.getOptional(); Object result; if (isOptional && fieldValue == Data.NULL) { // for optional fields, // null union members have been removed from translated union schema // default value of null should also be removed, make it so that there is no default result = null; } else { result = translate(path, fieldValue, fieldDataSchema); } return result; } }
private static FieldModifier toFieldModifier(RecordDataSchema.Field field) { if (field.getOptional()) { return FieldModifier.OPTIONAL; } else { if (field.getDefault() != null) { return FieldModifier.REQUIRED_WITH_DEFAULT; } else { return FieldModifier.REQUIRED; } } }
/** * Encode a field's optional flag. * * @param field providing the optional flag to encode. * @throws IOException if there is an error while encoding. */ protected void encodeFieldOptional(RecordDataSchema.Field field) throws IOException { boolean optional = field.getOptional(); if (optional) { _builder.writeBooleanField(OPTIONAL_KEY, optional); } }
/** * Just returns a "?" if this was an optional field either due to being decalred optional, or opting not to pass * the STRICT directive into the generator. **/ public String questionMarkIfOptional() { boolean isFieldOptional = _schemaField().getOptional(); boolean markFieldAsOptional = isFieldOptional || TSProperties.optionality == Optionality.REQUIRED_FIELDS_MAY_BE_ABSENT; return markFieldAsOptional? "?": ""; }
private boolean isFieldOptional(RecordDataSchema.Field field, DataElement element) { if (field.getOptional()) { return true; } return _options.getTreatOptional().evaluate(new SimpleDataElement(null, field.getName(), field.getType(), element)); }
public boolean isOptional(RecordTemplateSpec.Field field) { boolean isFieldOptional = field.getSchemaField().getOptional(); return isFieldOptional || swiftProperties.optionality == Optionality.REQUIRED_FIELDS_MAY_BE_ABSENT; }