/// <summary> /// Create a DeserializableMember with an explicit type being serialized, name, setter, parser, whether it is required, and a reset method. /// </summary> public static DeserializableMember Create( TypeInfo forType, string name, Setter setter, Parser parser, MemberRequired required, [NullableExposed("Reset is truly optional here, it's required and validated elsewhere")] Reset?reset ) => CreateInner(forType, name, setter, parser, required, reset, null);
/// <summary> /// Creates a DeserializableMember for the given property, with the given name, parser, whether it is required, and a reset method. /// </summary> public static DeserializableMember ForProperty(PropertyInfo property, string name, Parser parser, MemberRequired required, Reset reset) => CreateInner(property?.DeclaringType?.GetTypeInfo(), name, (Setter?)property?.SetMethod, parser, required, reset, null);
internal static DeserializableMember CreateInner( TypeInfo?beingDeserializedType, string?name, Setter?setter, Parser?parser, MemberRequired isRequired, Reset?reset, TypeInfo?aheadOfTimeGeneratedType ) { if (beingDeserializedType == null) { Throw.ArgumentNullException(nameof(beingDeserializedType)); } if (name == null) { Throw.ArgumentNullException(nameof(name)); } if (setter == null) { Throw.ArgumentNullException(nameof(setter)); } if (parser == null) { Throw.ArgumentNullException(nameof(parser)); } if (name.Length == 0) { Throw.ArgumentException($"{nameof(name)} must be at least 1 character long", nameof(name)); } bool isRequiredBool; switch (isRequired) { case MemberRequired.Yes: isRequiredBool = true; break; case MemberRequired.No: isRequiredBool = false; break; default: Throw.ArgumentException($"Unexpected {nameof(MemberRequired)}: {isRequired}", nameof(isRequired)); return(default); } var valueType = setter.Takes; if (!valueType.IsAssignableFrom(parser.Creates)) { Throw.ArgumentException($"Provided {nameof(Parser)} creates a {parser.Creates}, which cannot be passed to {setter} which expects a {valueType}", nameof(setter)); } if (reset != null && reset.RowType.HasValue) { if (!reset.RowType.Value.IsAssignableFrom(beingDeserializedType)) { Throw.ArgumentException($"{nameof(reset)} must be callable on {beingDeserializedType}", nameof(reset)); } } if (setter.Mode == BackingMode.ConstructorParameter && !isRequiredBool) { Throw.InvalidOperationException($"{nameof(Setter)} that is backed by a constructor parameter can only be used with {nameof(MemberRequired)}.{nameof(MemberRequired.Yes)}; {nameof(setter)} was {setter}"); } return(new DeserializableMember(name, setter, parser, isRequiredBool, reset, aheadOfTimeGeneratedType)); }
private ManualTypeDescriberBuilder WithDeserializeMember(TypeInfo?forType, Setter?setter, string?name, Parser?parser, MemberRequired required, Reset?reset) { if (forType == null) { Throw.ArgumentNullException(nameof(forType)); } if (setter == null) { Throw.ArgumentNullException(nameof(setter)); } if (name == null) { Throw.ArgumentNullException(nameof(name)); } if (parser == null) { Throw.ArgumentNullException(nameof(parser)); } var toAdd = DeserializableMember.Create(forType, name, setter, parser, required, reset); if (!Deserializers.TryGetValue(forType, out var d)) { Deserializers[forType] = d = ImmutableArray.CreateBuilder <DeserializableMember>(); } d.Add(toAdd); return(this); }
/// <summary> /// Creates a DeserializableMember for the given property, with the given name, parser, whether it is required, and a reset method. /// </summary> public static DeserializableMember ForField(FieldInfo field, string name, Parser parser, MemberRequired required, Reset reset) => CreateInner(field?.DeclaringType?.GetTypeInfo(), name, (Setter?)field, parser, required, reset, null);
/// <summary> /// Add a property to deserialize with the given name, parser, and whether the column is required - for the type which declares the property. /// </summary> public ManualTypeDescriberBuilder WithDeserializableProperty(PropertyInfo property, string name, Parser parser, MemberRequired required) => WithDeserializeMember(property?.DeclaringType?.GetTypeInfo(), (Setter?)property?.SetMethod, name, parser, required, null);
/// <summary> /// Add a property to deserialize with the given name, parser, whether the column is required, and a reset method - for the type which declares the property. /// </summary> public ManualTypeDescriberBuilder WithDeserializableProperty(PropertyInfo property, string name, Parser parser, MemberRequired required, Reset reset) { Utils.CheckArgumentNull(reset, nameof(reset)); return(WithDeserializeMember(property?.DeclaringType?.GetTypeInfo(), (Setter?)property?.SetMethod, name, parser, required, reset)); }
/// <summary> /// Add a field to deserialize with the given name, parser, whether the column is required, and a reset method - for the type which declares the field. /// </summary> public ManualTypeDescriberBuilder WithDeserializableField(FieldInfo field, string name, Parser parser, MemberRequired required, Reset reset) { Utils.CheckArgumentNull(reset, nameof(reset)); return(WithDeserializeMember(field?.DeclaringType?.GetTypeInfo(), (Setter?)field, name, parser, required, reset)); }
/// <summary> /// Add a field to deserialize with the given name, parser, and whether the column is required - for the type which declares the field. /// </summary> public ManualTypeDescriberBuilder WithDeserializableField(FieldInfo field, string name, Parser parser, MemberRequired required) => WithDeserializeMember(field?.DeclaringType?.GetTypeInfo(), (Setter?)field, name, parser, required, null);
/// <summary> /// Add a field to deserialize for the given type with the given name, parser, and whether the column is required. /// </summary> public ManualTypeDescriberBuilder WithDeserializableField(TypeInfo forType, FieldInfo field, string name, Parser parser, MemberRequired required) => WithDeserializeMember(forType, (Setter?)field, name, parser, required, null);
/// <summary> /// Add a setter for the given type, with the given name, using the given setter, parser, whether the column is required, and a reset method. /// </summary> public ManualTypeDescriberBuilder WithExplicitSetter(TypeInfo forType, string name, Setter setter, Parser parser, MemberRequired required, Reset reset) { Utils.CheckArgumentNull(reset, nameof(reset)); return(WithDeserializeMember(forType, setter, name, parser, required, reset)); }
/// <summary> /// Add a setter for the given type, with the given name, using the given setter, parser, and whether the column is required. /// </summary> public ManualTypeDescriberBuilder WithExplicitSetter(TypeInfo forType, string name, Setter setter, Parser parser, MemberRequired required) => WithDeserializeMember(forType, setter, name, parser, required, null);