Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UUID to parsing for matching elements across instances #152

Merged
merged 4 commits into from
Oct 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Nullable;

Expand All @@ -44,6 +45,7 @@ public class Constant implements UserElement {
this.element = element;
this.namespaces = namespaces;
this.mixin = new UserElementMixin(
element.uuid(),
element.name(),
element.location(),
element.documentation(),
Expand Down Expand Up @@ -74,6 +76,11 @@ public String getNamespaceFor(NamespaceScope scope) {
return ns;
}

@Override
public UUID uuid() {
return mixin.uuid();
}

@Override
public String name() {
return mixin.name();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import com.google.common.collect.ImmutableMap;
import com.microsoft.thrifty.schema.parser.EnumMemberElement;

import java.util.UUID;

/**
* A named member of an {@link EnumType}.
*/
Expand All @@ -45,6 +47,11 @@ public int value() {
return value;
}

@Override
public UUID uuid() {
return mixin.uuid();
}

@Override
public String name() {
return mixin.name();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import com.microsoft.thrifty.schema.parser.ConstValueElement;
import com.microsoft.thrifty.schema.parser.FieldElement;

import java.util.UUID;

import javax.annotation.Nullable;

public class Field implements UserElement {
Expand Down Expand Up @@ -82,6 +84,11 @@ public String typedefName() {
return name;
}

@Override
public UUID uuid() {
return mixin.uuid();
}

@Override
public String name() {
return mixin.name();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class ServiceMethod implements UserElement {

Expand Down Expand Up @@ -73,6 +74,11 @@ public boolean oneWay() {
return element.oneWay();
}

@Override
public UUID uuid() {
return mixin.uuid();
}

@Override
public String name() {
return mixin.name();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,21 @@

import com.google.common.collect.ImmutableMap;

import java.util.UUID;

/**
* Represents data common to user-defined elements of a Thrift program.
*/
public interface UserElement {
/**
* A globally unique ID for this element. This is useful for cases where you newBuilder() an element to change it
* (such as in a Schema preprocessor) and want to update references from other objects in a deterministic way that
* matches IDs. If you want a new instance of an object that is unrelated, you should change this value.
*
* @return the uuid of this element.
*/
UUID uuid();

/**
* Gets the name of the element.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import java.util.Locale;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Nullable;

Expand All @@ -41,44 +42,48 @@
* which does not conveniently fit in a single base class.
*/
class UserElementMixin implements UserElement {
private final UUID uuid;
private final String name;
private final Location location;
private final String documentation;
private final ImmutableMap<String, String> annotations;

UserElementMixin(StructElement struct) {
this(struct.name(), struct.location(), struct.documentation(), struct.annotations());
this(struct.uuid(), struct.name(), struct.location(), struct.documentation(), struct.annotations());
}

UserElementMixin(FieldElement field) {
this(field.name(), field.location(), field.documentation(), field.annotations());
this(field.uuid(), field.name(), field.location(), field.documentation(), field.annotations());
}

UserElementMixin(EnumElement enumElement) {
this(enumElement.name(), enumElement.location(), enumElement.documentation(), enumElement.annotations());
this(enumElement.uuid(), enumElement.name(), enumElement.location(), enumElement.documentation(),
enumElement.annotations());
}

UserElementMixin(EnumMemberElement member) {
this(member.name(), member.location(), member.documentation(), member.annotations());
this(member.uuid(), member.name(), member.location(), member.documentation(), member.annotations());
}

UserElementMixin(TypedefElement element) {
this(element.newName(), element.location(), element.documentation(), element.annotations());
this(element.uuid(), element.newName(), element.location(), element.documentation(), element.annotations());
}

UserElementMixin(ServiceElement element) {
this(element.name(), element.location(), element.documentation(), element.annotations());
this(element.uuid(), element.name(), element.location(), element.documentation(), element.annotations());
}

UserElementMixin(FunctionElement element) {
this(element.name(), element.location(), element.documentation(), element.annotations());
this(element.uuid(), element.name(), element.location(), element.documentation(), element.annotations());
}

UserElementMixin(
UUID uuid,
String name,
Location location,
String documentation,
@Nullable AnnotationElement annotationElement) {
this.uuid = uuid;
this.name = name;
this.location = location;
this.documentation = documentation;
Expand All @@ -91,12 +96,18 @@ class UserElementMixin implements UserElement {
}

private UserElementMixin(Builder builder) {
this.uuid = builder.uuid;
this.name = builder.name;
this.location = builder.location;
this.documentation = builder.documentation;
this.annotations = builder.annotations;
}

@Override
public UUID uuid() {
return uuid;
}

@Override
public Location location() {
return location;
Expand Down Expand Up @@ -144,7 +155,8 @@ boolean hasThriftOrJavadocAnnotation(String name) {
@Override
public String toString() {
return "UserElementMixin{"
+ "name='" + name + '\''
+ "uuid='" + uuid + '\''
+ ", name='" + name + '\''
+ ", location=" + location
+ ", documentation='" + documentation + '\''
+ ", annotations=" + annotations
Expand All @@ -158,6 +170,7 @@ public boolean equals(Object o) {

UserElementMixin that = (UserElementMixin) o;

if (!uuid.equals(that.uuid)) return false;
if (!name.equals(that.name)) return false;
if (!location.equals(that.location)) return false;
if (!documentation.equals(that.documentation)) return false;
Expand All @@ -167,7 +180,8 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
int result = name.hashCode();
int result = uuid.hashCode();
result = 31 * result + name.hashCode();
result = 31 * result + location.hashCode();
result = 31 * result + documentation.hashCode();
result = 31 * result + annotations.hashCode();
Expand All @@ -179,18 +193,25 @@ Builder toBuilder() {
}

static class Builder {
private UUID uuid;
private String name;
private Location location;
private String documentation;
private ImmutableMap<String, String> annotations;

private Builder(UserElement userElement) {
this.uuid = userElement.uuid();
this.name = userElement.name();
this.location = userElement.location();
this.documentation = userElement.documentation();
this.annotations = userElement.annotations();
}

Builder uuid(UUID uuid) {
this.uuid = Preconditions.checkNotNull(uuid, "uuid");
return this;
}

Builder name(String name) {
this.name = Preconditions.checkNotNull(name, "name");
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import java.util.Map;
import java.util.Objects;
import java.util.UUID;

/**
* Base type of all user-defined Thrift IDL types, including structs, unions,
Expand Down Expand Up @@ -62,6 +63,11 @@ public ImmutableMap<NamespaceScope, String> namespaces() {
return namespaces;
}

@Override
public UUID uuid() {
return mixin.uuid();
}

@Override
public Location location() {
return mixin.location();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,22 @@
import com.google.auto.value.AutoValue;
import com.microsoft.thrifty.schema.Location;

import java.util.UUID;

@AutoValue
public abstract class ConstElement {
public abstract Location location();
public abstract String documentation();
public abstract TypeElement type();
public abstract String name();
public abstract UUID uuid();
public abstract ConstValueElement value();

public static Builder builder(Location location) {
return new AutoValue_ConstElement.Builder()
.location(location)
.documentation("");
.documentation("")
.uuid(ThriftyParserPlugins.createUUID());
}

ConstElement() { }
Expand All @@ -45,6 +49,7 @@ public interface Builder {
Builder documentation(String documentation);
Builder type(TypeElement type);
Builder name(String name);
Builder uuid(UUID uuid);
Builder value(ConstValueElement value);

ConstElement build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.microsoft.thrifty.schema.Location;

import java.util.List;
import java.util.UUID;

import javax.annotation.Nullable;

Expand All @@ -33,6 +34,7 @@ public abstract class EnumElement {
public abstract Location location();
public abstract String documentation();
public abstract String name();
public abstract UUID uuid();
public abstract ImmutableList<EnumMemberElement> members();
@Nullable public abstract AnnotationElement annotations();

Expand All @@ -41,14 +43,16 @@ public abstract class EnumElement {
public static Builder builder(Location location) {
return new AutoValue_EnumElement.Builder()
.location(location)
.documentation("");
.documentation("")
.uuid(ThriftyParserPlugins.createUUID());
}

@AutoValue.Builder
public interface Builder {
Builder location(Location location);
Builder documentation(String documentation);
Builder name(String name);
Builder uuid(UUID uuid);
Builder members(List<EnumMemberElement> members);
Builder annotations(AnnotationElement annotations);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,24 @@
import com.google.auto.value.AutoValue;
import com.microsoft.thrifty.schema.Location;

import java.util.UUID;

import javax.annotation.Nullable;

@AutoValue
public abstract class EnumMemberElement {
public abstract Location location();
public abstract String documentation();
public abstract String name();
public abstract UUID uuid();
public abstract int value();
@Nullable public abstract AnnotationElement annotations();

public static Builder builder(Location location) {
return new AutoValue_EnumMemberElement.Builder()
.location(location)
.documentation("");
.documentation("")
.uuid(ThriftyParserPlugins.createUUID());
}

EnumMemberElement() { }
Expand All @@ -46,6 +50,7 @@ public interface Builder {
Builder location(Location location);
Builder documentation(String documentation);
Builder name(String name);
Builder uuid(UUID uuid);
Builder value(int value);
Builder annotations(AnnotationElement annotations);

Expand Down
Loading