コード例 #1
0
 private string WriteBasicField(ClassName className, string paramName)
 {
     if (!TryGetBasicJsonType(className, out var jsonType) || manager.IsKnownClassOrBase(className))
     {
         return($"Write({paramName})");
     }
     else
     {
         return(paramName);
     }
 }
コード例 #2
0
        public ClassObject(ClassName fullName, IReadOnlyList <ClassField> fields, IReadOnlyList <FieldToCtorPair> ctorFields, IReadOnlyList <ClassGeneric> generics, ClassBaseObject baseObject = null, bool inlineFields = false, bool isAbstract = false, bool isInterface = false)
        {
            this.FullName     = fullName;
            this.Fields       = fields;
            this.CtorFields   = ctorFields;
            this.Generics     = generics;
            this.BaseObject   = baseObject;
            this.InlineFields = inlineFields;

            this.IsAbstract  = isAbstract;
            this.IsInterface = isInterface;
        }
コード例 #3
0
        private static bool TryGetReadListPrimitive(ClassName className, out string result)
        {
            if (className.Value.StartsWith("System.") && className.Value.Count((char c) => c == '.') == 1)
            {
                var primitiveType = CodeGeneratorUtils.GetPrimitiveName(className);
                result = primitiveType;
                return(true);
            }

            result = "";
            return(false);
        }
コード例 #4
0
        private static bool TryGetBasicJsonType(ClassName className, out string result)
        {
            if (className.Value.StartsWith("System.") && className.Value.Count((char c) => c == '.') == 1)
            {
                var primitiveType = CodeGeneratorUtils.GetPrimitiveName(className);
                if (primitiveType.Contains("int", StringComparison.OrdinalIgnoreCase) ||
                    primitiveType.Contains("uint", StringComparison.OrdinalIgnoreCase) ||
                    primitiveType.Contains("short", StringComparison.OrdinalIgnoreCase) ||
                    primitiveType.Contains("ushort", StringComparison.OrdinalIgnoreCase) ||
                    primitiveType.Contains("byte", StringComparison.OrdinalIgnoreCase) ||
                    primitiveType.Contains("sbyte", StringComparison.OrdinalIgnoreCase))
                {
                    result = "Int";
                    return(true);
                }

                if (primitiveType.Contains("long", StringComparison.OrdinalIgnoreCase) ||
                    primitiveType.Contains("ulong", StringComparison.OrdinalIgnoreCase))
                {
                    result = "Long";
                    return(true);
                }

                if (primitiveType.Contains("single", StringComparison.OrdinalIgnoreCase))
                {
                    result = "Float";
                    return(true);
                }

                if (primitiveType.Contains("double", StringComparison.OrdinalIgnoreCase))
                {
                    result = "Double";
                    return(true);
                }

                if (primitiveType.EndsWith("String", StringComparison.OrdinalIgnoreCase))
                {
                    result = "String";
                    return(true);
                }

                if (primitiveType.EndsWith("Boolean", StringComparison.OrdinalIgnoreCase))
                {
                    result = "Bool";
                    return(true);
                }
            }

            result = "";
            return(false);
        }
コード例 #5
0
        private static bool TryGetReadListPrimitive(ClassName className, out string result)
        {
            if (className.Value.StartsWith("System.") && className.Value.Count((char c) => c == '.') == 1)
            {
                var primitiveType = CodeGeneratorUtils.GetPrimitiveName(className);

                if (primitiveType.Contains("int", StringComparison.OrdinalIgnoreCase))
                {
                    result = "Int32";
                    return(true);
                }
                if (primitiveType.Contains("long", StringComparison.OrdinalIgnoreCase))
                {
                    result = "Int64";
                    return(true);
                }
                if (primitiveType.Contains("float", StringComparison.OrdinalIgnoreCase))
                {
                    result = "Single";
                    return(true);
                }
                if (primitiveType.Contains("double", StringComparison.OrdinalIgnoreCase))
                {
                    result = "Double";
                    return(true);
                }
                if (primitiveType.Contains("decimal", StringComparison.OrdinalIgnoreCase))
                {
                    result = "Decimal";
                    return(true);
                }
                if (primitiveType.Contains("string", StringComparison.OrdinalIgnoreCase))
                {
                    result = "String";
                    return(true);
                }
                if (primitiveType.Contains("bool", StringComparison.OrdinalIgnoreCase))
                {
                    result = "Boolean";
                    return(true);
                }
            }

            result = "";
            return(false);
        }
コード例 #6
0
        public static void WriteOuterClass(Manager manager, ClassName className, IndentedTextWriter writer, string classSuffix, IEnumerable <string> usingImports, Action writeInner)
        {
            writer.WriteLine($"// Auto generated {classSuffix} for {className}\n");

            foreach (var usingImport in usingImports)
            {
                writer.WriteLine($"using {usingImport};");
            }
            writer.WriteLine();

            writer.WriteLine($"namespace {string.Join('.', manager.NameSpace)}");
            writer.WriteLine("{");
            writer.Indent++;
            writer.WriteLine($"public static partial class {manager.BaseSerialiserClassName}{classSuffix}");
            writer.WriteLine("{");
            writer.Indent++;

            writeInner();

            writer.Indent--;
            writer.WriteLine("}");
            writer.Indent--;
            writer.WriteLine("}");
        }
