private void createConfigFile(List <string> statements, string prefix, string key, VAL val) { if (val.IsAssociativeArray()) { prefix = MakeVariableName(prefix, key); foreach (var member in val.Members) { createConfigFile(statements, prefix, member.Name, member.Value); } statements.Add(string.Empty); return; } if (val.IsList) { prefix = MakeVariableName(prefix, key); int index = 0; foreach (var item in val) { createConfigFile(statements, prefix, $"[{index}]", item); index++; } return; } string var = MakeVariableName(prefix, key); string code = $"{var} = {val.ToString()};"; statements.Add(code); }
private static string ToXML(VAL val, string tag, int tab) { StringWriter o = new StringWriter(); if (val.IsAssociativeArray()) { o.Write(Indent(tab)); o.WriteLine("<" + tag + ">"); for (int i = 0; i < val.Size; i++) { VAL v = val[i]; o.Write(ToXML(v[1], v[0].Str, tab + 1)); } o.Write(Indent(tab)); o.WriteLine("</" + tag + ">"); } else if (val.ty == VALTYPE.listcon) { for (int j = 0; j < val.Size; j++) { VAL v = val[j]; o.Write(ToXML(v, tag, tab + 1)); } } else { o.Write(Indent(tab)); o.Write("<" + tag + ">"); o.Write(XmlString(val.ToString2())); o.WriteLine("</" + tag + ">"); } return(o.ToString()); }
private void createKeyValues(Dictionary <string, VAL> dict, TableName tname, string prefix, string key, VAL val) { if (val.IsAssociativeArray()) { if (prefix == string.Empty) { prefix = key; } else { prefix = $"{prefix}.{key}"; } foreach (var member in val.Members) { createKeyValues(dict, tname, prefix, member.Name, member.Value); continue; } return; } string var = $"{prefix}.{key}"; if (prefix == string.Empty) { var = key; } dict.Add(var, val); }
private static void CopyContext(VAL context) { if (context.Defined && context.IsAssociativeArray()) { foreach (Member member in context.Members) { Context.DS.Add(member.Name, member.Value); } } }
private void create(Class clss, string prefix, string key, VAL val) { TypeInfo ty; Property prop; if (val.IsAssociativeArray()) { var clss1 = new Class(key) { Modifier = Modifier.Public, Sorted = true }; clss.Add(clss1); if (prefix == string.Empty) { prefix = key; } else { prefix = $"{prefix}.{key}"; } foreach (var member in val.Members) { create(clss1, prefix, member.Name, member.Value); continue; } ty = new TypeInfo { UserType = key }; } else { Type type = typeof(string); if (val.HostValue != null) { type = val.HostValue.GetType(); } ty = new TypeInfo(type); } string var = $"{prefix}.{key}"; if (prefix == string.Empty) { var = key; } prop = createProperty(key, ty, var); clss.Add(prop); }
private static string ToJson(VAL val, string tag, int tab, bool quotationMark) { StringWriter o = new StringWriter(); o.Write(Indent(tab)); if (tag != "") { if (quotationMark) { o.Write("\"" + tag + "\""); } else { o.Write(tag); } o.Write(" : "); } if (val.IsAssociativeArray()) { o.WriteLine("{"); for (int i = 0; i < val.Size; i++) { VAL v = val[i]; o.Write(ToJson(v[1], v[0].Str, tab + 1, quotationMark)); if (i < val.Size - 1) { o.WriteLine(","); } else { o.WriteLine(); } } o.Write(Indent(tab)); o.Write("}"); if (!quotationMark && val.Class != null) { o.Write(".typeof(\"{0}\")", val.Class); } } else if (val.ty == VALTYPE.listcon) { o.WriteLine("["); for (int j = 0; j < val.Size; j++) { VAL a = val[j]; o.Write(ToJson(a, "", tab + 1, quotationMark)); if (j < val.Size - 1) { o.WriteLine(","); } else { o.WriteLine(); } } o.Write(Indent(tab)); o.Write("]"); if (!quotationMark && val.Class != null) { o.Write(".typeof(\"{0}\")", val.Class); } } else if (val.ty == VALTYPE.hostcon) { val = HostValization.Host2Valor(val.value); if (val.ty == VALTYPE.listcon) { o.Write(ToJson(val, "", tab, quotationMark)); } else { o.Write(val.Valor); } } else { o.Write(val.Valor); } return(o.ToString()); }
private void createConfigKeyMap(Class clss, string prefix, string key, VAL val) { if (val.IsAssociativeArray()) { var clss1 = new Class(key) { Modifier = Modifier.Public | Modifier.Static }; clss.Add(clss1); prefix = MakeVariableName(prefix, key); foreach (var member in val.Members) { createConfigKeyMap(clss1, prefix, member.Name, member.Value); } return; } //if (val.IsList) //{ // prefix = MakeVariableName(prefix, key); // int index = 0; // foreach (var item in val) // { // createConfigKeyMap(clss, prefix, $"[{index}]", item); // index++; // } // return; //} if (val.IsFunction) { return; } Type type = typeof(string); if (val.HostValue != null) { type = val.HostValue.GetType(); } TypeInfo ty = new TypeInfo(type); string var = MakeVariableName(prefix, key); switch (HierarchicalMemberType) { case CodeMemberType.Property: Property prop = createProperty(key, ty, var); clss.Add(prop); break; case CodeMemberType.Method: Method mtd = createMethod(key, ty, var); clss.Add(mtd); break; default: Field fld = createField(key, ty, var); clss.Add(fld); break; } Other(ty, var, val); }
private void createClass(Class clss, string prefix, string key, VAL val, bool classOnly) { TypeInfo ty = null; string path = null; if (val.IsAssociativeArray()) { var clss1 = new Class(key) { Modifier = Modifier.Public | Modifier.Partial }; builder.AddClass(clss1); prefix = MakeVariableName(prefix, key); foreach (var member in val.Members) { createClass(clss1, prefix, member.Name, member.Value, classOnly: false); } if (classOnly) { return; } ty = new TypeInfo(key); } else if (val.IsList) { Dictionary <string, VAL> dict = new Dictionary <string, VAL>(); foreach (var item in val) { if (item.IsAssociativeArray()) { foreach (var member in item.Members) { if (!dict.ContainsKey(member.Name)) { dict.Add(member.Name, member.Value); } } } } VAL _val = new VAL(); foreach (var kvp in dict) { _val.AddMember(kvp.Key, kvp.Value); } if (dict.Count > 0) { //if (key.EndsWith("s")) // key = key.Substring(0, key.Length - 1); createClass(clss, prefix, key, _val, classOnly: true); ty = new TypeInfo(key) { IsArray = true }; path = MakeVariableName(prefix, $"{key}[]"); } } if (ty == null) { Type type = typeof(object); if (val.HostValue != null) { type = val.HostValue.GetType(); } ty = new TypeInfo(type); } if (path == null) { path = MakeVariableName(prefix, key); } Property prop = createProperty(key, ty, path); clss.Add(prop); }
/// <summary> /// parameters can be VAL, Dictionary, Json, DbParameter, and anonymous class /// </summary> /// <param name="parameters"></param> /// <returns></returns> public SqlCmd ParseParameters(object parameters) { if (parameters == null) { return(this); } if (parameters is VAL) { foreach (var parameter in (VAL)parameters) { AddParameter((string)parameter[0], parameter[1].HostValue); } } //Dictionary else if (parameters is IEnumerable <KeyValuePair <string, object> > ) { var args = (IEnumerable <KeyValuePair <string, object> >)parameters; foreach (var kvp in args) { AddParameter(kvp.Key, kvp.Value); } } //JSON else if (parameters is string) { string args = (string)parameters; if (string.IsNullOrEmpty(args)) { return(this); } VAL val = Script.Evaluate(args); if (val.IsAssociativeArray()) { foreach (var element in val) { if (element[0].Value is string) { string name = (string)element[0]; object value = element[1].HostValue; AddParameter(name, value); } } } else { throw new Exception($"invalid json parameters: {parameters}"); } } //DbParameters else if (parameters is IEnumerable <IDataParameter> ) { var args = (IEnumerable <IDataParameter>)parameters; foreach (var arg in args) { var p = AddParameter(arg.ParameterName, arg.Value); p.DbType = arg.DbType; p.Direction = arg.Direction; } } else { var args = parameters.GetType().GetProperties().Where(p => p.CanRead); foreach (var propertyInfo in args) { AddParameter(propertyInfo.Name, propertyInfo.GetValue(parameters)); } } return(this); }