示例#1
0
        private void ReadField(ClassObject classObject, ClassField classField)
        {
            var varString = classField.SafeCamelCaseName;
            var ctorPair  = classObject.CtorFields.FirstOrDefault(pair => pair.Field == classField);

            var inputField = $"input.GetProperty(\"{classField.CamelCaseName}\")";

            if (ctorPair != null && ctorPair.CtorField.HasDefaultValue)
            {
                inputField = classField.CamelCaseName + "Json";
                var startingValue = $"{CodeGeneratorUtils.WriteDefaultValue(this.TrimNameSpace(ctorPair.CtorField.Type.Name), ctorPair.CtorField.DefaultValue)}";

                writer.WriteLine($"var {varString} = {startingValue};");
                writer.WriteLine($"if (input.TryGetProperty(\"{classField.CamelCaseName}\", out var {inputField}))");
                writer.WriteLine("{");

                writer.Indent++;
                var valueString = ReadFieldType(inputField, classField.Name, classField.Type, 0);
                writer.WriteLine($"{varString} = {valueString};");

                writer.Indent--;
                writer.WriteLine("}");
            }
            else
            {
                var valueString = ReadFieldType(inputField, classField.Name, classField.Type, 0);

                if (varString != valueString)
                {
                    writer.WriteLine($"var {varString} = {valueString};");
                }
            }
        }
示例#2
0
        protected override void WriteField(ClassField classField)
        {
            var inputFieldName = $"input.{classField.Name}";
            var valueString    = WriteFieldType(classField.Type, inputFieldName, 0);

            writer.WriteLine($"result[\"{classField.CamelCaseName}\"] = {valueString};");
        }
示例#3
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;
 }
示例#4
0
        private void ReadField(ClassField classField)
        {
            var varString   = classField.SafeCamelCaseName;
            var valueString = ReadFieldType($"{classField.Name}", classField.Type, 0);

            if (varString != valueString)
            {
                writer.WriteLine($"var {varString} = {valueString};");
            }
        }
示例#5
0
        private void ReadField(ClassField classField)
        {
            var inputField  = $"input[\"{classField.CamelCaseName}\"]";
            var varString   = classField.SafeCamelCaseName;
            var valueString = ReadFieldType(inputField, classField.Name, classField.Type, 0);

            if (varString != valueString)
            {
                writer.WriteLine($"var {varString} = {valueString};");
            }
        }
示例#6
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);
        }
示例#7
0
 public FieldToCtorPair(ClassField field, ClassField ctorField)
 {
     this.Field     = field;
     this.CtorField = ctorField;
 }
示例#8
0
        protected override void WriteField(ClassField classField)
        {
            var inputFieldName = $"input.{classField.Name}";

            this.WriteFieldType(classField.Type, inputFieldName, 0);
        }
 protected abstract void WriteField(ClassField classField);