Example #1
0
        private static void Generate(Generator file, string space, IEnumerable <Type> types)
        {
            file.Start($"export namespace {space}");

            foreach (var type in types.OrderBy(t => t.Name))
            {
                if (type.IsSubclassOf(typeof(BaseSettings)))
                {
                    continue;
                }

                var jsonAtt = type.GetCustomAttribute <JSONSerializableAttribute>();

                if (type.IsEnum)
                {
                }
                else if (typeof(JSONSerializable).IsAssignableFrom(type))
                {
                    file.Start($"export interface {type.Name}");
                    foreach (var member in Member.GetMembers(type))
                    {
                        if (member.GetAttributes <JSONSkipAttribute>().Any())
                        {
                            continue;
                        }

                        string name = member.Name;
                        var    att  = member.GetAttributes <JSONFieldAttribute>().ToList();
                        if (att.Any())
                        {
                            name = att.First().FieldName;
                        }

                        file.WriteField(name, GetJsType(member.MemberType));
                    }
                    file.Stop();
                }
                else if (jsonAtt != null)
                {
                    file.Start($"export interface {type.Name}");
                    foreach (var member in Member.GetMembers(type))
                    {
                        if (member.GetAttributes <JSONSkipAttribute>().Any())
                        {
                            continue;
                        }

                        string name = member.Name;
                        if (jsonAtt.CorrectPascalCase)
                        {
                            name = JSON.CorrectPascalCase(name);
                        }

                        file.WriteField(name, GetJsType(member.MemberType));
                    }
                    file.Stop();
                }
            }

            file.Stop();
        }