Various XML fixes
This commit is contained in:
parent
d237c438c7
commit
72a28b5f81
16
imm/Imm.cs
16
imm/Imm.cs
@ -1,15 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using lib;
|
||||
|
||||
namespace imm;
|
||||
|
||||
|
||||
/*
|
||||
T O D O :
|
||||
T O D O :
|
||||
T O D O :
|
||||
x) Add unit tests for all this. This will definitely benefit from them
|
||||
*/
|
||||
|
||||
|
||||
static public class Util
|
||||
{
|
||||
//This can handle both Timed and Recorded
|
||||
@ -38,7 +48,7 @@ static public class Util
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//[lib.Ser( Types = lib.Types.None )]
|
||||
public record class Versioned<T>
|
||||
where T : Versioned<T>
|
||||
{
|
||||
@ -61,6 +71,7 @@ public record class Versioned<T>
|
||||
|
||||
public MetaData Meta => MetaStorage;
|
||||
|
||||
[lib.Dont]
|
||||
public ChangeDelegate OnChange = (x, y) => {};
|
||||
|
||||
public T Process( Func<T, T> fn, string reason = "" )
|
||||
@ -78,11 +89,14 @@ public record class Versioned<T>
|
||||
}
|
||||
}
|
||||
|
||||
//[lib.Ser( Types = lib.Types.None )]
|
||||
public record class Recorded<T> : Versioned<T>
|
||||
where T : Recorded<T>
|
||||
{
|
||||
|
||||
new public record class MetaData : Versioned<T>.MetaData
|
||||
{
|
||||
[lib.Dont]
|
||||
public T? ZZOld { get; internal set; }
|
||||
public T? Old => ZZOld;
|
||||
public string Expression { get; internal set; } = "";
|
||||
|
||||
@ -12,6 +12,7 @@ using System.Diagnostics;
|
||||
|
||||
using System.Runtime.InteropServices;
|
||||
using static System.Net.WebRequestMethods;
|
||||
using System.Linq;
|
||||
|
||||
|
||||
/*
|
||||
@ -47,8 +48,10 @@ namespace lib
|
||||
Fields = 0b_0001,
|
||||
Props = 0b_0010,
|
||||
Implied = 0b_0100,
|
||||
Explicit= 0b_1000,
|
||||
|
||||
None = 0b_000,
|
||||
|
||||
None = 0b_0000,
|
||||
Default = Fields,
|
||||
All = Fields | Props,
|
||||
}
|
||||
@ -58,6 +61,14 @@ namespace lib
|
||||
public Types Types { get; set; } = Types.Default;
|
||||
}
|
||||
|
||||
public class Do : Attribute
|
||||
{
|
||||
}
|
||||
|
||||
public class Dont : Attribute
|
||||
{
|
||||
}
|
||||
|
||||
public class ChildAttributes : Attribute
|
||||
{
|
||||
public string[] Values { get; private set; }
|
||||
@ -82,10 +93,10 @@ namespace lib
|
||||
{
|
||||
Invalid,
|
||||
|
||||
//
|
||||
// Breaks on circular datastructures since it will go on forever
|
||||
Tree,
|
||||
|
||||
//
|
||||
// Works for everything.
|
||||
Graph,
|
||||
|
||||
}
|
||||
@ -94,7 +105,7 @@ namespace lib
|
||||
|
||||
public class XmlFormatter2Cfg: Config
|
||||
{
|
||||
public Datastructure datastructure = Datastructure.Graph;
|
||||
public Datastructure datastructure = Datastructure.Tree;
|
||||
|
||||
public int Version = 2;
|
||||
|
||||
@ -132,12 +143,12 @@ namespace lib
|
||||
|
||||
public XmlFormatter2()
|
||||
{
|
||||
Context = new StreamingContext( StreamingContextStates.All );
|
||||
//Context = new StreamingContext( StreamingContextStates.All );
|
||||
}
|
||||
|
||||
public XmlFormatter2( XmlFormatter2Cfg cfg )
|
||||
{
|
||||
Context = new StreamingContext( StreamingContextStates.All );
|
||||
//Context = new StreamingContext( StreamingContextStates.All );
|
||||
|
||||
m_cfg = cfg;
|
||||
|
||||
@ -406,8 +417,10 @@ namespace lib
|
||||
mm_consType[1] = typeof( StreamingContext );
|
||||
ConstructorInfo serCons = finalType.GetConstructor( BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, mm_consType, null );
|
||||
|
||||
var context = new StreamingContext( StreamingContextStates.File, obj );
|
||||
|
||||
mm_args[0] = serInfo;
|
||||
mm_args[1] = Context;
|
||||
mm_args[1] = context;
|
||||
serCons.Invoke( obj, mm_args );
|
||||
|
||||
if( objUnOnDeser != null )
|
||||
@ -433,36 +446,50 @@ namespace lib
|
||||
*/
|
||||
}
|
||||
else
|
||||
{
|
||||
HydrateObjectOfNarrowType(elem, mi, finalType, obj);
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
private void HydrateObjectOfNarrowType(XmlElement elem, MemberInfo mi, Type narrowType, object obj)
|
||||
{
|
||||
XmlNodeList allChildren = elem.ChildNodes;
|
||||
|
||||
bool filterFields, filterProps, doImpls, doFields, doProps;
|
||||
HashSet<string> whitelistFields, whitelistProps;
|
||||
GetFilters( m_cfg.TypesDefault, mi, finalType, out filterFields, out filterProps, out doImpls, out doFields, out doProps, out whitelistFields, out whitelistProps );
|
||||
|
||||
/*
|
||||
List<MemberInfo> members = new();
|
||||
GetFilters(m_cfg.TypesDefault, mi, narrowType, out filterFields, out filterProps, out doImpls, out doFields, out doProps, out whitelistFields, out whitelistProps);
|
||||
|
||||
if (doFields || doImpls)
|
||||
{
|
||||
members.AddRange( refl.GetAllFields( finalType ) );
|
||||
}
|
||||
|
||||
if( doProps || doImpls )
|
||||
{
|
||||
members.AddRange( refl.GetAllProperties( finalType ) );
|
||||
}
|
||||
*/
|
||||
|
||||
if( doFields || doImpls )
|
||||
{
|
||||
var fields = refl.GetAllFields( finalType );
|
||||
var fields = refl.GetAllFields(narrowType);
|
||||
|
||||
foreach (FieldInfo childFi in fields)
|
||||
{
|
||||
|
||||
String name = childFi.Name;
|
||||
|
||||
var dontAtt = childFi.GetCustomAttributes<lib.Dont>();
|
||||
|
||||
if( name.StartsWith( "<" ) && name.EndsWith( "BackingField" ) )
|
||||
{
|
||||
var gtIndex = name.IndexOf( '>' );
|
||||
|
||||
var propName = name.Substring( 1, gtIndex - 1 );
|
||||
|
||||
var propInfo = narrowType.GetProperty( propName );
|
||||
|
||||
dontAtt = propInfo.GetCustomAttributes<lib.Dont>();
|
||||
}
|
||||
|
||||
if( dontAtt.Any() )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//if( name.EndsWith( ) )
|
||||
|
||||
//This is to convert c# names that would be bad as XML tags
|
||||
name = refl.TypeToIdentifier(name);
|
||||
|
||||
@ -484,11 +511,16 @@ namespace lib
|
||||
|
||||
if (doProps || doImpls)
|
||||
{
|
||||
var props = refl.GetAllProperties( finalType );
|
||||
var props = refl.GetAllProperties(narrowType);
|
||||
|
||||
foreach (var childPi in props)
|
||||
{
|
||||
String name = childPi.Name;
|
||||
var dontAtt = childPi.GetCustomAttributes<lib.Dont>();
|
||||
if( dontAtt.Any() )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
name = refl.TypeToIdentifier(name);
|
||||
|
||||
@ -519,18 +551,13 @@ namespace lib
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
private static bool FilterField( bool filterFields, bool doImpls, HashSet<string> whitelistFields, MemberInfo mi, string name )
|
||||
{
|
||||
if( doImpls )
|
||||
{
|
||||
if( mi.GetCustomAttribute<ChildAttributes>() == null ) return true;
|
||||
if( mi.GetCustomAttribute<ChildAttributes>( true ) == null ) return true;
|
||||
}
|
||||
|
||||
if( filterFields && !whitelistFields.Contains( name ) ) return true;
|
||||
@ -880,9 +907,16 @@ namespace lib
|
||||
|
||||
if (root is ISerializable ser)
|
||||
{
|
||||
if ((root is Delegate))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var serInfo = new SerializationInfo(type, new FormatterConverter());
|
||||
|
||||
ser.GetObjectData( serInfo, Context );
|
||||
var context = new StreamingContext(StreamingContextStates.File, root);
|
||||
|
||||
ser.GetObjectData(serInfo, context);
|
||||
|
||||
foreach (var serMember in serInfo)
|
||||
{
|
||||
@ -921,18 +955,42 @@ namespace lib
|
||||
}
|
||||
|
||||
//*/
|
||||
SerializeObjectOfNarrowType(writer, mi, root, depth, type);
|
||||
}
|
||||
}
|
||||
|
||||
private void SerializeObjectOfNarrowType(XmlWriter writer, MemberInfo mi, object root, int depth, Type narrowType)
|
||||
{
|
||||
bool filterFields, filterProps, doImpls, doFields, doProps;
|
||||
HashSet<string> whitelistFields, whitelistProps;
|
||||
GetFilters( m_cfg.TypesDefault, mi, type, out filterFields, out filterProps, out doImpls, out doFields, out doProps, out whitelistFields, out whitelistProps );
|
||||
GetFilters(m_cfg.TypesDefault, mi, narrowType, out filterFields, out filterProps, out doImpls, out doFields, out doProps, out whitelistFields, out whitelistProps);
|
||||
|
||||
if (doFields || doImpls)
|
||||
{
|
||||
var fields = refl.GetAllFields( type );
|
||||
var fields = refl.GetAllFields(narrowType);
|
||||
|
||||
foreach (var childFi in fields)
|
||||
{
|
||||
String name = childFi.Name;
|
||||
var dontAtt = childFi.GetCustomAttributes<lib.Dont>();
|
||||
|
||||
|
||||
if( name.StartsWith( "<" ) && name.EndsWith( "BackingField" ) )
|
||||
{
|
||||
var gtIndex = name.IndexOf( '>' );
|
||||
|
||||
var propName = name.Substring( 1, gtIndex - 1 );
|
||||
|
||||
var propInfo = narrowType.GetProperty( propName );
|
||||
|
||||
dontAtt = propInfo.GetCustomAttributes<lib.Dont>();
|
||||
}
|
||||
|
||||
|
||||
if( dontAtt.Any() )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (FilterField(filterFields, doImpls, whitelistFields, childFi as MemberInfo, name)) continue;
|
||||
|
||||
@ -953,12 +1011,18 @@ namespace lib
|
||||
|
||||
if (doProps || doImpls)
|
||||
{
|
||||
var props = refl.GetAllProperties( type );
|
||||
var props = refl.GetAllProperties(narrowType);
|
||||
|
||||
foreach (var childPi in props)
|
||||
{
|
||||
String name = childPi.Name;
|
||||
|
||||
var dontAtt = childPi.GetCustomAttributes<lib.Dont>();
|
||||
if( dontAtt.Any() )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (FilterField(filterProps, doImpls, whitelistProps, childPi as MemberInfo, name)) continue;
|
||||
|
||||
object[] objs = childPi.GetCustomAttributes(typeof(NonSerializedAttribute), true);
|
||||
@ -975,18 +1039,16 @@ namespace lib
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void GetFilters( Types TypesDefault, MemberInfo mi, Type type, out bool filterFields, out bool filterProps, out bool doImpls, out bool doFields, out bool doProps, out HashSet<string> whitelistFields, out HashSet<string> whitelistProps )
|
||||
{
|
||||
var custWLFields = mi?.GetCustomAttribute<ChildFieldsAttribute>();
|
||||
var custWLProps = mi?.GetCustomAttribute<ChildPropsAttribute>();
|
||||
var custWLFields = mi?.GetCustomAttribute<ChildFieldsAttribute>( true );
|
||||
var custWLProps = mi?.GetCustomAttribute<ChildPropsAttribute>( true );
|
||||
|
||||
filterFields = custWLFields != null;
|
||||
filterProps = custWLProps != null;
|
||||
|
||||
var typesTodo = type.GetCustomAttribute<Ser>()?.Types ?? TypesDefault;
|
||||
var typesTodo = type.GetCustomAttribute<Ser>( true )?.Types ?? TypesDefault;
|
||||
|
||||
doImpls = typesTodo.HasFlag( Types.Implied );
|
||||
doFields = filterFields || typesTodo.HasFlag( Types.Fields );
|
||||
|
||||
Loading…
Reference in New Issue
Block a user