コード例 #7
0
        private void WriteBasicField(ClassName className, string propertyName, string paramName)
        {
            var hasPropertyName = IsValidPropertyName(propertyName);

            if (!TryGetBasicJsonType(className, out var jsonType) || manager.IsKnownClassOrBase(className))
            {
                if (hasPropertyName)
                {
                    writer.WriteLine($"output.WritePropertyName({propertyName});");
                }
                writer.WriteLine($"Write({paramName}, output);");
            }
            else
            {
                if (!hasPropertyName)
                {
                    writer.WriteLine($"output.Write{jsonType}Value({paramName});");
                }
                else
                {
                    writer.WriteLine($"output.Write{jsonType}({propertyName}, {paramName});");
                }
            }
        }
コード例 #8
0
 public bool IsKnownClassOrBase(ClassName className)
 {
     return(this.classMap.ContainsKey(className) ||
            this.classBaseObjectMap.ContainsKey(className) ||
            this.classStubMap.ContainsKey(className));
 }
コード例 #9
0
        public ClassBaseObject AddBaseObjectFromType(Type type, string typeDiscriminatorName, Type interfaceBase = null)
        {
            var subclasses = Manager.GetEnumerableOfType(type);

            if (!subclasses.Any())
            {
                throw new Exception("BaseClass has no known sub classes");
            }

            var typeDiscriminatorField = subclasses.First().GetField(typeDiscriminatorName, BindingFlags.Public | BindingFlags.Static);

            if (typeDiscriminatorField == null)
            {
                throw new Exception("Unable to find type discriminator in sub class: " + subclasses.First().FullName);
            }

            if (!TryGetValidFieldType(typeDiscriminatorField, out var typeDiscriminatorType))
            {
                throw new Exception("Unable to get valid field type for type discrimination: " + type.FullName);
            }

            // Make sure that all comps have the same type discrimination and that it's on all sub classes.
            foreach (var subclass in subclasses.Skip(1))
            {
                var checkTypeDiscriminatorField = subclass.GetField(typeDiscriminatorName, BindingFlags.Public | BindingFlags.Static);

                if (checkTypeDiscriminatorField == null)
                {
                    throw new Exception("Not all sub classes have type discriminator field");
                }

                if (!TryGetValidFieldType(checkTypeDiscriminatorField, out var checkTypeDiscriminatorType))
                {
                    throw new Exception("Unable to get valid field type for sub class type discrimination");
                }

                if (checkTypeDiscriminatorType != typeDiscriminatorType)
                {
                    throw new Exception("Not all sub classes have the same type for type discrimination");
                }
            }

            var classType  = CreateTypeFromType(typeDiscriminatorType);
            var classField = new ClassField(typeDiscriminatorName, classType);

            var interfaceName = ClassName.Empty;

            if (interfaceBase != null)
            {
                interfaceName = new ClassName(interfaceBase.FullName);
            }

            var classGenerics = CreateGenericsFromType(type);

            // Bit sneaky to give the class base object a mutable list even though it wants a readonly one
            // and to update it after creation.
            var subclassObjects = new List <ClassBaseObject.SubclassPair>();
            var result          = new ClassBaseObject(new ClassName(type.FullName), classField, classGenerics, subclassObjects, interfaceName);

            foreach (var subclass in subclasses)
            {
                var classObject = this.CreateObjectFromType(subclass, result);
                var typeValue   = subclass.GetField(typeDiscriminatorName, BindingFlags.Public | BindingFlags.Static).GetValue(null);
                subclassObjects.Add(new ClassBaseObject.SubclassPair(classObject, typeValue));

                this.AddClass(classObject);
            }

            this.AddBaseClass(result);

            return(result);
        }
コード例 #10
0
 public ClassBaseObject(ClassName fullName, ClassField typeDiscriminator, IReadOnlyList <ClassGeneric> generics, IReadOnlyList <SubclassPair> subclasses, ClassName interfaceName)
 {
     this.FullName          = fullName;
     this.TypeDiscriminator = typeDiscriminator;
     this.Generics          = generics;
     this.Subclasses        = subclasses;
     this.InterfaceName     = interfaceName;
 }
コード例 #11
0
 protected virtual string TrimNameSpace(ClassName className)
 {
     return(className.TrimNameSpace(this.manager.NameSpace));
 }
コード例 #12
0
        protected virtual string MakeReadValueMethod(ClassName className)
        {
            var shortName = className.TrimNameSpace(this.manager.NameSpace).Replace(".", "");

            return("Read" + shortName);
        }
コード例 #13
0
        public static string GetPrimitiveName(ClassName className)
        {
            var lastDot = className.Value.LastIndexOf('.');

            return(className.Value.Substring(lastDot + 1));
        }
コード例 #14
0
 public static string ToCamelCase(ClassName input)
 {
     return(ToCamelCase(input.Value));
 }
コード例 #15
0
 public bool Equals(ClassName other)
 {
     return(this.Value == other.Value);
 }