Skip to content

Commit 45c2afa

Browse files
committed
Included IsList in IsEnumerable, moved GetSet into GetAccessors and tidied up ModelBuilder (encapsulated common groups of property setters on PropertyModel, using IXmlSchemaNode).
1 parent 53aed3f commit 45c2afa

File tree

4 files changed

+378
-404
lines changed

4 files changed

+378
-404
lines changed
Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.IO;
43
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
74
using System.Xml.Schema;
85

96
namespace XmlSchemaClassGenerator
@@ -12,31 +9,18 @@ public static class Extensions
129
{
1310
public static XmlSchema GetSchema(this XmlSchemaObject xmlSchemaObject)
1411
{
15-
while (xmlSchemaObject != null && !(xmlSchemaObject is XmlSchema))
12+
while (xmlSchemaObject is not null and not XmlSchema)
1613
xmlSchemaObject = xmlSchemaObject.Parent;
1714
return (XmlSchema)xmlSchemaObject;
1815
}
1916

20-
public static PropertyValueTypeCode GetPropertyValueTypeCode(this TypeModel model)
17+
public static PropertyValueTypeCode GetPropertyValueTypeCode(this TypeModel model) => model switch
2118
{
22-
if (model is not SimpleModel simpleType)
23-
{
24-
if (!(model is EnumModel))
25-
{
26-
return PropertyValueTypeCode.Other;
27-
}
28-
return PropertyValueTypeCode.ValueType;
29-
}
30-
if (simpleType.ValueType.IsArray)
31-
{
32-
return PropertyValueTypeCode.Array;
33-
}
34-
if (simpleType.ValueType.IsValueType)
35-
{
36-
return PropertyValueTypeCode.ValueType;
37-
}
38-
return PropertyValueTypeCode.Other;
39-
}
19+
SimpleModel { ValueType.IsArray: true } => PropertyValueTypeCode.Array,
20+
SimpleModel { ValueType.IsValueType: true } => PropertyValueTypeCode.ValueType,
21+
SimpleModel or not EnumModel => PropertyValueTypeCode.Other,
22+
_ => PropertyValueTypeCode.ValueType
23+
};
4024

4125
public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> propertySelector)
4226
{
@@ -45,17 +29,9 @@ public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TS
4529

4630
public static string QuoteIfNeeded(this string text)
4731
{
48-
if (string.IsNullOrEmpty(text))
49-
{
50-
return text;
51-
}
52-
53-
if (text.Contains(" "))
54-
{
55-
return "\"" + text + "\"";
56-
}
57-
58-
return text;
32+
return string.IsNullOrEmpty(text) ? text
33+
: text.Contains(" ") ? "\"" + text + "\""
34+
: text;
5935
}
6036
}
6137
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System.Xml;
2+
using System.Xml.Schema;
3+
4+
namespace XmlSchemaClassGenerator
5+
{
6+
public interface IXmlSchemaNode
7+
{
8+
string Name { get; }
9+
string DefaultValue { get; }
10+
string FixedValue { get; }
11+
XmlSchemaForm Form { get; }
12+
XmlQualifiedName QualifiedName { get; }
13+
XmlQualifiedName RefName { get; }
14+
XmlSchemaType SchemaType { get; }
15+
XmlSchemaType NodeSchemaType { get; }
16+
XmlQualifiedName SchemaTypeName { get; }
17+
18+
XmlSchemaAnnotated Base { get; }
19+
XmlSchemaForm FormDefault { get; }
20+
}
21+
22+
public sealed class XmlSchemaAttributeEx : IXmlSchemaNode
23+
{
24+
private XmlSchemaAttributeEx(XmlSchemaAttribute xs) => Real = xs;
25+
26+
public XmlSchemaAttribute Real { get; }
27+
28+
public string Name => Real.Name;
29+
public string DefaultValue => Real.DefaultValue;
30+
public string FixedValue => Real.FixedValue;
31+
public XmlSchemaForm Form => Real.Form;
32+
public XmlQualifiedName QualifiedName => Real.QualifiedName;
33+
public XmlQualifiedName RefName => Real.RefName;
34+
public XmlQualifiedName SchemaTypeName => Real.SchemaTypeName;
35+
public XmlSchemaSimpleType SchemaType => Real.SchemaType;
36+
public XmlSchemaSimpleType AttributeSchemaType => Real.AttributeSchemaType;
37+
38+
public XmlSchemaAnnotated Base => Real;
39+
40+
public XmlSchemaForm FormDefault => Base.GetSchema().AttributeFormDefault;
41+
42+
XmlSchemaType IXmlSchemaNode.SchemaType => SchemaType;
43+
44+
XmlSchemaType IXmlSchemaNode.NodeSchemaType => AttributeSchemaType;
45+
46+
public XmlSchemaUse Use => Real.Use;
47+
48+
public static implicit operator XmlSchemaAttributeEx(XmlSchemaAttribute xs) => new(xs);
49+
public static implicit operator XmlSchemaAttribute(XmlSchemaAttributeEx ex) => ex.Real;
50+
}
51+
52+
public sealed class XmlSchemaElementEx : IXmlSchemaNode
53+
{
54+
private XmlSchemaElementEx(XmlSchemaElement xs) => Real = xs;
55+
56+
public XmlSchemaElement Real { get; }
57+
58+
public string Name => Real.Name;
59+
public string DefaultValue => Real.DefaultValue;
60+
public string FixedValue => Real.FixedValue;
61+
public XmlSchemaForm Form => Real.Form;
62+
public XmlQualifiedName QualifiedName => Real.QualifiedName;
63+
public XmlQualifiedName RefName => Real.RefName;
64+
public XmlQualifiedName SchemaTypeName => Real.SchemaTypeName;
65+
public XmlSchemaType SchemaType => Real.SchemaType;
66+
public XmlSchemaType ElementSchemaType => Real.ElementSchemaType;
67+
68+
public XmlSchemaAnnotated Base => Real;
69+
70+
public XmlSchemaForm FormDefault => Base.GetSchema().ElementFormDefault;
71+
72+
XmlSchemaType IXmlSchemaNode.SchemaType => SchemaType;
73+
74+
XmlSchemaType IXmlSchemaNode.NodeSchemaType => ElementSchemaType;
75+
76+
public bool IsNillable => Real.IsNillable;
77+
78+
public static implicit operator XmlSchemaElementEx(XmlSchemaElement xs) => new(xs);
79+
public static implicit operator XmlSchemaElement(XmlSchemaElementEx ex) => ex.Real;
80+
}
81+
}

0 commit comments

Comments
 (0)