Skip to content
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
@@ -0,0 +1,79 @@
// Copyright 2020 Google LLC
//
// 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.

package com.google.api.generator.engine.ast;

import com.google.api.generator.engine.lexicon.Literal;
import com.google.auto.value.AutoValue;

@AutoValue
public abstract class PrimitiveValue implements Value {
static class TypeMismatchException extends RuntimeException {
public TypeMismatchException(String errorMessage) {
super(errorMessage);
}
}

// TODO(miraleung):F Handle object types.
public abstract TypeNode type();

public abstract String value();

public static Builder builder() {
return new AutoValue_PrimitiveValue.Builder();
}

@AutoValue.Builder
public abstract static class Builder {
public abstract Builder setType(TypeNode type);

public abstract Builder setValue(String value);

abstract PrimitiveValue autoBuild();

public PrimitiveValue build() throws TypeMismatchException {
// TODO(unsupported): byte, short, char, array initialization.
// TODO(miraleung): Object type checking?
PrimitiveValue primitiveValue = autoBuild();
TypeNode type = primitiveValue.type();
String value = primitiveValue.value();
if (!typeMatchesValue(type, value)) {
throw new TypeMismatchException(
String.format("Type %s does not match value %s", type.typeKind(), value));
}
return primitiveValue;
}

private static boolean typeMatchesValue(TypeNode type, String value) {
switch (type.typeKind()) {
case INT:
return Literal.isIntegerLiteral(value);
case LONG:
return Literal.isLongLiteral(value);
case FLOAT:
return Literal.isFloatLiteral(value);
case DOUBLE:
return Literal.isDoubleLiteral(value);
case BOOLEAN:
return Literal.isBooleanLiteral(value);
case OBJECT: // Fall through.
case BYTE: // Fall through.
case CHAR: // Fall through.
case SHORT: // Fall through.
default:
}
return false;
}
}
}
17 changes: 17 additions & 0 deletions src/main/java/com/google/api/generator/engine/ast/Value.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2020 Google LLC
//
// 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.

package com.google.api.generator.engine.ast;

public interface Value {}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package(default_visibility = ["//visibility:public"])

TESTS = [
"IdentifierNodeTest",
"PrimitiveValueTest",
]

filegroup(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2020 Google LLC
//
// 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.

package com.google.api.generator.engine.ast;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;

import com.google.api.generator.engine.ast.PrimitiveValue.TypeMismatchException;
import com.google.api.generator.engine.ast.TypeNode.TypeKind;
import org.junit.Test;

public class PrimitiveValueTest {
@Test
public void createPrimitiveValue_basic() {
assertValidValue(TypeKind.INT, "3");
assertValidValue(TypeKind.BOOLEAN, "false");
assertValidValue(TypeKind.LONG, "123");
assertValidValue(TypeKind.FLOAT, "123.f");
assertValidValue(TypeKind.DOUBLE, "123e10");
}

@Test
public void createPrimitiveValue_invalid() {
assertInvalidValue(TypeKind.INT, "123.f");
assertInvalidValue(TypeKind.INT, "false");
assertInvalidValue(TypeKind.BOOLEAN, "False");
assertInvalidValue(TypeKind.FLOAT, "asdfg");
}

@Test
public void createPrimitiveValue_unsupported() {
assertInvalidValue(TypeKind.BYTE, "0x2");
assertInvalidValue(TypeKind.SHORT, "1");
assertInvalidValue(TypeKind.CHAR, "a");
}

private static void assertValidValue(TypeKind typeKind, String value) {
TypeNode type = TypeNode.builder().setTypeKind(typeKind).build();
PrimitiveValue primitiveValue = PrimitiveValue.builder().setType(type).setValue(value).build();
assertThat(primitiveValue.value()).isEqualTo(value);
assertThat(primitiveValue.type()).isEqualTo(type);
}

private void assertInvalidValue(TypeKind typeKind, String value) {
TypeNode type = TypeNode.builder().setTypeKind(typeKind).build();
assertThrows(
TypeMismatchException.class,
() -> {
PrimitiveValue.builder().setType(type).setValue(value).build();
});
}
}