|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.flink.statefun.e2e.smoke.common; |
| 20 | + |
| 21 | +import org.apache.flink.statefun.e2e.smoke.generated.Commands; |
| 22 | +import org.apache.flink.statefun.e2e.smoke.generated.SourceCommand; |
| 23 | +import org.apache.flink.statefun.e2e.smoke.generated.VerificationResult; |
| 24 | +import org.apache.flink.statefun.sdk.TypeName; |
| 25 | +import org.apache.flink.statefun.sdk.reqreply.generated.TypedValue; |
| 26 | + |
| 27 | +public final class Types { |
| 28 | + private Types() {} |
| 29 | + |
| 30 | + public static final TypeName SOURCE_COMMANDS_TYPE = |
| 31 | + TypeName.parseFrom(Constants.NAMESPACE + "/source-command"); |
| 32 | + public static final TypeName VERIFICATION_RESULT_TYPE = |
| 33 | + TypeName.parseFrom(Constants.NAMESPACE + "/verification-result"); |
| 34 | + public static final TypeName COMMANDS_TYPE = |
| 35 | + TypeName.parseFrom(Constants.NAMESPACE + "/commands"); |
| 36 | + |
| 37 | + public static boolean isTypeOf(TypedValue value, TypeName type) { |
| 38 | + return value.getTypename().equals(type.canonicalTypenameString()); |
| 39 | + } |
| 40 | + |
| 41 | + public static TypedValue packSourceCommand(SourceCommand sourceCommand) { |
| 42 | + return TypedValue.newBuilder() |
| 43 | + .setTypename(SOURCE_COMMANDS_TYPE.canonicalTypenameString()) |
| 44 | + .setHasValue(true) |
| 45 | + .setValue(sourceCommand.toByteString()) |
| 46 | + .build(); |
| 47 | + } |
| 48 | + |
| 49 | + public static SourceCommand unpackSourceCommand(TypedValue typedValue) { |
| 50 | + if (!isTypeOf(typedValue, SOURCE_COMMANDS_TYPE)) { |
| 51 | + throw new IllegalStateException("Unexpected TypedValue: " + typedValue); |
| 52 | + } |
| 53 | + try { |
| 54 | + return SourceCommand.parseFrom(typedValue.getValue()); |
| 55 | + } catch (Exception e) { |
| 56 | + throw new RuntimeException("Unable to parse SourceCommand from TypedValue.", e); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public static TypedValue packCommands(Commands commands) { |
| 61 | + return TypedValue.newBuilder() |
| 62 | + .setTypename(COMMANDS_TYPE.canonicalTypenameString()) |
| 63 | + .setHasValue(true) |
| 64 | + .setValue(commands.toByteString()) |
| 65 | + .build(); |
| 66 | + } |
| 67 | + |
| 68 | + public static Commands unpackCommands(TypedValue typedValue) { |
| 69 | + if (!isTypeOf(typedValue, COMMANDS_TYPE)) { |
| 70 | + throw new IllegalStateException("Unexpected TypedValue: " + typedValue); |
| 71 | + } |
| 72 | + try { |
| 73 | + return Commands.parseFrom(typedValue.getValue()); |
| 74 | + } catch (Exception e) { |
| 75 | + throw new RuntimeException("Unable to parse Commands from TypedValue.", e); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + public static TypedValue packVerificationResult(VerificationResult verificationResult) { |
| 80 | + return TypedValue.newBuilder() |
| 81 | + .setTypename(VERIFICATION_RESULT_TYPE.canonicalTypenameString()) |
| 82 | + .setHasValue(true) |
| 83 | + .setValue(verificationResult.toByteString()) |
| 84 | + .build(); |
| 85 | + } |
| 86 | + |
| 87 | + public static VerificationResult unpackVerificationResult(TypedValue typedValue) { |
| 88 | + if (!isTypeOf(typedValue, VERIFICATION_RESULT_TYPE)) { |
| 89 | + throw new IllegalStateException("Unexpected TypedValue: " + typedValue); |
| 90 | + } |
| 91 | + try { |
| 92 | + return VerificationResult.parseFrom(typedValue.getValue()); |
| 93 | + } catch (Exception e) { |
| 94 | + throw new RuntimeException("Unable to parse SourceCommand from TypedValue.", e); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments