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
Expand Up @@ -32,6 +32,8 @@ alloy.common.LanguageTagFormatTrait$Provider
alloy.openapi.OpenApiExtensionsTrait$Provider
alloy.openapi.SummaryTrait$Provider
alloy.proto.GrpcTrait$Provider
alloy.proto.GrpcErrorTrait$Provider
alloy.proto.GrpcErrorMessageTrait$Provider
alloy.proto.ProtoCompactLocalDateTrait$Provider
alloy.proto.ProtoCompactLocalTimeTrait$Provider
alloy.proto.ProtoCompactMonthDayTrait$Provider
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
alloy.proto.validation.GrpcTraitValidator
alloy.proto.validation.GrpcErrorTraitValidator
alloy.proto.validation.ProtoIndexTraitValidator
alloy.proto.validation.ProtoInlinedOneOfValidator
alloy.proto.validation.ProtoReservedFieldsTraitValidator
Expand Down
2 changes: 2 additions & 0 deletions modules/core/resources/META-INF/smithy/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ examples.smithy
openapi/openapi.smithy
presence.smithy
proto/proto.smithy
proto/grpc-status.smithy
proto/status.smithy
restjson.smithy
string.smithy
jsonunknown.smithy
Expand Down
39 changes: 39 additions & 0 deletions modules/core/resources/META-INF/smithy/proto/grpc-status.smithy
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
$version: "2"

namespace alloy.proto

use alloy#openEnum

@openEnum
intEnum GrpcStatusCode {
OK = 0
CANCELLED = 1
UNKNOWN = 2
INVALID_ARGUMENT = 3
DEADLINE_EXCEEDED = 4
NOT_FOUND = 5
ALREADY_EXISTS = 6
PERMISSION_DENIED = 7
RESOURCE_EXHAUSTED = 8
FAILED_PRECONDITION = 9
ABORTED = 10
OUT_OF_RANGE = 11
UNIMPLEMENTED = 12
INTERNAL = 13
UNAVAILABLE = 14
DATA_LOSS = 15
UNAUTHENTICATED = 16
}

@trait(selector: "structure[trait|smithy.api#error]")
structure grpcError {
@required
code: Integer
message: String
}

@trait(
selector: "structure[trait|alloy.proto#grpcError] > member :test(> string)"
structurallyExclusive: "member"
)
structure grpcErrorMessage {}
26 changes: 26 additions & 0 deletions modules/core/resources/META-INF/smithy/proto/status.smithy
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
$version: "2"

namespace alloy.proto

structure ProtobufAny {
@protoIndex(1)
typeUrl: String

@protoIndex(2)
value: Blob
}

list ProtobufAnyList {
member: ProtobufAny
}

structure GoogleRpcStatus {
@protoIndex(1)
code: Integer

@protoIndex(2)
message: String

@protoIndex(3)
details: ProtobufAnyList
}
46 changes: 46 additions & 0 deletions modules/core/src/alloy/proto/GrpcErrorMessageTrait.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* Copyright 2022 Disney Streaming
*
* Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://disneystreaming.github.io/TOST-1.0.txt
*
* 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 alloy.proto;

import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.node.ObjectNode;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.traits.AbstractTrait;
import software.amazon.smithy.model.traits.AnnotationTrait;

