Wednesday, February 12, 2014

Thrift Notes on Default values

I have been using Apache Thrift at production for close to 2 years now and something that we did not know for a long time was how to assign default values to thrift's collection types like map, list, set.

It so seems that Thrift IDL default value assignments can be JSON style values.



struct Vehicle {
1: string name
2: string model
}
struct Person {
1: string name
2: i32 age
// Empty collections can be specified using []. Same works for map and set as well.
3: list<Vehicle> cars = []
// Another way to specify default empty values
4: list<Vehicle> bikes = empty
}
// Reference - http://stackoverflow.com/a/12501037
struct Family {
1: Person familyHead = {"name": "Father", "age":60}
}
....
@Override
public void clear() {
this.familyHead = new Person();
this.familyHead.setName("Father");
this.familyHead.setAge(60);
}
....
view raw Family.java hosted with ❤ by GitHub
....
@Override
public void clear() {
this.name = null;
setAgeIsSet(false);
this.age = 0;
this.cars = new ArrayList<Vehicle>();
this.bikes = new ArrayList<Vehicle>();
}
....
view raw Person.java hosted with ❤ by GitHub

No comments:

Post a Comment