Skip to content

Commit

Permalink
add isSet/unset methods to swift structs
Browse files Browse the repository at this point in the history
Summary:
Adding isSet/unset methods to match functionality found in javadeprecated. This is to make migrating projects using this feature easier to migrate to java swift.

#forcetdhashing

Reviewed By: stevegury, avalonalex

Differential Revision: D20970753

fbshipit-source-id: 0ebd324fdc64d2203774f33329d5ddabb09b015b
  • Loading branch information
Robert Roeser authored and facebook-github-bot committed Jul 24, 2020
1 parent 495becd commit e0b19cc
Show file tree
Hide file tree
Showing 83 changed files with 4,674 additions and 673 deletions.
7 changes: 7 additions & 0 deletions thrift/compiler/generate/t_mstch_swift_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ class mstch_swift_field : public mstch_field {
{"field:typeFieldName", &mstch_swift_field::type_field_name},
{"field:isSensitive?", &mstch_swift_field::is_sensitive},
{"field:hasInitialValue?", &mstch_swift_field::has_initial_value},
{"field:isPrimitive?", &mstch_swift_field::is_primitive},
});
}

Expand Down Expand Up @@ -385,6 +386,12 @@ class mstch_swift_field : public mstch_field {
nestedDepth--;
return mstch::node();
}
mstch::node is_primitive() {
auto type = field_->get_type()->get_true_type();
return type->is_void() || type->is_bool() || type->is_byte() ||
type->is_i16() || type->is_i32() || type->is_i64() ||
type->is_double() || type->is_float();
}
mstch::node is_nullable_or_optional_not_enum() {
if (field_->get_req() == t_field::e_req::T_OPTIONAL) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import org.apache.thrift.meta_data.FieldValueMetaData;
public final class {{struct:javaCapitalName}} extends {{#struct:extendRuntimeException?}}java.lang.RuntimeException{{/struct:extendRuntimeException?}}{{^struct:extendRuntimeException?}}java.lang.Exception{{/struct:extendRuntimeException?}} {
private static final long serialVersionUID = 1L;
private BitSet __isset_bit_vector = new BitSet();
private static final TStruct STRUCT_DESC = new TStruct("{{struct:name}}");
{{#struct:fields}}
private final {{> FieldType}} {{field:javaName}};
Expand Down
4 changes: 4 additions & 0 deletions thrift/compiler/generate/templates/java/swift/Struct.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ import static com.google.common.base.MoreObjects.ToStringHelper;
@ThriftStruct(value="{{struct:name}}", builder={{struct:javaCapitalName}}.Builder.class){{> common/ObjectCustomAnnotation}}
{{/struct:asBean?}}
public final class {{struct:javaCapitalName}} {
private BitSet __isset_bit_vector = new BitSet();
{{> struct/ThriftConstructor}}

{{> struct/StructBuilder}}
Expand Down Expand Up @@ -83,6 +85,8 @@ public final class {{struct:javaCapitalName}} {

{{> struct/FieldSetter}}
{{/struct:asBean?}}{{!
}}
{{> struct/FieldIsSet}}{{!
}}{{/struct:fields}}

{{> struct/StructToString}}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{{!
Copyright (c) Facebook, Inc. and its affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
}}

/** don't use this method for new code, it's here to make migrating to swift easier */
@Deprecated
public boolean fieldIsSet{{field:javaCapitalName}}() {
{{#field:isPrimitive?}}
{{#field:optional?}}
return this.{{field:javaName}} != null;
{{/field:optional?}}
{{^field:optional?}}
return __isset_bit_vector.get(_{{field:javaAllCapsName}});
{{/field:optional?}}
{{/field:isPrimitive?}}
{{^field:isPrimitive?}}
return this.{{field:javaName}} != null;
{{/field:isPrimitive?}}
}
{{!
}}{{#struct:asBean?}}{{!
}}/** don't use this method for new code, it's here to make migrating to swift easier */
@Deprecated
public void unset{{field:javaCapitalName}}() {
{{#field:isPrimitive?}}
{{#field:optional?}}
this.{{field:javaName}} = null;
{{/field:optional?}}
{{^field:optional?}}
__isset_bit_vector.clear(_{{field:javaAllCapsName}});
{{/field:optional?}}
{{/field:isPrimitive?}}
{{^field:isPrimitive?}}
this.{{field:javaName}} = null;
{{/field:isPrimitive?}}
}{{!
}}{{/struct:asBean?}}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
}}
public static class Builder {
private final BitSet __optional_isset = new BitSet();
{{#struct:fields}}
{{#field:isSupportedApacheType?}}
{{^field:value}}
Expand All @@ -38,7 +40,7 @@ public static class Builder {
this.{{field:javaName}} = {{field:javaName}};
return this;
}

public {{> FieldType}} {{> GetterName}}() { return {{field:javaName}}; }

{{/struct:fields}}{{!
Expand All @@ -53,24 +55,28 @@ public static class Builder {
@ThriftConstructor
public {{struct:javaCapitalName}} build() {
{{^struct:isBigStruct?}}{{^struct:asBean?}}
return new {{struct:javaCapitalName}} (
{{struct:javaCapitalName}} result = new {{struct:javaCapitalName}} (
{{#struct:fields}}
this.{{field:javaName}}{{^last?}},{{/last?}}
{{/struct:fields}}
);
result.__isset_bit_vector.or(__optional_isset);
return result;
{{/struct:asBean?}}{{/struct:isBigStruct?}}{{!
}}{{#struct:isBigStruct?}}
{{struct:javaCapitalName}} o{{struct:javaCapitalName}} = new {{struct:javaCapitalName}}();
{{#struct:fields}}
o{{struct:javaCapitalName}}.{{field:javaName}} = this.{{field:javaName}};
{{/struct:fields}}
o{{struct:javaCapitalName}}.__isset_bit_vector.or(__optional_isset);
return o{{struct:javaCapitalName}};
{{/struct:isBigStruct?}}{{!
}}{{#struct:asBean?}}
{{struct:javaCapitalName}} result = new {{struct:javaCapitalName}}();
{{#struct:fields}}
result.{{field:javaName}} = this.{{field:javaName}};
{{/struct:fields}}
result.__isset_bit_vector.or(__optional_isset);
return result;
{{/struct:asBean?}}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
@SwiftGenerated
@ThriftStruct(value="MyStruct", builder=MyStruct.Builder.class)
public final class MyStruct {
private BitSet __isset_bit_vector = new BitSet();

@ThriftConstructor
public MyStruct(
@ThriftField(value=1, name="major", requiredness=Requiredness.NONE) final long major,
Expand Down Expand Up @@ -54,6 +56,8 @@ protected MyStruct() {
}

public static class Builder {
private final BitSet __optional_isset = new BitSet();

private long major = 0L;
private String _package = null;
private String annotationWithQuote = null;
Expand All @@ -66,47 +70,47 @@ public Builder setMajor(long major) {
this.major = major;
return this;
}

public long getMajor() { return major; }

@ThriftField(value=2, name="package", requiredness=Requiredness.NONE)
public Builder setPackage(String _package) {
this._package = _package;
return this;
}

public String getPackage() { return _package; }

@ThriftField(value=3, name="annotation_with_quote", requiredness=Requiredness.NONE)
public Builder setAnnotationWithQuote(String annotationWithQuote) {
this.annotationWithQuote = annotationWithQuote;
return this;
}

public String getAnnotationWithQuote() { return annotationWithQuote; }

@ThriftField(value=4, name="class_", requiredness=Requiredness.NONE)
public Builder setClass_(String class_) {
this.class_ = class_;
return this;
}

public String getClass_() { return class_; }

@ThriftField(value=5, name="annotation_with_trailing_comma", requiredness=Requiredness.NONE)
public Builder setAnnotationWithTrailingComma(String annotationWithTrailingComma) {
this.annotationWithTrailingComma = annotationWithTrailingComma;
return this;
}

public String getAnnotationWithTrailingComma() { return annotationWithTrailingComma; }

@ThriftField(value=6, name="empty_annotations", requiredness=Requiredness.NONE)
public Builder setEmptyAnnotations(String emptyAnnotations) {
this.emptyAnnotations = emptyAnnotations;
return this;
}

public String getEmptyAnnotations() { return emptyAnnotations; }

public Builder() { }
Expand All @@ -121,14 +125,16 @@ public Builder(MyStruct other) {

@ThriftConstructor
public MyStruct build() {
return new MyStruct (
MyStruct result = new MyStruct (
this.major,
this._package,
this.annotationWithQuote,
this.class_,
this.annotationWithTrailingComma,
this.emptyAnnotations
);
result.__isset_bit_vector.or(__optional_isset);
return result;
}
}

Expand Down Expand Up @@ -170,26 +176,62 @@ public MyStruct build() {

@ThriftField(value=1, name="major", requiredness=Requiredness.NONE)
public long getMajor() { return major; }

/** don't use this method for new code, it's here to make migrating to swift easier */
@Deprecated
public boolean fieldIsSetMajor() {
return __isset_bit_vector.get(_MAJOR);
}


@ThriftField(value=2, name="package", requiredness=Requiredness.NONE)
public String getPackage() { return _package; }

/** don't use this method for new code, it's here to make migrating to swift easier */
@Deprecated
public boolean fieldIsSetPackage() {
return this._package != null;
}


@ThriftField(value=3, name="annotation_with_quote", requiredness=Requiredness.NONE)
public String getAnnotationWithQuote() { return annotationWithQuote; }

/** don't use this method for new code, it's here to make migrating to swift easier */
@Deprecated
public boolean fieldIsSetAnnotationWithQuote() {
return this.annotationWithQuote != null;
}


@ThriftField(value=4, name="class_", requiredness=Requiredness.NONE)
public String getClass_() { return class_; }

/** don't use this method for new code, it's here to make migrating to swift easier */
@Deprecated
public boolean fieldIsSetClass_() {
return this.class_ != null;
}


@ThriftField(value=5, name="annotation_with_trailing_comma", requiredness=Requiredness.NONE)
public String getAnnotationWithTrailingComma() { return annotationWithTrailingComma; }

/** don't use this method for new code, it's here to make migrating to swift easier */
@Deprecated
public boolean fieldIsSetAnnotationWithTrailingComma() {
return this.annotationWithTrailingComma != null;
}


@ThriftField(value=6, name="empty_annotations", requiredness=Requiredness.NONE)
public String getEmptyAnnotations() { return emptyAnnotations; }

/** don't use this method for new code, it's here to make migrating to swift easier */
@Deprecated
public boolean fieldIsSetEmptyAnnotations() {
return this.emptyAnnotations != null;
}

@Override
public String toString() {
Expand Down
Loading

0 comments on commit e0b19cc

Please sign in to comment.