internal static bool ShouldBeEnum( this JsonSchema schema, string typeName, string propertyName, HintDictionary hintDictionary, out EnumHint enumHint) { enumHint = null; if (hintDictionary == null) { return(false); } enumHint = hintDictionary.GetPropertyHint <EnumHint>(typeName, propertyName); if (enumHint != null) { if (string.IsNullOrWhiteSpace(enumHint.TypeName)) { throw Error.CreateException( Resources.ErrorEnumHintRequiresTypeName, typeName, propertyName); } return(true); } return(false); }
public override void AddMembers() { if (Schema?.Enum.Count > 0) { EnumHint enumHint = GetEnumHintForType(TypeName); int[] enumValues = enumHint?.MemberValues; bool isFlagsEnum = enumHint?.Flags == true; var enumDeclaration = TypeDeclaration as EnumDeclarationSyntax; var enumMembers = new List <EnumMemberDeclarationSyntax>(); int enumValueIndexOffset = enumHint?.HasZeroValue == true ? 1 : 0; for (int i = 0; i < Schema.Enum.Count; ++i) { EqualsValueClauseSyntax equalsValueClause = null; int?enumValue = null; if (ShouldSupplyValueFor(i, enumHint?.HasZeroValue == true)) { if (enumValues != null) { enumValue = enumValues[i - enumValueIndexOffset]; } else if (isFlagsEnum) { enumValue = (int)Math.Pow(2, i - 1); } } if (enumValue.HasValue) { equalsValueClause = SyntaxFactory.EqualsValueClause( SyntaxFactory.LiteralExpression( SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(enumValue.Value))); } string enumName = Schema.Enum[i].ToString().ToPascalCase(); enumMembers.Add( SyntaxFactory.EnumMemberDeclaration( default(SyntaxList <AttributeListSyntax>), SyntaxFactory.Identifier(enumName), equalsValueClause)); } TypeDeclaration = enumDeclaration.AddMembers(enumMembers.ToArray()); } }
public override BaseTypeDeclarationSyntax GenerateTypeDeclaration() { var enumDeclaration = SyntaxFactory.EnumDeclaration(SyntaxFactory.Identifier(SuffixedTypeName)) .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword)); EnumHint enumHint = GetEnumHintForType(TypeName); if (enumHint?.Flags == true) { enumDeclaration = AddAttribute(enumDeclaration, FlagsAttributeName); AddUsing("System"); } return(enumDeclaration); }
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); }
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]); }