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.
It so seems that Thrift IDL default value assignments can be JSON style values.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.... | |
@Override | |
public void clear() { | |
this.familyHead = new Person(); | |
this.familyHead.setName("Father"); | |
this.familyHead.setAge(60); | |
} | |
.... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.... | |
@Override | |
public void clear() { | |
this.name = null; | |
setAgeIsSet(false); | |
this.age = 0; | |
this.cars = new ArrayList<Vehicle>(); | |
this.bikes = new ArrayList<Vehicle>(); | |
} | |
.... | |
No comments:
Post a Comment