Struct EmptyStringAsMissingConverter<T, TConverter>
- Namespace
- DynaSharp.Serialization
- Assembly
- DynaSharp.dll
Reads and writes what TConverter does, and counts a stored empty string, {"S":""}, as if the attribute were not in the item, on top of what
TConverter counts. It is for a table where other code wrote "" for no value, into an attribute whose converter cannot read it:
[DynamoDbConverter(typeof(EmptyStringAsMissingConverter<DateTimeOffset, DateTimeOffsetConverter>))] public DateTimeOffset? ShippedAt { get; init; }
reads {"S":""} as null, and {"S":"2026-10-31T12:00:00.0000000Z"} as that instant.
public readonly struct EmptyStringAsMissingConverter<T, TConverter> : IDynamoDbConverter<T> where TConverter : IDynamoDbConverter<T>
Type Parameters
TThe type of the property.
TConverterThe converter that reads and writes the values, a built-in or one of your own.
- Implements
- Inherited Members
Remarks
As if the attribute were not in the item means what CountsAsMissing(in DynamoDbReader, AttributeType) says: the property keeps its initial value, a nullable one too, and a
required one throws DynamoDbMappingException. Only the property is asked: an element of a list, a map or a set, a plain key and Item read the
empty string with TConverter, which may throw. A write never stores "" for a value unless TConverter does.
It passes IsOmitted(T) on, and not TryReadMissing(out T): an answer there would tell DYNA209 that a
Select of the property cannot throw, and for a value that is not nullable it throws on an empty string. A nullable property gets null from its wrapper of null, as without this.
Naming a generic converter in an attribute is no CA1000, which is about declaring one.
Methods
CountsAsMissing(in DynamoDbReader, AttributeType)
public static bool CountsAsMissing(in DynamoDbReader reader, AttributeType type)
Parameters
readerDynamoDbReadertypeAttributeType
Returns
Explicit Interface Implementations
IsOmitted(T)
Finds out whether a write treats a value like null, which leaves the attribute out. It is for a value that DynamoDB would refuse, or that should not be stored, such as a list stored as a set, which cannot be
empty: public static bool IsOmitted(List<string> value) => value.Count == 0;. OmitEmptyAttribute asks the same question through DynaSharp.Serialization.OmitEmptyConverter<TConverter> and
DynaSharp.Serialization.OmitEmptyCollectionConverter<T, TElement, TConverter>.
static bool IsOmitted(T value)
Parameters
valueTThe value, never null on a write.
Returns
Remarks
Every write of the property asks: a Put of the entity, Item.From, a batch, a step of a transaction and a nested document leave the attribute out, Set by handle is a Remove,
SetIfNotNull adds nothing and SetIfNotExists rejects the value with an ArgumentException. A raw path writes what it is given. A nullable property asks with a value that is not null.
The default answers false. The source generator emits the call only for a [DynamoDbConverter] or a [DynamoDbDefaultConverter] that implements this, implicitly or explicitly. It is the property that a converter answers for: a property that feeds a key is always written, because a key cannot do without it, and an element of a list or a map is
always written, because a list has positions.
A read does not ask. The item has no attribute, so a read of the entity keeps the initial value of the property, and throws DynamoDbMappingException if it is required. Give the property an
initial value that means the same as what was left out, and implement TryReadMissing(out T) for Select.
Read(ref DynamoDbReader, AttributeType)
Reads the attribute value the reader is positioned on.
static T Read(ref DynamoDbReader reader, AttributeType type)
Parameters
readerDynamoDbReaderThe reader, positioned on the value after ReadStartAttribute() or TryReadStartAttribute(out AttributeType). The caller reads the closing brace afterwards, so the converter must read the value completely (all elements of a list, map or set) and nothing more.
typeAttributeTypeThe DynamoDB type of the value. Check it with Expect(AttributeType, AttributeType), which throws JsonException for types the converter does not support. Null is passed to the converter too, so it can map it to null where that makes sense.
Returns
- T
The value.
Write(DynamoDbWriter, T)
Writes a value as one complete attribute value, for example with WriteNumber<T>(T).
static void Write(DynamoDbWriter writer, T value)
Parameters
writerDynamoDbWriterThe writer, positioned where a value is expected (after an attribute name, or inside a list). It is a ref struct, so it is never null and needs no check.
valueTThe value.