Example #1
0
        internal string GenerateEnum(Type type)
        {
            var outputWriter = new OutputWriter();
            outputWriter.Write("namespace ")
                .Write(Utils.GetNamespace(type.Namespace, _options.NamespaceSubstitution)).EndLine()
                .Write("{").EndLine()
                .Indent()
                .Write("public enum ").Write(type.Name).EndLine()
                .Write("{").EndLine()
                .Indent();
            var values = Enum.GetValues(type);
            for(var i = 0; i < values.Length; i++)
            {
                var name = Enum.GetName(type, values.GetValue(i));
                var value = Convert.ChangeType(values.GetValue(i), Enum.GetUnderlyingType(type));
                if (name.ToString() == value.ToString())
                    outputWriter.Write(name);
                else
                    outputWriter.Write(name).Write(" = ") .Write(value.ToString());
                if (i < values.Length - 1)
                    outputWriter.Write(",");
                outputWriter.EndLine();
            }
            outputWriter
                .Unindent()
                .Write("}").EndLine()
                .Unindent()
                .Write("}").EndLine().EndLine()
                .Unindent();

            return outputWriter.ToString();
        }
Example #2
0
        internal string GenerateBase(Type type)
        {
            var outputWriter = new OutputWriter();
            outputWriter.Write("namespace ")
                .Write(Utils.GetNamespace(type.Namespace, _options.NamespaceSubstitution)).EndLine()
                .Write("{").EndLine()
                .Indent()
                .Write("public class ").Write(GetViewModelBaseTypeName(type));

            var isInheritsFromOtherViewModel = type.BaseType != typeof (Object);
            if (isInheritsFromOtherViewModel)
            {
                // assume this has already been validated
                outputWriter.Write(" : ").Write(Utils.GetViewModelTypeNameWithNamespace(type.BaseType, _options.NamespaceSubstitution));
                _nestedPropertyTypes.Add(type.BaseType);
            }
            else
            {
                var idProperty =
                    type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly).
                        FirstOrDefault(p => p.Name == "Id");
                var idPropertyType = typeof (int);
                if (idProperty != null)
                    idPropertyType = idProperty.PropertyType;
                outputWriter.Write(" : ViewModel<").Write(idPropertyType.FullName).Write(">");
            }

            outputWriter
                .EndLine()
                .Write("{").EndLine()
                .Indent();

            // write getter/setters for individual properties
            foreach(var p in type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
            {
                if (p.GetCustomAttributes(typeof(ViewModelIgnoreAttribute), true).Any())
                    continue;

                if (p.Name == "Id")
                    continue;

                var returnTypeName = GetPropertyTypeNameForMethodSignature(p.PropertyType, false);
                var implementationTypeName = GetPropertyTypeNameForMethodSignature(p.PropertyType);
                var jsPropertyName = GetJsPropertyName(p.Name);
                outputWriter.Write("public ").Write(returnTypeName).Write(" ").Write(p.Name)
                    .EndLine()
                    .Write("{").EndLine()
                    .Indent()
                    .Write("[InlineCode(\"{this}.get('").Write(jsPropertyName).Write("')\")]").EndLine()
                    //.Write("get { return ").Write("GetProperty<").Write(propertyTypeName).Write(">(\"").Write(jsPropertyName).Write("\"); }").EndLine()
                    .Write("get { return default(").Write(implementationTypeName).Write("); }").EndLine()
                    .Write("[InlineCode(\"{this}.set({{'").Write(jsPropertyName).Write("': {value}}})\")]").EndLine()
                    //.Write("set { SetProperty(\"").Write(jsPropertyName).Write("\", value); }").EndLine()
                    .Write("set { }").EndLine()
                    .Unindent()
                    .Write("}").EndLine();
                outputWriter.EndLine();
            }

            // write 'Set' method to update object from JSON
            var settableProperties = 0;
            foreach (var p in type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
            {
                if (!ShouldHaveSpecialSetOverride(p))
                    continue;
                settableProperties++;
            }

            settableProperties = 1; // TODO NCU TMP
            if (settableProperties > 0)
            {
                //outputWriter.Write("[PreserveCase]").EndLine();
                outputWriter.Write("public override bool SetFromJSON(JsDictionary<string, object> json, ViewSetOptions options)").EndLine()
                    .Write("{").EndLine()
                    .Indent()
                    .Write("if (json == null)").EndLine()
                    .Indent()
                    .Write("return true;").EndLine()
                    .Unindent();

                foreach (var p in type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
                {
                    if (!ShouldHaveSpecialSetOverride(p))
                        continue;

                    var jsPropertyName = GetJsPropertyName(p.Name);
                    outputWriter.Write("if (json.ContainsKey(\"").Write(jsPropertyName).Write("\"))").EndLine()
                        .Write("{").EndLine()
                        .Indent();
                    if (IsViewModelType(p.PropertyType))
                    {
                        // ViewModel
                        outputWriter.Write("if (this.").Write(p.Name).Write(" != null)").EndLine()
                            .Write("{").EndLine()
                            .Indent()
                            .Write("if (this.").Write(p.Name).Write(".SetFromJSON((JsDictionary<string, object>)json[\"").Write(jsPropertyName).Write("\"], options))").EndLine()
                            .Indent()
                            .Write("json.Remove(\"").Write(jsPropertyName).Write("\");").EndLine()
                            .Unindent()
                            .Write("else").EndLine()
                            .Indent()
                            .Write("return false;").EndLine()
                            .Unindent()
                            .Unindent()
                            .Write("}").EndLine()
                            .Write("else").EndLine()
                            .Write("{").EndLine()
                            .Indent()
                            .Write(GetPropertyTypeNameForMethodSignature(p.PropertyType, false)).Write(" x = new ").Write(GetPropertyTypeNameForMethodSignature(p.PropertyType)).Write("();").EndLine()
                            .Write("if (!x.SetFromJSON((JsDictionary<string, object>)json[\"").Write(jsPropertyName).Write("\"], options))").EndLine()
                            .Indent()
                            .Write("return false;").EndLine()
                            .Unindent()
                            .Write("json[\"").Write(jsPropertyName).Write("\"] = x;").EndLine()
                            //.Write("this.").Write(p.Name).Write(" = x;").EndLine()
                            .Unindent()
                            .Write("}").EndLine();
                    }
                    else
                    {
                        // ViewModelCollection
                        outputWriter
                            .Write(GetPropertyTypeNameForMethodSignature(p.PropertyType, false)).Write(" l = new ").Write(GetPropertyTypeNameForMethodSignature(p.PropertyType)).Write("();").EndLine()
                            .Write("if (this.").Write(p.Name).Write(" != null)").EndLine()
                            .Indent()
                            .Write("l = this.").Write(p.Name).Write(";").EndLine()
                            .Unindent()
                            .EndLine()
                            .Write("l.Clear();").EndLine()
                            .Write("var jsonList = (List<JsDictionary<string, object>>)json[\"").Write(jsPropertyName).Write("\"];").EndLine()
                            .Write("if (jsonList != null)")
                            .EndLine()
                            .Write("{").EndLine().Indent()
                            .Write("foreach(JsDictionary<string, object> itemJson in jsonList)").EndLine()
                            .Write("{").EndLine()
                            .Indent()
                            .Write(GetPropertyTypeNameForMethodSignature(p.PropertyType.GetGenericArguments()[0])).Write(" x = new ").Write(GetPropertyTypeNameForMethodSignature(p.PropertyType.GetGenericArguments()[0])).Write("();").EndLine()
                            .Write("if (!x.SetFromJSON(itemJson, options))").EndLine()
                            .Indent()
                            .Write("return false;").EndLine()
                            .Unindent()
                            .Write("l.Add(x);").EndLine()
                            .Unindent()
                            .Write("}").EndLine()
                            .Unindent().Write("}").EndLine()
                            .Write("json[\"").Write(jsPropertyName).Write("\"] = l;").EndLine();
                    }

                    outputWriter
                        .Unindent()
                        .Write("}").EndLine();
                }
                if (isInheritsFromOtherViewModel)
                    outputWriter.Write("return base.SetFromJSON(json, options);").EndLine();
                else
                    outputWriter.Write("return base.Set(json, options);").EndLine();

                outputWriter
                        .Unindent()
                        .Write("}").EndLine();
            }

            // write 'SetXFromString' methods
            foreach (var p in type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
            {
                if (p.GetCustomAttributes(typeof(ViewModelIgnoreAttribute), true).Any())
                    continue;

                if (IsPrimitiveOrNullableProperty(p.PropertyType))
                {
                    var jsPropertyName = GetJsPropertyName(p.Name);
                    outputWriter.Write("public void Set").Write(p.Name).Write("FromString(string value)")
                        .EndLine()
                        .Write("{").EndLine()
                        .Indent()
                        .Write("SetPropertyFromString(\"").Write(jsPropertyName).Write("\", value, typeof(").Write(GetPropertyTypeNameForJsTypeConversion(p.PropertyType)).Write("), ").Write(p.PropertyType.Name == "Nullable`1" ? "true" : "false").Write(");").EndLine()
                        .Unindent()
                        .Write("}").EndLine();
                    outputWriter.EndLine();
                }
                foreach (var propertyTypeForGeneration in GetPropertyTypeForGeneration(p.PropertyType))
                {
                    //Console.WriteLine("Adding nested property type " + propertyTypeForGeneration + " for property " + p.Name + " in " + type.FullName);
                    if (!_nestedPropertyTypes.Contains(propertyTypeForGeneration))
                        _nestedPropertyTypes.Add(propertyTypeForGeneration);
                }
            }

            // write 'Validate' method
            GenerateBaseValidateMethod(type, outputWriter);

            outputWriter.Unindent()
                .Write("}").EndLine()
                .Unindent()
                .Write("}").EndLine().EndLine();

            return outputWriter.ToString();
        }