|
1 | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
2 | 2 | // Licensed under the MIT License. |
| 3 | + |
3 | 4 | package com.microsoft.typespec.http.client.generator.core.util; |
4 | 5 |
|
| 6 | +import com.microsoft.typespec.http.client.generator.core.Javagen; |
| 7 | +import com.microsoft.typespec.http.client.generator.core.extension.plugin.PluginLogger; |
5 | 8 | import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClassType; |
6 | 9 | import com.microsoft.typespec.http.client.generator.core.model.clientmodel.IType; |
7 | 10 | import com.microsoft.typespec.http.client.generator.core.model.clientmodel.PrimitiveType; |
|
10 | 13 | import java.time.Duration; |
11 | 14 | import java.time.Instant; |
12 | 15 | import java.time.ZoneOffset; |
| 16 | +import org.slf4j.Logger; |
13 | 17 |
|
14 | 18 | /** |
15 | 19 | * Class to group conversion logic between client type and wire type. |
16 | 20 | */ |
17 | 21 | public class WireTypeClientTypeConverter { |
18 | 22 |
|
| 23 | + private final static Logger LOGGER |
| 24 | + = new PluginLogger(Javagen.getPluginInstance(), WireTypeClientTypeConverter.class); |
| 25 | + |
19 | 26 | private WireTypeClientTypeConverter() { |
20 | 27 | } |
21 | 28 |
|
@@ -158,20 +165,27 @@ public static String convertToWireTypeExpression(PrimitiveType clientType, Strin |
158 | 165 | */ |
159 | 166 | public static String convertLiteralToClientValue(IType wireType, String literalInWireType) { |
160 | 167 | String literalValue = literalInWireType; |
161 | | - if (wireType == ClassType.DATE_TIME_RFC_1123) { |
162 | | - literalValue = new DateTimeRfc1123(literalValue).getDateTime().toString(); |
163 | | - } else if (wireType == ClassType.BASE_64_URL) { |
164 | | - literalValue = new Base64Uri(literalValue).toString(); |
165 | | - } else if (wireType.asNullable() == ClassType.UNIX_TIME_LONG) { |
166 | | - literalValue = Instant.ofEpochSecond(Long.parseLong(literalValue)).atOffset(ZoneOffset.UTC).toString(); |
167 | | - } else if (wireType.asNullable() == ClassType.DURATION_LONG) { |
168 | | - literalValue = Duration.ofSeconds(Long.parseLong(literalValue)).toString(); |
169 | | - } else if (wireType.asNullable() == ClassType.DURATION_DOUBLE) { |
170 | | - literalValue = Duration.ofNanos((long) (Double.parseDouble(literalInWireType) * 1000_000_000L)).toString(); |
171 | | - } else if (wireType.asNullable() == ClassType.DURATION_MILLISECONDS_LONG) { |
172 | | - literalValue = Duration.ofMillis(Long.parseLong(literalValue)).toString(); |
173 | | - } else if (wireType.asNullable() == ClassType.DURATION_MILLISECONDS_DOUBLE) { |
174 | | - literalValue = Duration.ofNanos((long) (Double.parseDouble(literalInWireType) * 1000_000L)).toString(); |
| 168 | + try { |
| 169 | + if (wireType == ClassType.DATE_TIME_RFC_1123) { |
| 170 | + literalValue = new DateTimeRfc1123(literalValue).getDateTime().toString(); |
| 171 | + } else if (wireType == ClassType.BASE_64_URL) { |
| 172 | + literalValue = new Base64Uri(literalValue).toString(); |
| 173 | + } else if (wireType.asNullable() == ClassType.UNIX_TIME_LONG) { |
| 174 | + literalValue = Instant.ofEpochSecond(Long.parseLong(literalValue)).atOffset(ZoneOffset.UTC).toString(); |
| 175 | + } else if (wireType.asNullable() == ClassType.DURATION_LONG) { |
| 176 | + literalValue = Duration.ofSeconds(Long.parseLong(literalValue)).toString(); |
| 177 | + } else if (wireType.asNullable() == ClassType.DURATION_DOUBLE) { |
| 178 | + literalValue |
| 179 | + = Duration.ofNanos((long) (Double.parseDouble(literalInWireType) * 1000_000_000L)).toString(); |
| 180 | + } else if (wireType.asNullable() == ClassType.DURATION_MILLISECONDS_LONG) { |
| 181 | + literalValue = Duration.ofMillis(Long.parseLong(literalValue)).toString(); |
| 182 | + } else if (wireType.asNullable() == ClassType.DURATION_MILLISECONDS_DOUBLE) { |
| 183 | + literalValue = Duration.ofNanos((long) (Double.parseDouble(literalInWireType) * 1000_000L)).toString(); |
| 184 | + } |
| 185 | + } catch (RuntimeException e) { |
| 186 | + LOGGER.warn( |
| 187 | + "Failed to convert literal value '{}' from wire type to client type. Return the original literal value. Error: {}", |
| 188 | + literalInWireType, e.getMessage()); |
175 | 189 | } |
176 | 190 | return literalValue; |
177 | 191 | } |
|
0 commit comments