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();
        }
Example #3
0
 internal string GenerateServiceArgs()
 {
     foreach (var serviceArguments in _argumentTypes)
     {
         KeyValuePair<string, ServiceArguments> arguments = serviceArguments;
         var hasOthers = _argumentTypes.Count(kvp => kvp.Value.HasArgumentsWithSameNameButDifferentTypes(arguments.Value)) > 1;
         if (arguments.Value.Arguments.Count == 0)
             serviceArguments.Value.GeneratedTypeName = null;
         else if (arguments.Value.Arguments.Count == 1
             && arguments.Value.Arguments[0].Name.ToLower() == "id"
             && arguments.Value.Arguments[0].Type == typeof(int))
         // special case for Id
         {
             serviceArguments.Value.GeneratedTypeName = "IdServiceParam";
             var ns = serviceArguments.Key.Substring(0, serviceArguments.Key.IndexOf(':'));
             ns = ns.Substring(0, ns.LastIndexOf('.'));
             // TODO NCU this is incorrect - find the common base between Item1 and Item2 and use that.
             if (_options.NamespaceSubstitution != null && _options.NamespaceSubstitution.Item1 != null)
                 ns = ns.Substring(0, _options.NamespaceSubstitution.Item1.Length);
             serviceArguments.Value.GeneratedTypeNamespace = ns;
         }
         else
         {
             serviceArguments.Value.GeneratedTypeName = serviceArguments.Key.Substring(serviceArguments.Key.LastIndexOf('.') + 1).Replace(":", "_") + "ServiceParam";
             var ns = serviceArguments.Key.Substring(0, serviceArguments.Key.IndexOf(':'));
             ns = ns.Substring(0, ns.LastIndexOf('.'));
             serviceArguments.Value.GeneratedTypeNamespace = ns;
         }
     }
     var generated = new List<string>();
     var outputWriter = new OutputWriter();
     foreach (var serviceArguments in _argumentTypes)
     {
         if (serviceArguments.Value.Arguments.Count == 0)
             continue;
         if (generated.Contains(serviceArguments.Value.GeneratedTypeName))
             continue;
         outputWriter.Write("namespace ")
             .Write(Utils.GetNamespace(serviceArguments.Value.GeneratedTypeNamespace, _options.NamespaceSubstitution)).EndLine()
             .Write("{").EndLine()
             .Indent();
         outputWriter
             .Write("[IgnoreNamespace]")
             .Write("[Imported]")
             .Write("[ScriptName(\"Object\")]")
             .EndLine();
         outputWriter.Write("public class ").Write(serviceArguments.Value.GeneratedTypeName).EndLine()
             .Write("{").EndLine()
             .Indent();
         foreach(var param in serviceArguments.Value.Arguments)
         {
             outputWriter
                 .Write("[IntrinsicProperty]")
                 .EndLine();
             outputWriter
                 .Write("public ")
                 .Write(GetParameterTypeForCallMethod(param.Type))
                 .Write(" ").Write(GetParameterName(param.Name))
                 .Write(" { get; set; }")
                 .EndLine()
                 .EndLine();
         }
         outputWriter
             .Unindent()
             .Write("}").EndLine()
             .Unindent()
             .Write("}").EndLine();
         generated.Add(serviceArguments.Value.GeneratedTypeName);
     }
     return outputWriter.ToString();
 }
Example #4
0
        internal Dictionary<string, string> GenerateSuper(Type type)
        {
            if (!IsApplicableForGeneration(type))
                return null;
            var ret = new Dictionary<string, string>();
            foreach (var method in GetApplicableControllerMethods(type))
            {
                var outputWriter = new OutputWriter();
                outputWriter.Write("namespace ")
                    .Write(Utils.GetNamespace(type.Namespace, _options.NamespaceSubstitution)).EndLine()
                    .Write("{").EndLine()
                    .Indent();

                outputWriter.Write("[DependencyDefinition]")
                    .EndLine();
                outputWriter.Write("public class ").Write(GetServiceTypeName(type.Name, method.Name))
                    .Write(" : ").Write(GetServiceBaseTypeName(type.Name, method.Name)).EndLine()
                    .Write("{").EndLine()
                    .Write("}").EndLine();

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

                ret[Utils.GetNamespace(type.Namespace, _options.NamespaceSubstitution) + "." + GetServiceTypeName(type.Name, method.Name)] = outputWriter.ToString();
            }
            return ret;
        }
