Skip to content

Commit

Permalink
tsp, correct way to write encode on scalar (#2643)
Browse files Browse the repository at this point in the history
  • Loading branch information
weidongxu-microsoft authored Mar 27, 2024
1 parent 9efebaf commit bc9493b
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public final class OptionalAsyncClient {
* bytesDict (Optional): {
* String: byte[] (Required)
* }
* epochDateTimeRequiredNullable: Long (Required)
* epochDateTimeNullable: Long (Optional)
* }
* }</pre>
* <p><strong>Response Body Schema</strong></p>
Expand All @@ -108,6 +110,8 @@ public final class OptionalAsyncClient {
* bytesDict (Optional): {
* String: byte[] (Required)
* }
* epochDateTimeRequiredNullable: Long (Optional)
* epochDateTimeNullable: Long (Optional)
* immutable (Optional): {
* stringReadWriteRequired: String (Required)
* stringReadOnlyOptional: String (Optional)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public final class OptionalClient {
* bytesDict (Optional): {
* String: byte[] (Required)
* }
* epochDateTimeRequiredNullable: Long (Required)
* epochDateTimeNullable: Long (Optional)
* }
* }</pre>
* <p><strong>Response Body Schema</strong></p>
Expand All @@ -106,6 +108,8 @@ public final class OptionalClient {
* bytesDict (Optional): {
* String: byte[] (Required)
* }
* epochDateTimeRequiredNullable: Long (Optional)
* epochDateTimeNullable: Long (Optional)
* immutable (Optional): {
* stringReadWriteRequired: String (Required)
* stringReadOnlyOptional: String (Optional)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ Response<BinaryData> putSync(@HostParam("endpoint") String endpoint,
* bytesDict (Optional): {
* String: byte[] (Required)
* }
* epochDateTimeRequiredNullable: Long (Required)
* epochDateTimeNullable: Long (Optional)
* }
* }</pre>
* <p><strong>Response Body Schema</strong></p>
Expand All @@ -155,6 +157,8 @@ Response<BinaryData> putSync(@HostParam("endpoint") String endpoint,
* bytesDict (Optional): {
* String: byte[] (Required)
* }
* epochDateTimeRequiredNullable: Long (Optional)
* epochDateTimeNullable: Long (Optional)
* immutable (Optional): {
* stringReadWriteRequired: String (Required)
* stringReadOnlyOptional: String (Optional)
Expand Down Expand Up @@ -232,6 +236,8 @@ public Mono<Response<BinaryData>> putWithResponseAsync(String requestHeaderRequi
* bytesDict (Optional): {
* String: byte[] (Required)
* }
* epochDateTimeRequiredNullable: Long (Required)
* epochDateTimeNullable: Long (Optional)
* }
* }</pre>
* <p><strong>Response Body Schema</strong></p>
Expand All @@ -258,6 +264,8 @@ public Mono<Response<BinaryData>> putWithResponseAsync(String requestHeaderRequi
* bytesDict (Optional): {
* String: byte[] (Required)
* }
* epochDateTimeRequiredNullable: Long (Optional)
* epochDateTimeNullable: Long (Optional)
* immutable (Optional): {
* stringReadWriteRequired: String (Required)
* stringReadOnlyOptional: String (Optional)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import com.azure.json.JsonWriter;
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -125,6 +127,18 @@ public final class AllPropertiesOptional implements JsonSerializable<AllProperti
@Generated
private Map<String, byte[]> bytesDict;

/*
* The epochDateTimeRequiredNullable property.
*/
@Generated
private Long epochDateTimeRequiredNullable;

/*
* The epochDateTimeNullable property.
*/
@Generated
private Long epochDateTimeNullable;

/*
* The immutable property.
*/
Expand Down Expand Up @@ -308,6 +322,32 @@ public Map<String, byte[]> getBytesDict() {
return this.bytesDict;
}

/**
* Get the epochDateTimeRequiredNullable property: The epochDateTimeRequiredNullable property.
*
* @return the epochDateTimeRequiredNullable value.
*/
@Generated
public OffsetDateTime getEpochDateTimeRequiredNullable() {
if (this.epochDateTimeRequiredNullable == null) {
return null;
}
return OffsetDateTime.ofInstant(Instant.ofEpochSecond(this.epochDateTimeRequiredNullable), ZoneOffset.UTC);
}

/**
* Get the epochDateTimeNullable property: The epochDateTimeNullable property.
*
* @return the epochDateTimeNullable value.
*/
@Generated
public OffsetDateTime getEpochDateTimeNullable() {
if (this.epochDateTimeNullable == null) {
return null;
}
return OffsetDateTime.ofInstant(Instant.ofEpochSecond(this.epochDateTimeNullable), ZoneOffset.UTC);
}

/**
* Get the immutable property: The immutable property.
*
Expand Down Expand Up @@ -343,6 +383,8 @@ public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
this.dateTime == null ? null : DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(this.dateTime));
jsonWriter.writeArrayField("stringList", this.stringList, (writer, element) -> writer.writeString(element));
jsonWriter.writeMapField("bytesDict", this.bytesDict, (writer, element) -> writer.writeBinary(element));
jsonWriter.writeNumberField("epochDateTimeRequiredNullable", this.epochDateTimeRequiredNullable);
jsonWriter.writeNumberField("epochDateTimeNullable", this.epochDateTimeNullable);
jsonWriter.writeJsonField("immutable", this.immutable);
return jsonWriter.writeEndObject();
}
Expand Down Expand Up @@ -401,6 +443,11 @@ public static AllPropertiesOptional fromJson(JsonReader jsonReader) throws IOExc
} else if ("bytesDict".equals(fieldName)) {
Map<String, byte[]> bytesDict = reader.readMap(reader1 -> reader1.getBinary());
deserializedAllPropertiesOptional.bytesDict = bytesDict;
} else if ("epochDateTimeRequiredNullable".equals(fieldName)) {
deserializedAllPropertiesOptional.epochDateTimeRequiredNullable
= reader.getNullable(JsonReader::getLong);
} else if ("epochDateTimeNullable".equals(fieldName)) {
deserializedAllPropertiesOptional.epochDateTimeNullable = reader.getNullable(JsonReader::getLong);
} else if ("immutable".equals(fieldName)) {
deserializedAllPropertiesOptional.immutable = ImmutableModel.fromJson(reader);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import com.azure.json.JsonWriter;
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -125,21 +127,35 @@ public final class Optional implements JsonSerializable<Optional> {
@Generated
private Map<String, byte[]> bytesDict;

/*
* The epochDateTimeRequiredNullable property.
*/
@Generated
private final Long epochDateTimeRequiredNullable;

/*
* The epochDateTimeNullable property.
*/
@Generated
private Long epochDateTimeNullable;

/**
* Creates an instance of Optional class.
*
* @param booleanRequired the booleanRequired value to set.
* @param booleanRequiredNullable the booleanRequiredNullable value to set.
* @param stringRequired the stringRequired value to set.
* @param stringRequiredNullable the stringRequiredNullable value to set.
* @param epochDateTimeRequiredNullable the epochDateTimeRequiredNullable value to set.
*/
@Generated
public Optional(boolean booleanRequired, Boolean booleanRequiredNullable, String stringRequired,
String stringRequiredNullable) {
String stringRequiredNullable, OffsetDateTime epochDateTimeRequiredNullable) {
this.booleanRequired = booleanRequired;
this.booleanRequiredNullable = booleanRequiredNullable;
this.stringRequired = stringRequired;
this.stringRequiredNullable = stringRequiredNullable;
this.epochDateTimeRequiredNullable = epochDateTimeRequiredNullable.toEpochSecond();
}

/**
Expand Down Expand Up @@ -468,6 +484,48 @@ public Optional setBytesDict(Map<String, byte[]> bytesDict) {
return this;
}

/**
* Get the epochDateTimeRequiredNullable property: The epochDateTimeRequiredNullable property.
*
* @return the epochDateTimeRequiredNullable value.
*/
@Generated
public OffsetDateTime getEpochDateTimeRequiredNullable() {
if (this.epochDateTimeRequiredNullable == null) {
return null;
}
return OffsetDateTime.ofInstant(Instant.ofEpochSecond(this.epochDateTimeRequiredNullable), ZoneOffset.UTC);
}

/**
* Get the epochDateTimeNullable property: The epochDateTimeNullable property.
*
* @return the epochDateTimeNullable value.
*/
@Generated
public OffsetDateTime getEpochDateTimeNullable() {
if (this.epochDateTimeNullable == null) {
return null;
}
return OffsetDateTime.ofInstant(Instant.ofEpochSecond(this.epochDateTimeNullable), ZoneOffset.UTC);
}

/**
* Set the epochDateTimeNullable property: The epochDateTimeNullable property.
*
* @param epochDateTimeNullable the epochDateTimeNullable value to set.
* @return the Optional object itself.
*/
@Generated
public Optional setEpochDateTimeNullable(OffsetDateTime epochDateTimeNullable) {
if (epochDateTimeNullable == null) {
this.epochDateTimeNullable = null;
} else {
this.epochDateTimeNullable = epochDateTimeNullable.toEpochSecond();
}
return this;
}

/**
* {@inheritDoc}
*/
Expand All @@ -479,6 +537,7 @@ public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
jsonWriter.writeBooleanField("booleanRequiredNullable", this.booleanRequiredNullable);
jsonWriter.writeStringField("stringRequired", this.stringRequired);
jsonWriter.writeStringField("stringRequiredNullable", this.stringRequiredNullable);
jsonWriter.writeNumberField("epochDateTimeRequiredNullable", this.epochDateTimeRequiredNullable);
jsonWriter.writeBooleanField("boolean", this.booleanProperty);
jsonWriter.writeBooleanField("booleanNullable", this.booleanNullable);
jsonWriter.writeStringField("string", this.string);
Expand All @@ -493,6 +552,7 @@ public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
this.dateTime == null ? null : DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(this.dateTime));
jsonWriter.writeArrayField("stringList", this.stringList, (writer, element) -> writer.writeString(element));
jsonWriter.writeMapField("bytesDict", this.bytesDict, (writer, element) -> writer.writeBinary(element));
jsonWriter.writeNumberField("epochDateTimeNullable", this.epochDateTimeNullable);
return jsonWriter.writeEndObject();
}

Expand All @@ -511,6 +571,7 @@ public static Optional fromJson(JsonReader jsonReader) throws IOException {
Boolean booleanRequiredNullable = null;
String stringRequired = null;
String stringRequiredNullable = null;
OffsetDateTime epochDateTimeRequiredNullable = null;
Boolean booleanProperty = null;
Boolean booleanNullable = null;
String string = null;
Expand All @@ -524,6 +585,7 @@ public static Optional fromJson(JsonReader jsonReader) throws IOException {
OffsetDateTime dateTime = null;
List<String> stringList = null;
Map<String, byte[]> bytesDict = null;
Long epochDateTimeNullable = null;
while (reader.nextToken() != JsonToken.END_OBJECT) {
String fieldName = reader.getFieldName();
reader.nextToken();
Expand All @@ -536,6 +598,12 @@ public static Optional fromJson(JsonReader jsonReader) throws IOException {
stringRequired = reader.getString();
} else if ("stringRequiredNullable".equals(fieldName)) {
stringRequiredNullable = reader.getString();
} else if ("epochDateTimeRequiredNullable".equals(fieldName)) {
Long epochDateTimeRequiredNullableHolder = reader.getNullable(JsonReader::getLong);
if (epochDateTimeRequiredNullableHolder != null) {
epochDateTimeRequiredNullable = OffsetDateTime
.ofInstant(Instant.ofEpochSecond(epochDateTimeRequiredNullableHolder), ZoneOffset.UTC);
}
} else if ("boolean".equals(fieldName)) {
booleanProperty = reader.getNullable(JsonReader::getBoolean);
} else if ("booleanNullable".equals(fieldName)) {
Expand All @@ -562,12 +630,14 @@ public static Optional fromJson(JsonReader jsonReader) throws IOException {
stringList = reader.readArray(reader1 -> reader1.getString());
} else if ("bytesDict".equals(fieldName)) {
bytesDict = reader.readMap(reader1 -> reader1.getBinary());
} else if ("epochDateTimeNullable".equals(fieldName)) {
epochDateTimeNullable = reader.getNullable(JsonReader::getLong);
} else {
reader.skipChildren();
}
}
Optional deserializedOptional
= new Optional(booleanRequired, booleanRequiredNullable, stringRequired, stringRequiredNullable);
Optional deserializedOptional = new Optional(booleanRequired, booleanRequiredNullable, stringRequired,
stringRequiredNullable, epochDateTimeRequiredNullable);
deserializedOptional.booleanProperty = booleanProperty;
deserializedOptional.booleanNullable = booleanNullable;
deserializedOptional.string = string;
Expand All @@ -581,6 +651,7 @@ public static Optional fromJson(JsonReader jsonReader) throws IOException {
deserializedOptional.dateTime = dateTime;
deserializedOptional.stringList = stringList;
deserializedOptional.bytesDict = bytesDict;
deserializedOptional.epochDateTimeNullable = epochDateTimeNullable;

return deserializedOptional;
});
Expand Down
5 changes: 5 additions & 0 deletions typespec-tests/tsp/optional.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ using TypeSpec.Http;
})
namespace Cadl.Optional;

@encode(DateTimeKnownEncoding.unixTimestamp, int64)
scalar epochDateTime extends utcDateTime;

model Optional {
boolean?: boolean = true;
booleanNullable?: boolean | null;
Expand All @@ -25,6 +28,8 @@ model Optional {
dateTime?: utcDateTime;
stringList?: string[];
bytesDict?: Record<bytes>;
epochDateTimeRequiredNullable: epochDateTime | null;
epochDateTimeNullable?: epochDateTime;
}

model Immutable {
Expand Down

0 comments on commit bc9493b

Please sign in to comment.