private Func <IReadableStream, ColumnVal> GetColumnReader(VariableDef c) { String TypeName; Boolean IsOptional; if (c.Type.OnTypeRef) { TypeName = c.Type.TypeRef.Value; IsOptional = false; } else if (c.Type.OnOptional) { TypeName = c.Type.Optional.Value; IsOptional = true; } else if (c.Type.OnList) { var ElementTypeName = c.Type.List.Value; if (!ElementTypeName.Equals("Byte", StringComparison.OrdinalIgnoreCase)) { throw new InvalidOperationException("InvalidColumnListType: List<{0}>".Formats(ElementTypeName)); } TypeName = "Binary"; IsOptional = false; } else { throw new InvalidOperationException(); } if (EnumUnderlyingTypes.ContainsKey(TypeName)) { TypeName = EnumUnderlyingTypes[TypeName]; } Func <IReadableStream, ColumnVal> Reader; if (!IsOptional) { if (TypeName.Equals("Boolean", StringComparison.OrdinalIgnoreCase)) { Reader = s => ColumnVal.CreatePrimitive(PrimitiveVal.CreateBooleanValue(s.ReadByte() != 0)); } else if (TypeName.Equals("String", StringComparison.OrdinalIgnoreCase)) { Reader = s => { var NumBytes = s.ReadInt32(); var Bytes = s.Read(NumBytes); var t = TextEncoding.UTF16.GetString(Bytes); return(ColumnVal.CreatePrimitive(PrimitiveVal.CreateStringValue(t))); }; } else if (TypeName.Equals("Int", StringComparison.OrdinalIgnoreCase)) { Reader = s => ColumnVal.CreatePrimitive(PrimitiveVal.CreateIntValue(s.ReadInt32())); } else if (TypeName.Equals("Int64", StringComparison.OrdinalIgnoreCase)) { Reader = s => ColumnVal.CreatePrimitive(PrimitiveVal.CreateInt64Value(s.ReadInt64())); } else if (TypeName.Equals("Real", StringComparison.OrdinalIgnoreCase)) { Reader = s => ColumnVal.CreatePrimitive(PrimitiveVal.CreateRealValue(s.ReadFloat64())); } else if (TypeName.Equals("Binary", StringComparison.OrdinalIgnoreCase)) { Reader = s => { var NumBytes = s.ReadInt32(); var Bytes = s.Read(NumBytes); return(ColumnVal.CreatePrimitive(PrimitiveVal.CreateBinaryValue(Bytes.ToList()))); }; } else { throw new InvalidOperationException("InvalidType: {0}".Formats(TypeName)); } } else { if (TypeName.Equals("Boolean", StringComparison.OrdinalIgnoreCase)) { Reader = s => { var OnSome = s.ReadInt32() != 0; if (OnSome) { return(ColumnVal.CreateOptional(PrimitiveVal.CreateBooleanValue(s.ReadByte() != 0))); } else { return(ColumnVal.CreateOptional(Optional <PrimitiveVal> .Empty)); } }; } else if (TypeName.Equals("String", StringComparison.OrdinalIgnoreCase)) { Reader = s => { var OnSome = s.ReadInt32() != 0; if (OnSome) { var NumBytes = s.ReadInt32(); var Bytes = s.Read(NumBytes); var t = TextEncoding.UTF16.GetString(Bytes); return(ColumnVal.CreateOptional(PrimitiveVal.CreateStringValue(t))); } else { return(ColumnVal.CreateOptional(Optional <PrimitiveVal> .Empty)); } }; } else if (TypeName.Equals("Int", StringComparison.OrdinalIgnoreCase)) { Reader = s => { var OnSome = s.ReadInt32() != 0; if (OnSome) { return(ColumnVal.CreateOptional(PrimitiveVal.CreateIntValue(s.ReadInt32()))); } else { return(ColumnVal.CreateOptional(Optional <PrimitiveVal> .Empty)); } }; } else if (TypeName.Equals("Int64", StringComparison.OrdinalIgnoreCase)) { Reader = s => { var OnSome = s.ReadInt32() != 0; if (OnSome) { return(ColumnVal.CreateOptional(PrimitiveVal.CreateInt64Value(s.ReadInt64()))); } else { return(ColumnVal.CreateOptional(Optional <PrimitiveVal> .Empty)); } }; } else if (TypeName.Equals("Real", StringComparison.OrdinalIgnoreCase)) { Reader = s => { var OnSome = s.ReadInt32() != 0; if (OnSome) { return(ColumnVal.CreateOptional(PrimitiveVal.CreateRealValue(s.ReadFloat64()))); } else { return(ColumnVal.CreateOptional(Optional <PrimitiveVal> .Empty)); } }; } else if (TypeName.Equals("Binary", StringComparison.OrdinalIgnoreCase)) { Reader = s => { var OnSome = s.ReadInt32() != 0; if (OnSome) { var NumBytes = s.ReadInt32(); var Bytes = s.Read(NumBytes); return(ColumnVal.CreateOptional(PrimitiveVal.CreateBinaryValue(Bytes.ToList()))); } else { return(ColumnVal.CreateOptional(Optional <PrimitiveVal> .Empty)); } }; } else { throw new InvalidOperationException("InvalidType: {0}".Formats(TypeName)); } } return(Reader); }
private Func <Node, ColumnVal> GetColumnReader(EntityDef e, VariableDef c) { String TypeName; Boolean IsOptional; if (c.Type.OnTypeRef) { TypeName = c.Type.TypeRef.Value; IsOptional = false; } else if (c.Type.OnOptional) { TypeName = c.Type.Optional.Value; IsOptional = true; } else if (c.Type.OnList) { var ElementTypeName = c.Type.List.Value; if (!ElementTypeName.Equals("Byte", StringComparison.OrdinalIgnoreCase)) { throw new InvalidOperationException("InvalidColumnListType: List<{0}>".Formats(ElementTypeName)); } TypeName = "Binary"; IsOptional = false; } else { throw new InvalidOperationException(); } Dictionary <String, Int64> EnumParser = null; if (EnumUnderlyingTypes.ContainsKey(TypeName)) { EnumParser = EnumParsers[TypeName]; TypeName = EnumUnderlyingTypes[TypeName]; } Func <Node, ColumnVal> Reader; if (!IsOptional) { if (TypeName.Equals("Boolean", StringComparison.OrdinalIgnoreCase)) { Reader = TreeRow => { var cvs = TreeRow.Stem.Children.Where(col => col.OnStem && col.Stem.Name.Equals(c.Name, StringComparison.OrdinalIgnoreCase)).ToList(); if (cvs.Count != 1) { throw new InvalidOperationException(String.Format("InvalidData: {0}.{1}", e.Name, c.Name)); } var cv = cvs.Single().Stem.Children.Single().Leaf; try { var v = Boolean.Parse(cv); return(ColumnVal.CreatePrimitive(PrimitiveVal.CreateBooleanValue(v))); } catch (FormatException) { throw new InvalidOperationException(String.Format("InvalidData: {0}.{1} '{2}'", e.Name, c.Name, cv)); } }; } else if (TypeName.Equals("String", StringComparison.OrdinalIgnoreCase)) { Reader = TreeRow => { var cvs = TreeRow.Stem.Children.Where(col => col.OnStem && col.Stem.Name.Equals(c.Name, StringComparison.OrdinalIgnoreCase)).ToList(); if (cvs.Count != 1) { throw new InvalidOperationException(String.Format("InvalidData: {0}.{1}", e.Name, c.Name)); } var cv = cvs.Single().Stem.Children.Single().Leaf; var v = cv; return(ColumnVal.CreatePrimitive(PrimitiveVal.CreateStringValue(v))); }; } else if (TypeName.Equals("Int", StringComparison.OrdinalIgnoreCase)) { if (EnumParser != null) { Reader = TreeRow => { var cvs = TreeRow.Stem.Children.Where(col => col.OnStem && col.Stem.Name.Equals(c.Name, StringComparison.OrdinalIgnoreCase)).ToList(); if (cvs.Count != 1) { throw new InvalidOperationException(String.Format("InvalidData: {0}.{1}", e.Name, c.Name)); } var cv = cvs.Single().Stem.Children.Single().Leaf; if (EnumParser.ContainsKey(cv)) { var v = (int)(EnumParser[cv]); return(ColumnVal.CreatePrimitive(PrimitiveVal.CreateIntValue(v))); } else { try { var v = NumericStrings.InvariantParseInt32(cv); return(ColumnVal.CreatePrimitive(PrimitiveVal.CreateIntValue(v))); } catch (FormatException) { throw new InvalidOperationException(String.Format("InvalidData: {0}.{1} '{2}'", e.Name, c.Name, cv)); } } }; } else { Reader = TreeRow => { var cvs = TreeRow.Stem.Children.Where(col => col.OnStem && col.Stem.Name.Equals(c.Name, StringComparison.OrdinalIgnoreCase)).ToList(); if (cvs.Count != 1) { throw new InvalidOperationException(String.Format("InvalidData: {0}.{1}", e.Name, c.Name)); } var cv = cvs.Single().Stem.Children.Single().Leaf; try { var v = NumericStrings.InvariantParseInt32(cv); return(ColumnVal.CreatePrimitive(PrimitiveVal.CreateIntValue(v))); } catch (FormatException) { throw new InvalidOperationException(String.Format("InvalidData: {0}.{1} '{2}'", e.Name, c.Name, cv)); } }; } } else if (TypeName.Equals("Int64", StringComparison.OrdinalIgnoreCase)) { if (EnumParser != null) { Reader = TreeRow => { var cvs = TreeRow.Stem.Children.Where(col => col.OnStem && col.Stem.Name.Equals(c.Name, StringComparison.OrdinalIgnoreCase)).ToList(); if (cvs.Count != 1) { throw new InvalidOperationException(String.Format("InvalidData: {0}.{1}", e.Name, c.Name)); } var cv = cvs.Single().Stem.Children.Single().Leaf; if (EnumParser.ContainsKey(cv)) { var v = (Int64)(EnumParser[cv]); return(ColumnVal.CreatePrimitive(PrimitiveVal.CreateInt64Value(v))); } else { try { var v = NumericStrings.InvariantParseInt64(cv); return(ColumnVal.CreatePrimitive(PrimitiveVal.CreateInt64Value(v))); } catch (FormatException) { throw new InvalidOperationException(String.Format("InvalidData: {0}.{1} '{2}'", e.Name, c.Name, cv)); } } }; } else { Reader = TreeRow => { var cvs = TreeRow.Stem.Children.Where(col => col.OnStem && col.Stem.Name.Equals(c.Name, StringComparison.OrdinalIgnoreCase)).ToList(); if (cvs.Count != 1) { throw new InvalidOperationException(String.Format("InvalidData: {0}.{1}", e.Name, c.Name)); } var cv = cvs.Single().Stem.Children.Single().Leaf; try { var v = NumericStrings.InvariantParseInt64(cv); return(ColumnVal.CreatePrimitive(PrimitiveVal.CreateInt64Value(v))); } catch (FormatException) { throw new InvalidOperationException(String.Format("InvalidData: {0}.{1} '{2}'", e.Name, c.Name, cv)); } }; } } else if (TypeName.Equals("Real", StringComparison.OrdinalIgnoreCase)) { Reader = TreeRow => { var cvs = TreeRow.Stem.Children.Where(col => col.OnStem && col.Stem.Name.Equals(c.Name, StringComparison.OrdinalIgnoreCase)).ToList(); if (cvs.Count != 1) { throw new InvalidOperationException(String.Format("InvalidData: {0}.{1}", e.Name, c.Name)); } var cv = cvs.Single().Stem.Children.Single().Leaf; try { var v = NumericStrings.InvariantParseFloat64(cv); return(ColumnVal.CreatePrimitive(PrimitiveVal.CreateRealValue(v))); } catch (FormatException) { throw new InvalidOperationException(String.Format("InvalidData: {0}.{1} '{2}'", e.Name, c.Name, cv)); } }; } else if (TypeName.Equals("Binary", StringComparison.OrdinalIgnoreCase)) { Reader = TreeRow => { var cvs = TreeRow.Stem.Children.Where(col => col.OnStem && col.Stem.Name.Equals(c.Name, StringComparison.OrdinalIgnoreCase)).ToList(); if (cvs.Count != 1) { throw new InvalidOperationException(String.Format("InvalidData: {0}.{1}", e.Name, c.Name)); } var cv = cvs.Single().Stem.Children.Single().Leaf; try { var v = Regex.Split(cv.Trim(" \t\r\n".ToCharArray()), "( |\t|\r|\n)+", RegexOptions.ExplicitCapture).Where(s => s != "").Select(s => Byte.Parse(s, System.Globalization.NumberStyles.HexNumber)).ToList(); return(ColumnVal.CreatePrimitive(PrimitiveVal.CreateBinaryValue(v))); } catch (FormatException) { throw new InvalidOperationException(String.Format("InvalidData: {0}.{1} '{2}'", e.Name, c.Name, cv)); } }; } else { throw new InvalidOperationException("InvalidType: {0}".Formats(TypeName)); } } else { if (TypeName.Equals("Boolean", StringComparison.OrdinalIgnoreCase)) { Reader = TreeRow => { var cvs = TreeRow.Stem.Children.Where(col => col.OnStem && col.Stem.Name.Equals(c.Name, StringComparison.OrdinalIgnoreCase)).ToList(); if (cvs.Count != 1) { throw new InvalidOperationException(String.Format("InvalidData: {0}.{1}", e.Name, c.Name)); } var cv = cvs.Single().Stem.Children.Single().Leaf; if ((cv == null) || (cv == "-")) { return(ColumnVal.CreateOptional(Optional <PrimitiveVal> .Empty)); } try { var v = Boolean.Parse(cv); return(ColumnVal.CreateOptional(PrimitiveVal.CreateBooleanValue(v))); } catch (FormatException) { throw new InvalidOperationException(String.Format("InvalidData: {0}.{1} '{2}'", e.Name, c.Name, cv)); } }; } else if (TypeName.Equals("String", StringComparison.OrdinalIgnoreCase)) { Reader = TreeRow => { var cvs = TreeRow.Stem.Children.Where(col => col.OnStem && col.Stem.Name.Equals(c.Name, StringComparison.OrdinalIgnoreCase)).ToList(); if (cvs.Count != 1) { throw new InvalidOperationException(String.Format("InvalidData: {0}.{1}", e.Name, c.Name)); } var cv = cvs.Single().Stem.Children.Single().Leaf; if ((cv == null) || (cv == "-")) { return(ColumnVal.CreateOptional(Optional <PrimitiveVal> .Empty)); } var v = cv; return(ColumnVal.CreateOptional(PrimitiveVal.CreateStringValue(v))); }; } else if (TypeName.Equals("Int", StringComparison.OrdinalIgnoreCase)) { if (EnumParser != null) { Reader = TreeRow => { var cvs = TreeRow.Stem.Children.Where(col => col.OnStem && col.Stem.Name.Equals(c.Name, StringComparison.OrdinalIgnoreCase)).ToList(); if (cvs.Count != 1) { throw new InvalidOperationException(String.Format("InvalidData: {0}.{1}", e.Name, c.Name)); } var cv = cvs.Single().Stem.Children.Single().Leaf; if ((cv == null) || (cv == "-")) { return(ColumnVal.CreateOptional(Optional <PrimitiveVal> .Empty)); } if (EnumParser.ContainsKey(cv)) { var v = (int)(EnumParser[cv]); return(ColumnVal.CreateOptional(PrimitiveVal.CreateIntValue(v))); } else { try { var v = NumericStrings.InvariantParseInt32(cv); return(ColumnVal.CreateOptional(PrimitiveVal.CreateIntValue(v))); } catch (FormatException) { throw new InvalidOperationException(String.Format("InvalidData: {0}.{1} '{2}'", e.Name, c.Name, cv)); } } }; } else { Reader = TreeRow => { var cvs = TreeRow.Stem.Children.Where(col => col.OnStem && col.Stem.Name.Equals(c.Name, StringComparison.OrdinalIgnoreCase)).ToList(); if (cvs.Count != 1) { throw new InvalidOperationException(String.Format("InvalidData: {0}.{1}", e.Name, c.Name)); } var cv = cvs.Single().Stem.Children.Single().Leaf; if ((cv == null) || (cv == "-")) { return(ColumnVal.CreateOptional(Optional <PrimitiveVal> .Empty)); } try { var v = NumericStrings.InvariantParseInt32(cv); return(ColumnVal.CreateOptional(PrimitiveVal.CreateIntValue(v))); } catch (FormatException) { throw new InvalidOperationException(String.Format("InvalidData: {0}.{1} '{2}'", e.Name, c.Name, cv)); } }; } } else if (TypeName.Equals("Int64", StringComparison.OrdinalIgnoreCase)) { if (EnumParser != null) { Reader = TreeRow => { var cvs = TreeRow.Stem.Children.Where(col => col.OnStem && col.Stem.Name.Equals(c.Name, StringComparison.OrdinalIgnoreCase)).ToList(); if (cvs.Count != 1) { throw new InvalidOperationException(String.Format("InvalidData: {0}.{1}", e.Name, c.Name)); } var cv = cvs.Single().Stem.Children.Single().Leaf; if ((cv == null) || (cv == "-")) { return(ColumnVal.CreateOptional(Optional <PrimitiveVal> .Empty)); } if (EnumParser.ContainsKey(cv)) { var v = (Int64)(EnumParser[cv]); return(ColumnVal.CreateOptional(PrimitiveVal.CreateInt64Value(v))); } else { try { var v = NumericStrings.InvariantParseInt64(cv); return(ColumnVal.CreateOptional(PrimitiveVal.CreateInt64Value(v))); } catch (FormatException) { throw new InvalidOperationException(String.Format("InvalidData: {0}.{1} '{2}'", e.Name, c.Name, cv)); } } }; } else { Reader = TreeRow => { var cvs = TreeRow.Stem.Children.Where(col => col.OnStem && col.Stem.Name.Equals(c.Name, StringComparison.OrdinalIgnoreCase)).ToList(); if (cvs.Count != 1) { throw new InvalidOperationException(String.Format("InvalidData: {0}.{1}", e.Name, c.Name)); } var cv = cvs.Single().Stem.Children.Single().Leaf; if ((cv == null) || (cv == "-")) { return(ColumnVal.CreateOptional(Optional <PrimitiveVal> .Empty)); } try { var v = NumericStrings.InvariantParseInt64(cv); return(ColumnVal.CreateOptional(PrimitiveVal.CreateInt64Value(v))); } catch (FormatException) { throw new InvalidOperationException(String.Format("InvalidData: {0}.{1} '{2}'", e.Name, c.Name, cv)); } }; } } else if (TypeName.Equals("Real", StringComparison.OrdinalIgnoreCase)) { Reader = TreeRow => { var cvs = TreeRow.Stem.Children.Where(col => col.OnStem && col.Stem.Name.Equals(c.Name, StringComparison.OrdinalIgnoreCase)).ToList(); if (cvs.Count != 1) { throw new InvalidOperationException(String.Format("InvalidData: {0}.{1}", e.Name, c.Name)); } var cv = cvs.Single().Stem.Children.Single().Leaf; if ((cv == null) || (cv == "-")) { return(ColumnVal.CreateOptional(Optional <PrimitiveVal> .Empty)); } try { var v = NumericStrings.InvariantParseFloat64(cv); return(ColumnVal.CreateOptional(PrimitiveVal.CreateRealValue(v))); } catch (FormatException) { throw new InvalidOperationException(String.Format("InvalidData: {0}.{1} '{2}'", e.Name, c.Name, cv)); } }; } else if (TypeName.Equals("Binary", StringComparison.OrdinalIgnoreCase)) { Reader = TreeRow => { var cvs = TreeRow.Stem.Children.Where(col => col.OnStem && col.Stem.Name.Equals(c.Name, StringComparison.OrdinalIgnoreCase)).ToList(); if (cvs.Count != 1) { throw new InvalidOperationException(String.Format("InvalidData: {0}.{1}", e.Name, c.Name)); } var cv = cvs.Single().Stem.Children.Single().Leaf; if ((cv == null) || (cv == "-")) { return(ColumnVal.CreateOptional(Optional <PrimitiveVal> .Empty)); } try { var v = Regex.Split(cv.Trim(" \t\r\n".ToCharArray()), "( |\t|\r|\n)+", RegexOptions.ExplicitCapture).Select(s => Byte.Parse(s, System.Globalization.NumberStyles.HexNumber)).ToList(); return(ColumnVal.CreateOptional(PrimitiveVal.CreateBinaryValue(v))); } catch (FormatException) { throw new InvalidOperationException(String.Format("InvalidData: {0}.{1} '{2}'", e.Name, c.Name, cv)); } }; } else { throw new InvalidOperationException("InvalidType: {0}".Formats(TypeName)); } } return(Reader); }
/// <summary>基元数据</summary> public static ColumnVal CreatePrimitive(PrimitiveVal Value) { return(new ColumnVal { _Tag = ColumnValTag.Primitive, Primitive = Value }); }