• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

temporalio / sdk-java / #333

16 Oct 2024 07:28PM UTC coverage: 78.65% (+0.6%) from 78.085%
#333

push

github

web-flow
Fix code coverage (#2275)

22670 of 28824 relevant lines covered (78.65%)

0.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

73.68
/temporal-sdk/src/main/java/io/temporal/internal/common/ProtoEnumNameUtils.java
1
/*
2
 * Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved.
3
 *
4
 * Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5
 *
6
 * Modifications copyright (C) 2017 Uber Technologies, Inc.
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this material except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *   http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20

21
package io.temporal.internal.common;
22

23
import com.google.common.base.CaseFormat;
24
import com.google.common.collect.ImmutableMap;
25
import io.temporal.api.enums.v1.CommandType;
26
import io.temporal.api.enums.v1.EventType;
27
import io.temporal.api.enums.v1.IndexedValueType;
28
import io.temporal.api.enums.v1.WorkflowTaskFailedCause;
29
import java.util.Map;
30
import javax.annotation.Nonnull;
31

32
/**
33
 * Because in Go enums are in a shared namespace, one of protobuf common practices is to prefix the
34
 * enums with enum name prefix to create unique names. The final result is verbose and when we use a
35
 * string representation of some enum in different parts of the system (history json or search
36
 * attribute types), we use a short version without prefix in camel case. So, protobuf enum {@link
37
 * io.temporal.api.enums.v1.EventType#EVENT_TYPE_WORKFLOW_EXECUTION_STARTED} gets converted to
38
 * "WorkflowExecutionStarted"
39
 *
40
 * <p><a href="https://google.aip.dev/126">AIP 126</a> <br>
41
 * - The other values should not be prefixed by the name of the enum itself. This generally requires
42
 * users to write MyState.MYSTATE_ACTIVE in their code, which is unnecessarily verbose. - To avoid
43
 * sharing values, APIs may prefix enum values with the name of the enum. In this case, they must do
44
 * so consistently within the enum.
45
 *
46
 * @see <a href="https://google.aip.dev/126">AIP 126</a>
47
 * @see <a
48
 *     href="https://github.com/temporalio/gogo-protobuf/commit/b38fb010909b8f81e2e600dc6f04925fc71d6a5e">
49
 *     Related commit to Go Proto module</a>
50
 */
51
public class ProtoEnumNameUtils {
×
52
  public static final String WORKFLOW_TASK_FAILED_CAUSE_PREFIX = "WORKFLOW_TASK_FAILED_CAUSE_";
53
  public static final String INDEXED_VALUE_TYPE_PREFIX = "INDEXED_VALUE_TYPE_";
54
  public static final String COMMAND_TYPE_PREFIX = "COMMAND_TYPE_";
55
  public static final String EVENT_TYPE_PREFIX = "EVENT_TYPE_";
56

57
  public static final Map<Class<?>, String> ENUM_CLASS_TO_PREFIX =
1✔
58
      ImmutableMap.of(
1✔
59
          WorkflowTaskFailedCause.class,
60
          WORKFLOW_TASK_FAILED_CAUSE_PREFIX,
61
          IndexedValueType.class,
62
          INDEXED_VALUE_TYPE_PREFIX,
63
          CommandType.class,
64
          COMMAND_TYPE_PREFIX,
65
          EventType.class,
66
          EVENT_TYPE_PREFIX);
67

68
  @Nonnull
69
  public static String uniqueToSimplifiedName(@Nonnull Enum<?> enumm) {
70
    String protoEnumName = enumm.name();
1✔
71
    String prefix = ENUM_CLASS_TO_PREFIX.get(enumm.getClass());
1✔
72
    if (prefix == null) {
1✔
73
      throw new IllegalStateException(
×
74
          "Enum "
75
              + enumm.getClass()
×
76
              + " must be explicitly registered in #ENUM_CLASS_TO_PREFIX to be used in this method");
77
    }
78
    if (!protoEnumName.startsWith(prefix)) {
1✔
79
      throw new IllegalArgumentException("protoEnumName should start with " + prefix + " prefix");
×
80
    }
81
    protoEnumName = protoEnumName.substring(prefix.length());
1✔
82
    return screamingCaseEventTypeToCamelCase(protoEnumName);
1✔
83
  }
84

85
  public static String uniqueToSimplifiedName(String protoEnumName, String prefix) {
86
    if (!protoEnumName.startsWith(prefix)) {
1✔
87
      throw new IllegalArgumentException("protoEnumName should start with " + prefix + " prefix");
×
88
    }
89
    protoEnumName = protoEnumName.substring(prefix.length());
1✔
90
    return screamingCaseEventTypeToCamelCase(protoEnumName);
1✔
91
  }
92

93
  public static String simplifiedToUniqueName(String enumName, String prefix) {
94
    return prefix + camelCaseToScreamingCase(enumName);
1✔
95
  }
96

97
  // https://github.com/temporalio/gogo-protobuf/commit/b38fb010909b8f81e2e600dc6f04925fc71d6a5e
98
  private static String camelCaseToScreamingCase(String camel) {
99
    return CaseFormat.UPPER_CAMEL.converterTo(CaseFormat.UPPER_UNDERSCORE).convert(camel);
1✔
100
  }
101

102
  // https://github.com/temporalio/gogo-protobuf/commit/b38fb010909b8f81e2e600dc6f04925fc71d6a5e
103
  private static String screamingCaseEventTypeToCamelCase(String screaming) {
104
    return CaseFormat.UPPER_UNDERSCORE.converterTo(CaseFormat.UPPER_CAMEL).convert(screaming);
1✔
105
  }
106
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc