Skip to content

Commit

Permalink
Merge pull request #255 from SpineEventEngine/generate-type-url-for-j…
Browse files Browse the repository at this point in the history
…s-messages

Generate a method providing `TypeUrl` for Protobuf types compiled to JS
  • Loading branch information
dmytro-grankin authored Dec 27, 2018
2 parents 998d69d + 9eb7ce1 commit e876685
Show file tree
Hide file tree
Showing 107 changed files with 3,034 additions and 1,109 deletions.
60 changes: 0 additions & 60 deletions base/src/main/java/io/spine/code/Generation.java

This file was deleted.

50 changes: 50 additions & 0 deletions base/src/main/java/io/spine/code/generate/GeneratedBySpine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2018, TeamDev. All rights reserved.
*
* Redistribution and use in source and/or binary forms, with or without
* modification, must retain the above copyright notice and the following
* disclaimer.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package io.spine.code.generate;

/**
* Provides fields for {@link javax.annotation.Generated Generated} annotation used by
* code generation tools for marking source code generated by Spine Model Compiler.
*/
public final class GeneratedBySpine {

private static final GeneratedBySpine INSTANCE = new GeneratedBySpine();

/** Prevents instantiation from outside. */
private GeneratedBySpine() {
}

public static GeneratedBySpine instance() {
return INSTANCE;
}

/** Obtains the name for annotation field. */
@SuppressWarnings("DuplicateStringLiteralInspection")
// Each occurrence has a different semantics.
public String getFieldName() {
return "value";
}

/** Obtains the code block for the annotation value. */
public String getCodeBlock() {
return "\"by Spine Model Compiler\"";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package io.spine.code;
package io.spine.code.generate;

import com.google.common.base.Strings;
import io.spine.value.StringTypeValue;
Expand Down
110 changes: 110 additions & 0 deletions base/src/main/java/io/spine/code/generate/IndentLevel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright 2018, TeamDev. All rights reserved.
*
* Redistribution and use in source and/or binary forms, with or without
* modification, must retain the above copyright notice and the following
* disclaimer.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package io.spine.code.generate;

import java.util.Objects;

import static com.google.common.base.Preconditions.checkArgument;

/**
* An indent level, which determines a number of {@linkplain Indent indents} to align code with.
*
* <p>The value cannot be negative.
*/
public class IndentLevel {

private static final IndentLevel ZERO = of(0);

private final int value;

private IndentLevel(int value) {
checkArgument(value >= 0, "An indent level cannot be negative.");
this.value = value;
}

/**
* Creates an indent level with the specified value.
*/
public static IndentLevel of(int value) {
return new IndentLevel(value);
}

/**
* Creates the indent level of zero.
*/
public static IndentLevel zero() {
return ZERO;
}

/**
* Obtains the value of the level.
*/
public int value() {
return value;
}

/**
* Obtains the indent level by incrementing this value.
*/
public IndentLevel incremented() {
return of(value + 1);
}

/**
* Obtains the indent level by decrementing this value.
*/
public IndentLevel decremented() {
return of(value - 1);
}

/**
* Obtains the total indent for the level.
*
* @param indentPerLevel
* the indent per a level
*/
public Indent totalIndent(Indent indentPerLevel) {
int totalSize = indentPerLevel.getSize() * value;
return Indent.of(totalSize);
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof IndentLevel)) {
return false;
}
IndentLevel level = (IndentLevel) o;
return value == level.value;
}

@Override
public int hashCode() {
return Objects.hash(value);
}

@Override
public String toString() {
return String.valueOf(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
*/

/**
* The classes which generate the JS code working with known Protobuf types.
* This package contains language-neutral classes and interfaces for code generation.
*/
@Internal
@CheckReturnValue
@ParametersAreNonnullByDefault
package io.spine.js.generate.type;
package io.spine.code.generate;

import com.google.errorprone.annotations.CheckReturnValue;
import io.spine.annotation.Internal;

import javax.annotation.ParametersAreNonnullByDefault;
97 changes: 97 additions & 0 deletions base/src/main/java/io/spine/code/js/MethodReference.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright 2018, TeamDev. All rights reserved.
*
* Redistribution and use in source and/or binary forms, with or without
* modification, must retain the above copyright notice and the following
* disclaimer.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package io.spine.code.js;

import java.util.Objects;

import static com.google.common.base.Preconditions.checkNotNull;

/**
* A reference to a method of a Protobuf type.
*
* <p>The reference allows to identify a method in code
* since it includes a type name and a method name.
*/
public final class MethodReference {

/** The type declaring the method. */
private final TypeName typeName;
/** The name of the method. */
private final String methodName;
/** Whether the method is defined on the prototype or not. */
private final boolean onPrototype;

private MethodReference(TypeName typeName, String methodName, boolean onPrototype) {
checkNotNull(typeName);
checkNotNull(methodName);
this.typeName = typeName;
this.methodName = methodName;
this.onPrototype = onPrototype;
}

/**
* Obtains the reference to the instance method of the specified type.
*/
public static MethodReference onType(TypeName typeName, String methodName) {
return new MethodReference(typeName, methodName, false);
}

/**
* Obtains the reference to the static method of the specified type.
*/
public static MethodReference onPrototype(TypeName typeName, String methodName) {
return new MethodReference(typeName, methodName, true);
}

/**
* Obtains the value of the reference.
*/
public String value() {
String delimiter = onPrototype
? ".prototype."
: ".";
return typeName + delimiter + methodName;
}

@Override
public String toString() {
return value();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof MethodReference)) {
return false;
}
MethodReference reference = (MethodReference) o;
return onPrototype == reference.onPrototype &&
typeName.equals(reference.typeName) &&
methodName.equals(reference.methodName);
}

@Override
public int hashCode() {
return Objects.hash(typeName, methodName, onPrototype);
}
}
16 changes: 4 additions & 12 deletions base/src/main/java/io/spine/code/js/TypeName.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@

package io.spine.code.js;

import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.Descriptors.EnumDescriptor;
import com.google.protobuf.Descriptors.GenericDescriptor;
import io.spine.value.StringTypeValue;

import static com.google.common.base.Preconditions.checkNotNull;
Expand All @@ -46,16 +45,9 @@ private TypeName(String value) {
super(value);
}

public static TypeName from(Descriptor messageDescriptor) {
checkNotNull(messageDescriptor);
String typeName = messageDescriptor.getFullName();
String nameWithPrefix = PREFIX + typeName;
return new TypeName(nameWithPrefix);
}

public static TypeName from(EnumDescriptor enumDescriptor) {
checkNotNull(enumDescriptor);
String typeName = enumDescriptor.getFullName();
public static TypeName from(GenericDescriptor descriptor) {
checkNotNull(descriptor);
String typeName = descriptor.getFullName();
String nameWithPrefix = PREFIX + typeName;
return new TypeName(nameWithPrefix);
}
Expand Down
Loading

0 comments on commit e876685

Please sign in to comment.