Пример #1
0
        private void GenerateAdditionalTypeFromEnumHint(EnumHint enumHint, JsonSchema schema)
        {
            if (enumHint.AllowMemberCountMismatch == false &&
                enumHint.MemberNames != null &&
                schema.Enum != null &&
                enumHint.MemberNames.Length != schema.Enum.Count)
            {
                throw Error.CreateException(
                          Resources.ErrorMismatchedEnumCount,
                          nameof(EnumHint),
                          enumHint.TypeName,
                          enumHint.MemberNames.Length,
                          schema.Enum.Count);
            }

            var enumNames = new List <string>();

            if (!string.IsNullOrWhiteSpace(enumHint.ZeroValueName))
            {
                enumNames.Add(enumHint.ZeroValueName);
            }

            if (enumHint.MemberNames != null)
            {
                enumNames.AddRange(enumHint.MemberNames);
            }
            else
            {
                enumNames.AddRange(schema.Enum.Select(e => e.ToString()));
            }

            var enumTypeSchema = new JsonSchema
            {
                Description = enumHint.Description ?? schema.Description,
                Enum        = enumNames.Cast <object>().ToList()
            };

            var generator = new EnumGenerator(enumTypeSchema, _settings.TypeNameSuffix, _settings.HintDictionary);

            _pathToFileContentsDictionary[enumHint.TypeName + _settings.TypeNameSuffix] =
                generator.Generate(
                    _settings.SuffixedNamespaceName,
                    enumHint.TypeName,
                    _settings.CopyrightNotice,
                    enumTypeSchema.Description);
        }
Пример #2
0
        internal string GenerateClass(
            string className,
            JsonSchema schema,
            bool sealClasses,
            string classNameSuffix)
        {
            className = GetHintedClassName(className).ToPascalCase();
            string suffixedClassName = className + classNameSuffix;

            var propertyInfoDictionary = new PropertyInfoDictionary(
                className,
                _settings.TypeNameSuffix,
                schema,
                _settings.HintDictionary,
                OnAdditionalTypeRequired);

            _classInfoDictionary.Add(suffixedClassName, propertyInfoDictionary);

            EnumHint      enumHint      = null;
            InterfaceHint interfaceHint = null;

            if (_settings.HintDictionary != null)
            {
                string key = className.ToCamelCase();
                enumHint      = _settings.HintDictionary.GetHint <EnumHint>(key);
                interfaceHint = _settings.HintDictionary.GetHint <InterfaceHint>(key);
            }

            string baseInterfaceName = null;

            if (interfaceHint != null)
            {
                baseInterfaceName = "I" + suffixedClassName;
            }

            TypeGenerator typeGenerator;

            if (enumHint == null)
            {
                typeGenerator = new ClassGenerator(
                    propertyInfoDictionary,
                    schema,
                    _settings.HintDictionary,
                    baseInterfaceName,
                    _settings.GenerateEqualityComparers,
                    _settings.GenerateCloningCode,
                    _settings.SealClasses,
                    _nodeInterfaceName,
                    _kindEnumName,
                    _settings.TypeNameSuffix);

                if (_settings.GenerateCloningCode)
                {
                    // The cloning code includes an enumeration with one member for each
                    // generated class, so keep track of the class names.
                    _generatedClassNames.Add(suffixedClassName);
                }
            }
            else
            {
                typeGenerator = new EnumGenerator(schema, _settings.TypeNameSuffix, _settings.HintDictionary);
            }

            _pathToFileContentsDictionary[suffixedClassName] = typeGenerator.Generate(
                _settings.SuffixedNamespaceName,
                className,
                _settings.CopyrightNotice,
                schema.Description);

            if (interfaceHint != null)
            {
                typeGenerator = new InterfaceGenerator(
                    propertyInfoDictionary,
                    schema,
                    _settings.TypeNameSuffix,
                    _settings.HintDictionary);
                string description = interfaceHint.Description ?? schema.Description;

                _pathToFileContentsDictionary[baseInterfaceName + _settings.TypeNameSuffix] = typeGenerator.Generate(
                    _settings.SuffixedNamespaceName,
                    baseInterfaceName,
                    _settings.CopyrightNotice,
                    description);
            }

            return(_pathToFileContentsDictionary[suffixedClassName]);
        }