public final class GrpcErrorMessageTrait extends AnnotationTrait {

public static ShapeId ID = ShapeId.from("alloy.proto#grpcErrorMessage");

public GrpcErrorMessageTrait(ObjectNode node) {
super(ID, node);
}

public GrpcErrorMessageTrait() {
super(ID, Node.objectNode());
}

public static final class Provider extends AbstractTrait.Provider {
public Provider() {
super(ID);
}

@Override
public GrpcErrorMessageTrait createTrait(ShapeId target, Node node) {
return new GrpcErrorMessageTrait(node.expectObjectNode());
}
}
}
135 changes: 135 additions & 0 deletions modules/core/src/alloy/proto/GrpcErrorTrait.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/* Copyright 2022 Disney Streaming
*
* Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://disneystreaming.github.io/TOST-1.0.txt
*
* 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 alloy.proto;

import software.amazon.smithy.model.FromSourceLocation;
import software.amazon.smithy.model.SourceException;
import software.amazon.smithy.model.SourceLocation;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.node.ObjectNode;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.traits.AbstractTrait;
import software.amazon.smithy.model.traits.Trait;

import java.util.Optional;

public final class GrpcErrorTrait extends AbstractTrait {
public static final ShapeId ID = ShapeId.from("alloy.proto#grpcError");

private final int code;
private final String message;

public GrpcErrorTrait(int code, String message, FromSourceLocation sourceLocation) {
super(ID, sourceLocation);
this.code = code;
this.message = message;
}

public GrpcErrorTrait(int code, String message) {
this(code, message, SourceLocation.NONE);
}

public GrpcErrorTrait(int code) {
this(code, null, SourceLocation.NONE);
}

public int getCode() {
return code;
}

public Optional<String> getMessage() {
return Optional.ofNullable(message);
}

public static final class Provider extends AbstractTrait.Provider {
public Provider() {
super(ID);
}

@Override
public Trait createTrait(ShapeId target, Node value) {
ObjectNode obj = value.expectObjectNode();

Node codeNode = obj.getMember("code").orElseThrow(
() -> new SourceException("grpcError requires a 'code' field", value.getSourceLocation())
);

int code;
if (codeNode.isNumberNode()) {
code = codeNode.expectNumberNode().getValue().intValue();
} else if (codeNode.isStringNode()) {
code = parseSymbol(codeNode.expectStringNode().getValue(), codeNode.getSourceLocation());
} else {
throw new SourceException("grpcError 'code' must be a number or enum symbol", codeNode.getSourceLocation());
}

String message = obj.getStringMember("message").map(n -> n.getValue()).orElse(null);

return new GrpcErrorTrait(code, message, value.getSourceLocation());
}
}

@Override
protected Node createNode() {
ObjectNode.Builder builder = Node.objectNodeBuilder()
.withMember("code", Node.from(code));
if (message != null) {
builder.withMember("message", Node.from(message));
}
return builder.build();
}

private static int parseSymbol(String value, SourceLocation sourceLocation) {
switch (value) {
case "OK":
return 0;
case "CANCELLED":
return 1;
case "UNKNOWN":
return 2;
case "INVALID_ARGUMENT":
return 3;
case "DEADLINE_EXCEEDED":
return 4;
case "NOT_FOUND":
return 5;
case "ALREADY_EXISTS":
return 6;
case "PERMISSION_DENIED":
return 7;
case "RESOURCE_EXHAUSTED":
return 8;
case "FAILED_PRECONDITION":
return 9;
case "ABORTED":
return 10;
case "OUT_OF_RANGE":
return 11;
case "UNIMPLEMENTED":
return 12;
case "INTERNAL":
return 13;
case "UNAVAILABLE":
return 14;
case "DATA_LOSS":
return 15;
case "UNAUTHENTICATED":
return 16;
default:
throw new SourceException("Unknown grpcError code: " + value, sourceLocation);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* Copyright 2022 Disney Streaming
*
* Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://disneystreaming.github.io/TOST-1.0.txt
*
* 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 alloy.proto.validation;

import alloy.proto.GrpcErrorTrait;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.validation.AbstractValidator;
import software.amazon.smithy.model.validation.Severity;
import software.amazon.smithy.model.validation.ValidationEvent;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public final class GrpcErrorTraitValidator extends AbstractValidator {
public static final String GRPC_ERROR_NON_STANDARD = "GrpcErrorNonStandard";

@Override
public List<ValidationEvent> validate(Model model) {
return model.getShapesWithTrait(GrpcErrorTrait.class).stream()
.map(shape -> validateTrait(shape, shape.expectTrait(GrpcErrorTrait.class)))
.flatMap(List::stream)
.collect(Collectors.toList());
}

private List<ValidationEvent> validateTrait(Shape shape, GrpcErrorTrait trait) {
int code = trait.getCode();
if (code < 0 || code > 16) {
return Collections.singletonList(ValidationEvent.builder()
.id(GRPC_ERROR_NON_STANDARD)
.severity(Severity.WARNING)
.shape(shape)
.message("grpcError code is outside the standard gRPC range (0..16); many runtimes coerce unknown codes to UNKNOWN")
.build());
}

return Collections.emptyList();
}
}
9 changes: 9 additions & 0 deletions modules/core/test/resources/META-INF/smithy/traits.smithy
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ use alloy.proto#protoCompactMonthDay
use alloy.proto#protoOffsetDateTimeFormat
use alloy.proto#protoWrapped
use alloy.proto#protoReservedFields
use alloy.proto#grpcError
use alloy.proto#grpcErrorMessage
use alloy#localTimeFormat
use alloy#localDateTimeFormat
use alloy#offsetTimeFormat
Expand Down Expand Up @@ -149,6 +151,13 @@ structure ProtoStructTwo {
enum: TestEnum
}

@error("client")
@grpcError(code: "UNAUTHENTICATED")
structure GrpcStatusError {
@grpcErrorMessage
message: String
}

enum TestEnum {
A
}
Expand Down
Loading
Loading