Example #5
0
        internal string GenerateBase(Type type)
        {
            if (!IsApplicableForGeneration(type))
                return null;
            var outputWriter = new OutputWriter();
            foreach (var method in GetApplicableControllerMethods(type))
            {
                var clientServiceAttribute = (ClientServiceAttribute)method.GetCustomAttributes(typeof(ClientServiceAttribute), true)[0];

                var returnType = clientServiceAttribute.ReturnType;
                var args = _argumentTypes[type.FullName + ":" + method.Name];

                outputWriter.Write("namespace ")
                    .Write(Utils.GetNamespace(type.Namespace, _options.NamespaceSubstitution)).EndLine()
                    .Write("{").EndLine()
                    .Indent();
                outputWriter.Write("public class ").Write(GetServiceBaseTypeName(type.Name, method.Name))
                    .Write(" : Service")
                    .Write("<");
                if (args.Arguments.Count > 0)
                    outputWriter.Write(args.GeneratedTypeName).Write(",");
                else
                    outputWriter.Write("object,");
                outputWriter.Write(Utils.GetViewModelTypeNameWithNamespace(returnType, _viewModelGeneratorOptions.NamespaceSubstitution)).Write(">")
                //outputWriter.Write(Utils.GetNamespace(returnType.Namespace, _options.NamespaceSubstitution) + "." + Utils.GetViewModelTypeName(returnType)).Write(">")
                    .EndLine()
                    .Write("{").EndLine()
                    .Indent();

                // Call method
                outputWriter.Write("public override void Call(");
                if (args.Arguments.Count > 0)
                    outputWriter.Write(args.GeneratedTypeName).Write(" query");
                else
                    outputWriter.Write("object ignored = null");

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

                if (args.Arguments.Count > 0)
                    outputWriter.Write("DoCall(query);").EndLine();
                else
                    outputWriter.Write("DoCall();").EndLine();

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

                // AddOnCompleteCallback method
                /*
                var returnType = clientServiceAttribute.ReturnType;
                outputWriter.Write("public void AddOnCompleteCallback(Action<Dictionary<string, object>> callback)").EndLine()
                    .Write("{").EndLine()
                    .Indent()
                    .Write("DoAddOnSuccessCallback(callback);").EndLine()
                    .Unindent()
                    .Write("}").EndLine();
                 */

                // overridable HttpMethod { get; } property
                var isPost = method.GetCustomAttributes(typeof(HttpPostAttribute), true).Length != 0;
                var acceptVerbsAttributes = (AcceptVerbsAttribute)method.GetCustomAttributes(typeof(AcceptVerbsAttribute), true).FirstOrDefault();
                if (acceptVerbsAttributes != null && acceptVerbsAttributes.Verbs.Select(x => x.ToUpper()).Any(x => x == "POST"))
                    isPost = true;

                if (isPost)
                {
                    outputWriter.Write("public override string HttpMethod").EndLine()
                        .Write("{").EndLine()
                        .Indent()
                        .Write("get { return \"").Write("POST").Write("\"; }").EndLine()
                        .Unindent()
                        .Write("}").EndLine();
                }

                // overrideable InstantiateModel method
                // if using a model
                /*
                if (!Utils.PrimitiveTypes.Contains(returnType))
                {
                    outputWriter.Write("protected override object InstantiateModel(Dictionary<string, object> returnValue)").EndLine()
                        .Write("{").EndLine()
                        .Indent()
                        .Write(GetReturnTypeForInstantiateMethod(returnType)).Write(" x = new ").Write(GetReturnTypeForInstantiateMethod(returnType)).Write("();").EndLine()
                        .Write("x.Set(returnValue, null);").EndLine()
                        .Write("return x;").EndLine()
                        .Unindent()
                        .Write("}").EndLine();
                }
                 */

                outputWriter.Write("public override string GetUrl()").EndLine()
                    .Write("{").EndLine()
                    .Indent()
                    .Write("return \"/")
                        .Write(type.Name.Replace("Controller", ""))
                        .Write("/")
                        .Write(method.Name)
                        .Write("\";").EndLine()
                    .Unindent()
                    .Write("}").EndLine();

                // end class
                outputWriter
                    .Unindent()
                    .Write("}").EndLine();

                // end namespace
                outputWriter
                    .Unindent()
                    .Write("}").EndLine().EndLine();
            }
            return outputWriter.ToString();
        }