Skip to content

Instantly share code, notes, and snippets.

@jimexist
Created April 19, 2022 01:08
Show Gist options
  • Save jimexist/7acffaa0c4967193937a05f56098853b to your computer and use it in GitHub Desktop.
Save jimexist/7acffaa0c4967193937a05f56098853b to your computer and use it in GitHub Desktop.
demo usage of `annotation_metadata' flag
{
"name": "annotations",
"namespaces": {
"java": "org.apache.anno.test"
},
"includes": [
],
"enums": [
],
"typedefs": [
],
"structs": [
{
"name": "Person",
"isException": false,
"isUnion": false,
"fields": [
{
"key": 1,
"name": "name",
"typeId": "string",
"annotations": {
"access_level": "public"
},
"required": "required"
},
{
"key": 2,
"name": "phone",
"typeId": "string",
"annotations": {
"max_length": "12",
"min_length": "5"
},
"required": "optional"
},
{
"key": 3,
"name": "addresses",
"typeId": "list",
"type": {
"typeId": "list",
"elemTypeId": "string"
},
"annotations": {
"non_empty": "true"
},
"required": "optional"
}
]
}
],
"constants": [
],
"services": [
]
}
namespace java org.apache.anno.test
struct Person {
1: required string name (access_level = "public")
2: optional string phone (min_length = "5", max_length = "12")
3: optional list<string> addresses (non_empty = "true")
}
/**
* Autogenerated by Thrift Compiler (0.17.0)
*
* DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
* @generated
*/
package org.apache.anno.test;
@SuppressWarnings({"cast", "rawtypes", "serial", "unchecked", "unused"})
@javax.annotation.Generated(value = "Autogenerated by Thrift Compiler (0.17.0)", date = "2022-04-19")
public class Person implements org.apache.thrift.TBase<Person, Person._Fields>, java.io.Serializable, Cloneable, Comparable<Person> {
private static final org.apache.thrift.protocol.TStruct STRUCT_DESC = new org.apache.thrift.protocol.TStruct("Person");
private static final org.apache.thrift.protocol.TField NAME_FIELD_DESC = new org.apache.thrift.protocol.TField("name", org.apache.thrift.protocol.TType.STRING, (short)1);
private static final org.apache.thrift.protocol.TField PHONE_FIELD_DESC = new org.apache.thrift.protocol.TField("phone", org.apache.thrift.protocol.TType.STRING, (short)2);
private static final org.apache.thrift.protocol.TField ADDRESSES_FIELD_DESC = new org.apache.thrift.protocol.TField("addresses", org.apache.thrift.protocol.TType.LIST, (short)3);
private static final org.apache.thrift.scheme.SchemeFactory STANDARD_SCHEME_FACTORY = new PersonStandardSchemeFactory();
private static final org.apache.thrift.scheme.SchemeFactory TUPLE_SCHEME_FACTORY = new PersonTupleSchemeFactory();
public @org.apache.thrift.annotation.Nullable java.lang.String name; // required
public @org.apache.thrift.annotation.Nullable java.lang.String phone; // optional
public @org.apache.thrift.annotation.Nullable java.util.List<java.lang.String> addresses; // optional
/** The set of fields this struct contains, along with convenience methods for finding and manipulating them. */
public enum _Fields implements org.apache.thrift.TFieldIdEnum {
NAME((short)1, "name"),
PHONE((short)2, "phone"),
ADDRESSES((short)3, "addresses");
private static final java.util.Map<java.lang.String, _Fields> byName = new java.util.HashMap<java.lang.String, _Fields>();
static {
for (_Fields field : java.util.EnumSet.allOf(_Fields.class)) {
byName.put(field.getFieldName(), field);
}
}
/**
* Find the _Fields constant that matches fieldId, or null if its not found.
*/
@org.apache.thrift.annotation.Nullable
public static _Fields findByThriftId(int fieldId) {
switch(fieldId) {
case 1: // NAME
return NAME;
case 2: // PHONE
return PHONE;
case 3: // ADDRESSES
return ADDRESSES;
default:
return null;
}
}
/**
* Find the _Fields constant that matches fieldId, throwing an exception
* if it is not found.
*/
public static _Fields findByThriftIdOrThrow(int fieldId) {
_Fields fields = findByThriftId(fieldId);
if (fields == null) throw new java.lang.IllegalArgumentException("Field " + fieldId + " doesn't exist!");
return fields;
}
/**
* Find the _Fields constant that matches name, or null if its not found.
*/
@org.apache.thrift.annotation.Nullable
public static _Fields findByName(java.lang.String name) {
return byName.get(name);
}
private final short _thriftId;
private final java.lang.String _fieldName;
_Fields(short thriftId, java.lang.String fieldName) {
_thriftId = thriftId;
_fieldName = fieldName;
}
@Override
public short getThriftFieldId() {
return _thriftId;
}
@Override
public java.lang.String getFieldName() {
return _fieldName;
}
}
// isset id assignments
private static final _Fields optionals[] = {_Fields.PHONE,_Fields.ADDRESSES};
public static final java.util.Map<_Fields, org.apache.thrift.meta_data.FieldMetaData> metaDataMap;
static {
java.util.Map<_Fields, org.apache.thrift.meta_data.FieldMetaData> tmpMap = new java.util.EnumMap<_Fields, org.apache.thrift.meta_data.FieldMetaData>(_Fields.class);
tmpMap.put(_Fields.NAME, new org.apache.thrift.meta_data.FieldMetaData("name", org.apache.thrift.TFieldRequirementType.REQUIRED,
new org.apache.thrift.meta_data.FieldValueMetaData(org.apache.thrift.protocol.TType.STRING),
java.util.stream.Stream.<java.util.Map.Entry<java.lang.String, java.lang.String>>builder()
.add(new java.util.AbstractMap.SimpleImmutableEntry<>("access_level", "public"))
.build().collect(java.util.stream.Collectors.toMap(java.util.Map.Entry::getKey, java.util.Map.Entry::getValue))));
tmpMap.put(_Fields.PHONE, new org.apache.thrift.meta_data.FieldMetaData("phone", org.apache.thrift.TFieldRequirementType.OPTIONAL,
new org.apache.thrift.meta_data.FieldValueMetaData(org.apache.thrift.protocol.TType.STRING),
java.util.stream.Stream.<java.util.Map.Entry<java.lang.String, java.lang.String>>builder()
.add(new java.util.AbstractMap.SimpleImmutableEntry<>("max_length", "12"))
.add(new java.util.AbstractMap.SimpleImmutableEntry<>("min_length", "5"))
.build().collect(java.util.stream.Collectors.toMap(java.util.Map.Entry::getKey, java.util.Map.Entry::getValue))));
tmpMap.put(_Fields.ADDRESSES, new org.apache.thrift.meta_data.FieldMetaData("addresses", org.apache.thrift.TFieldRequirementType.OPTIONAL,
new org.apache.thrift.meta_data.ListMetaData(org.apache.thrift.protocol.TType.LIST,
new org.apache.thrift.meta_data.FieldValueMetaData(org.apache.thrift.protocol.TType.STRING)),
java.util.stream.Stream.<java.util.Map.Entry<java.lang.String, java.lang.String>>builder()
.add(new java.util.AbstractMap.SimpleImmutableEntry<>("non_empty", "true"))
.build().collect(java.util.stream.Collectors.toMap(java.util.Map.Entry::getKey, java.util.Map.Entry::getValue))));
metaDataMap = java.util.Collections.unmodifiableMap(tmpMap);
org.apache.thrift.meta_data.FieldMetaData.addStructMetaDataMap(Person.class, metaDataMap);
}
public Person() {
}
public Person(
java.lang.String name)
{
this();
this.name = name;
}
/**
* Performs a deep copy on <i>other</i>.
*/
public Person(Person other) {
if (other.isSetName()) {
this.name = other.name;
}
if (other.isSetPhone()) {
this.phone = other.phone;
}
if (other.isSetAddresses()) {
java.util.List<java.lang.String> __this__addresses = new java.util.ArrayList<java.lang.String>(other.addresses);
this.addresses = __this__addresses;
}
}
@Override
public Person deepCopy() {
return new Person(this);
}
@Override
public void clear() {
this.name = null;
this.phone = null;
this.addresses = null;
}
@org.apache.thrift.annotation.Nullable
public java.lang.String getName() {
return this.name;
}
public Person setName(@org.apache.thrift.annotation.Nullable java.lang.String name) {
this.name = name;
return this;
}
public void unsetName() {
this.name = null;
}
/** Returns true if field name is set (has been assigned a value) and false otherwise */
public boolean isSetName() {
return this.name != null;
}
public void setNameIsSet(boolean value) {
if (!value) {
this.name = null;
}
}
@org.apache.thrift.annotation.Nullable
public java.lang.String getPhone() {
return this.phone;
}
public Person setPhone(@org.apache.thrift.annotation.Nullable java.lang.String phone) {
this.phone = phone;
return this;
}
public void unsetPhone() {
this.phone = null;
}
/** Returns true if field phone is set (has been assigned a value) and false otherwise */
public boolean isSetPhone() {
return this.phone != null;
}
public void setPhoneIsSet(boolean value) {
if (!value) {
this.phone = null;
}
}
public int getAddressesSize() {
return (this.addresses == null) ? 0 : this.addresses.size();
}
@org.apache.thrift.annotation.Nullable
public java.util.Iterator<java.lang.String> getAddressesIterator() {
return (this.addresses == null) ? null : this.addresses.iterator();
}
public void addToAddresses(java.lang.String elem) {
if (this.addresses == null) {
this.addresses = new java.util.ArrayList<java.lang.String>();
}
this.addresses.add(elem);
}
@org.apache.thrift.annotation.Nullable
public java.util.List<java.lang.String> getAddresses() {
return this.addresses;
}
public Person setAddresses(@org.apache.thrift.annotation.Nullable java.util.List<java.lang.String> addresses) {
this.addresses = addresses;
return this;
}
public void unsetAddresses() {
this.addresses = null;
}
/** Returns true if field addresses is set (has been assigned a value) and false otherwise */
public boolean isSetAddresses() {
return this.addresses != null;
}
public void setAddressesIsSet(boolean value) {
if (!value) {
this.addresses = null;
}
}
@Override
public void setFieldValue(_Fields field, @org.apache.thrift.annotation.Nullable java.lang.Object value) {
switch (field) {
case NAME:
if (value == null) {
unsetName();
} else {
setName((java.lang.String)value);
}
break;
case PHONE:
if (value == null) {
unsetPhone();
} else {
setPhone((java.lang.String)value);
}
break;
case ADDRESSES:
if (value == null) {
unsetAddresses();
} else {
setAddresses((java.util.List<java.lang.String>)value);
}
break;
}
}
@org.apache.thrift.annotation.Nullable
@Override
public java.lang.Object getFieldValue(_Fields field) {
switch (field) {
case NAME:
return getName();
case PHONE:
return getPhone();
case ADDRESSES:
return getAddresses();
}
throw new java.lang.IllegalStateException();
}
/** Returns true if field corresponding to fieldID is set (has been assigned a value) and false otherwise */
@Override
public boolean isSet(_Fields field) {
if (field == null) {
throw new java.lang.IllegalArgumentException();
}
switch (field) {
case NAME:
return isSetName();
case PHONE:
return isSetPhone();
case ADDRESSES:
return isSetAddresses();
}
throw new java.lang.IllegalStateException();
}
@Override
public boolean equals(java.lang.Object that) {
if (that instanceof Person)
return this.equals((Person)that);
return false;
}
public boolean equals(Person that) {
if (that == null)
return false;
if (this == that)
return true;
boolean this_present_name = true && this.isSetName();
boolean that_present_name = true && that.isSetName();
if (this_present_name || that_present_name) {
if (!(this_present_name && that_present_name))
return false;
if (!this.name.equals(that.name))
return false;
}
boolean this_present_phone = true && this.isSetPhone();
boolean that_present_phone = true && that.isSetPhone();
if (this_present_phone || that_present_phone) {
if (!(this_present_phone && that_present_phone))
return false;
if (!this.phone.equals(that.phone))
return false;
}
boolean this_present_addresses = true && this.isSetAddresses();
boolean that_present_addresses = true && that.isSetAddresses();
if (this_present_addresses || that_present_addresses) {
if (!(this_present_addresses && that_present_addresses))
return false;
if (!this.addresses.equals(that.addresses))
return false;
}
return true;
}
@Override
public int hashCode() {
int hashCode = 1;
hashCode = hashCode * 8191 + ((isSetName()) ? 131071 : 524287);
if (isSetName())
hashCode = hashCode * 8191 + name.hashCode();
hashCode = hashCode * 8191 + ((isSetPhone()) ? 131071 : 524287);
if (isSetPhone())
hashCode = hashCode * 8191 + phone.hashCode();
hashCode = hashCode * 8191 + ((isSetAddresses()) ? 131071 : 524287);
if (isSetAddresses())
hashCode = hashCode * 8191 + addresses.hashCode();
return hashCode;
}
@Override
public int compareTo(Person other) {
if (!getClass().equals(other.getClass())) {
return getClass().getName().compareTo(other.getClass().getName());
}
int lastComparison = 0;
lastComparison = java.lang.Boolean.compare(isSetName(), other.isSetName());
if (lastComparison != 0) {
return lastComparison;
}
if (isSetName()) {
lastComparison = org.apache.thrift.TBaseHelper.compareTo(this.name, other.name);
if (lastComparison != 0) {
return lastComparison;
}
}
lastComparison = java.lang.Boolean.compare(isSetPhone(), other.isSetPhone());
if (lastComparison != 0) {
return lastComparison;
}
if (isSetPhone()) {
lastComparison = org.apache.thrift.TBaseHelper.compareTo(this.phone, other.phone);
if (lastComparison != 0) {
return lastComparison;
}
}
lastComparison = java.lang.Boolean.compare(isSetAddresses(), other.isSetAddresses());
if (lastComparison != 0) {
return lastComparison;
}
if (isSetAddresses()) {
lastComparison = org.apache.thrift.TBaseHelper.compareTo(this.addresses, other.addresses);
if (lastComparison != 0) {
return lastComparison;
}
}
return 0;
}
@org.apache.thrift.annotation.Nullable
@Override
public _Fields fieldForId(int fieldId) {
return _Fields.findByThriftId(fieldId);
}
@Override
public void read(org.apache.thrift.protocol.TProtocol iprot) throws org.apache.thrift.TException {
scheme(iprot).read(iprot, this);
}
@Override
public void write(org.apache.thrift.protocol.TProtocol oprot) throws org.apache.thrift.TException {
scheme(oprot).write(oprot, this);
}
@Override
public java.lang.String toString() {
java.lang.StringBuilder sb = new java.lang.StringBuilder("Person(");
boolean first = true;
sb.append("name:");
if (this.name == null) {
sb.append("null");
} else {
sb.append(this.name);
}
first = false;
if (isSetPhone()) {
if (!first) sb.append(", ");
sb.append("phone:");
if (this.phone == null) {
sb.append("null");
} else {
sb.append(this.phone);
}
first = false;
}
if (isSetAddresses()) {
if (!first) sb.append(", ");
sb.append("addresses:");
if (this.addresses == null) {
sb.append("null");
} else {
sb.append(this.addresses);
}
first = false;
}
sb.append(")");
return sb.toString();
}
public void validate() throws org.apache.thrift.TException {
// check for required fields
if (name == null) {
throw new org.apache.thrift.protocol.TProtocolException("Required field 'name' was not present! Struct: " + toString());
}
// check for sub-struct validity
}
private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException {
try {
write(new org.apache.thrift.protocol.TCompactProtocol(new org.apache.thrift.transport.TIOStreamTransport(out)));
} catch (org.apache.thrift.TException te) {
throw new java.io.IOException(te);
}
}
private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, java.lang.ClassNotFoundException {
try {
read(new org.apache.thrift.protocol.TCompactProtocol(new org.apache.thrift.transport.TIOStreamTransport(in)));
} catch (org.apache.thrift.TException te) {
throw new java.io.IOException(te);
}
}
private static class PersonStandardSchemeFactory implements org.apache.thrift.scheme.SchemeFactory {
@Override
public PersonStandardScheme getScheme() {
return new PersonStandardScheme();
}
}
private static class PersonStandardScheme extends org.apache.thrift.scheme.StandardScheme<Person> {
@Override
public void read(org.apache.thrift.protocol.TProtocol iprot, Person struct) throws org.apache.thrift.TException {
org.apache.thrift.protocol.TField schemeField;
iprot.readStructBegin();
while (true)
{
schemeField = iprot.readFieldBegin();
if (schemeField.type == org.apache.thrift.protocol.TType.STOP) {
break;
}
switch (schemeField.id) {
case 1: // NAME
if (schemeField.type == org.apache.thrift.protocol.TType.STRING) {
struct.name = iprot.readString();
struct.setNameIsSet(true);
} else {
org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);
}
break;
case 2: // PHONE
if (schemeField.type == org.apache.thrift.protocol.TType.STRING) {
struct.phone = iprot.readString();
struct.setPhoneIsSet(true);
} else {
org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);
}
break;
case 3: // ADDRESSES
if (schemeField.type == org.apache.thrift.protocol.TType.LIST) {
{
org.apache.thrift.protocol.TList _list0 = iprot.readListBegin();
struct.addresses = new java.util.ArrayList<java.lang.String>(_list0.size);
@org.apache.thrift.annotation.Nullable java.lang.String _elem1;
for (int _i2 = 0; _i2 < _list0.size; ++_i2)
{
_elem1 = iprot.readString();
struct.addresses.add(_elem1);
}
iprot.readListEnd();
}
struct.setAddressesIsSet(true);
} else {
org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);
}
break;
default:
org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);
}
iprot.readFieldEnd();
}
iprot.readStructEnd();
// check for required fields of primitive type, which can't be checked in the validate method
struct.validate();
}
@Override
public void write(org.apache.thrift.protocol.TProtocol oprot, Person struct) throws org.apache.thrift.TException {
struct.validate();
oprot.writeStructBegin(STRUCT_DESC);
if (struct.name != null) {
oprot.writeFieldBegin(NAME_FIELD_DESC);
oprot.writeString(struct.name);
oprot.writeFieldEnd();
}
if (struct.phone != null) {
if (struct.isSetPhone()) {
oprot.writeFieldBegin(PHONE_FIELD_DESC);
oprot.writeString(struct.phone);
oprot.writeFieldEnd();
}
}
if (struct.addresses != null) {
if (struct.isSetAddresses()) {
oprot.writeFieldBegin(ADDRESSES_FIELD_DESC);
{
oprot.writeListBegin(new org.apache.thrift.protocol.TList(org.apache.thrift.protocol.TType.STRING, struct.addresses.size()));
for (java.lang.String _iter3 : struct.addresses)
{
oprot.writeString(_iter3);
}
oprot.writeListEnd();
}
oprot.writeFieldEnd();
}
}
oprot.writeFieldStop();
oprot.writeStructEnd();
}
}
private static class PersonTupleSchemeFactory implements org.apache.thrift.scheme.SchemeFactory {
@Override
public PersonTupleScheme getScheme() {
return new PersonTupleScheme();
}
}
private static class PersonTupleScheme extends org.apache.thrift.scheme.TupleScheme<Person> {
@Override
public void write(org.apache.thrift.protocol.TProtocol prot, Person struct) throws org.apache.thrift.TException {
org.apache.thrift.protocol.TTupleProtocol oprot = (org.apache.thrift.protocol.TTupleProtocol) prot;
oprot.writeString(struct.name);
java.util.BitSet optionals = new java.util.BitSet();
if (struct.isSetPhone()) {
optionals.set(0);
}
if (struct.isSetAddresses()) {
optionals.set(1);
}
oprot.writeBitSet(optionals, 2);
if (struct.isSetPhone()) {
oprot.writeString(struct.phone);
}
if (struct.isSetAddresses()) {
{
oprot.writeI32(struct.addresses.size());
for (java.lang.String _iter4 : struct.addresses)
{
oprot.writeString(_iter4);
}
}
}
}
@Override
public void read(org.apache.thrift.protocol.TProtocol prot, Person struct) throws org.apache.thrift.TException {
org.apache.thrift.protocol.TTupleProtocol iprot = (org.apache.thrift.protocol.TTupleProtocol) prot;
struct.name = iprot.readString();
struct.setNameIsSet(true);
java.util.BitSet incoming = iprot.readBitSet(2);
if (incoming.get(0)) {
struct.phone = iprot.readString();
struct.setPhoneIsSet(true);
}
if (incoming.get(1)) {
{
org.apache.thrift.protocol.TList _list5 = iprot.readListBegin(org.apache.thrift.protocol.TType.STRING);
struct.addresses = new java.util.ArrayList<java.lang.String>(_list5.size);
@org.apache.thrift.annotation.Nullable java.lang.String _elem6;
for (int _i7 = 0; _i7 < _list5.size; ++_i7)
{
_elem6 = iprot.readString();
struct.addresses.add(_elem6);
}
}
struct.setAddressesIsSet(true);
}
}
}
private static <S extends org.apache.thrift.scheme.IScheme> S scheme(org.apache.thrift.protocol.TProtocol proto) {
return (org.apache.thrift.scheme.StandardScheme.class.equals(proto.getScheme()) ? STANDARD_SCHEME_FACTORY : TUPLE_SCHEME_FACTORY).getScheme();
}
}
@jimexist
Copy link
Author

the somewhat convoluted java.util.stream.Stream.<java.util.Map.Entry<java.lang.String, java.lang.String>>builder() is because in JDK8 there's no vararg map building functions. ideally we can move to https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Map.html#ofEntries(java.util.Map.Entry...) later

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment