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);
        }
        internal static bool ShouldBeDictionary(
            this JsonSchema schema,
            string typeName,
            string propertyName,
            HintDictionary hintDictionary,
            out DictionaryHint dictionaryHint)
        {
            dictionaryHint = null;

            // Ignore any DictionaryHint that might apply to this property
            // if the property is not an object.
            if (schema.SafeGetType() != SchemaType.Object)
            {
                return(false);
            }

            // Is there a DictionaryHint that targets this property?
            if (hintDictionary == null)
            {
                return(false);
            }

            dictionaryHint = hintDictionary.GetPropertyHint <DictionaryHint>(typeName, propertyName);
            if (dictionaryHint == null)
            {
                return(false);
            }

            return(true);
        }
Beispiel #3
0
 public EnumGenerator(
     JsonSchema schema,
     string typeNameSuffix,
     HintDictionary hintDictionary)
     : base(schema, typeNameSuffix, hintDictionary)
 {
 }
 public InterfaceGenerator(
     PropertyInfoDictionary propertyInfoDictionary,
     JsonSchema schema,
     string typeNameSuffix,
     HintDictionary hintDictionary)
     : base(propertyInfoDictionary, schema, typeNameSuffix, hintDictionary)
 {
 }
Beispiel #5
0
 public ClassOrInterfaceGenerator(
     PropertyInfoDictionary propertyInfoDictionary,
     JsonSchema schema,
     string typeNameSuffix,
     HintDictionary hintDictionary)
     : base(schema, typeNameSuffix, hintDictionary)
 {
     PropInfoDictionary = propertyInfoDictionary;
 }
Beispiel #6
0
 protected TypeGenerator(
     JsonSchema schema,
     string typeNameSuffix,
     HintDictionary hintDictionary)
 {
     Schema          = schema;
     HintDictionary  = hintDictionary;
     _typeNameSuffix = typeNameSuffix;
 }
Beispiel #7
0
 private EnumHint GetEnumHintForType(string typeName)
 {
     return(HintDictionary
            .SelectMany(kvp => kvp.Value)
            .Where(hint => hint is EnumHint)
            .Cast <EnumHint>()
            .Where(eh => eh.TypeName == typeName)
            .FirstOrDefault());
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="PropertyInfoDictionary"/> class.
        /// </summary>
        /// <param name="typeName">
        /// The name of the type described by <paramref name="schema"/>.
        /// </param>
        /// <param name="schema">
        /// A schema describing each property in the class.
        /// </param>
        /// <param name="hintDictionary">
        /// A dictionary of hints to guide code generation.
        /// </param>
        public PropertyInfoDictionary(
            string typeName,
            string typeNameSuffix,
            JsonSchema schema,
            HintDictionary hintDictionary,
            AdditionalTypeRequiredDelegate additionalTypeRequiredDelegate)
        {
            _typeName       = typeName;
            _typeNameSuffix = typeNameSuffix;
            _schema         = schema;
            _hintDictionary = hintDictionary;
            _additionalTypeRequiredDelegate = additionalTypeRequiredDelegate;

            _dictionary = PropertyInfoDictionaryFromSchema();
        }
        protected override bool IncludeProperty(string propertyName)
        {
            const string WildCard = "*";

            string hintDictionaryKey = MakeHintDictionaryKey(propertyName);
            PropertyModifiersHint propertyModifiersHint = HintDictionary.GetHint <PropertyModifiersHint>(hintDictionaryKey);

            if (propertyModifiersHint == null)
            {
                hintDictionaryKey     = WildCard + "." + propertyName.ToPascalCase();
                propertyModifiersHint = HintDictionary.GetHint <PropertyModifiersHint>(hintDictionaryKey);
            }

            if (propertyModifiersHint?.Modifiers.Count > 0)
            {
                bool isPublic = propertyModifiersHint.Modifiers.Contains(SyntaxFactory.Token(SyntaxKind.PublicKeyword));
                return(isPublic);
            }

            return(true);
        }
Beispiel #10
0
        private static int Run(Options options)
        {
            int exitCode = 1;

            try
            {
                string     jsonText = File.ReadAllText(options.SchemaFilePath);
                JsonSchema schema   = SchemaReader.ReadSchema(jsonText, options.SchemaFilePath);

                HintDictionary hintDictionary = null;
                if (options.CodeGenHintsPath != null)
                {
                    if (!File.Exists(options.CodeGenHintsPath))
                    {
                        throw new ArgumentException(
                                  string.Format(
                                      CultureInfo.CurrentCulture,
                                      Resources.ErrorHintsFileNotFound,
                                      options.CodeGenHintsPath));
                    }

                    string hintDictionaryText = File.ReadAllText(options.CodeGenHintsPath);
                    hintDictionary = new HintDictionary(hintDictionaryText, options.TypeNameSuffix);
                }

                string copyrightNotice = null;
                if (options.CopyrightFilePath != null)
                {
                    if (!File.Exists(options.CopyrightFilePath))
                    {
                        throw new ArgumentException(
                                  string.Format(
                                      CultureInfo.CurrentCulture,
                                      Resources.ErrorCopyrightFileNotFound,
                                      options.CopyrightFilePath));
                    }

                    copyrightNotice = File.ReadAllText(options.CopyrightFilePath);
                }

                DataModelGeneratorSettings settings = new DataModelGeneratorSettings
                {
                    OutputDirectory           = options.OutputDirectory,
                    TypeNameSuffix            = options.TypeNameSuffix,
                    ForceOverwrite            = options.ForceOverwrite,
                    NamespaceName             = options.NamespaceName,
                    RootClassName             = options.RootClassName,
                    SchemaName                = options.SchemaName,
                    CopyrightNotice           = copyrightNotice,
                    HintDictionary            = hintDictionary,
                    GenerateEqualityComparers = options.GenerateEqualityComparers,
                    GenerateCloningCode       = options.GenerateCloningCode,
                    SealClasses               = options.SealClasses
                };

                new DataModelGenerator(settings).Generate(schema);

                exitCode = 0;
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(
                    string.Format(
                        CultureInfo.CurrentCulture,
                        Resources.Error,
                        ex.Message));
            }

            return(exitCode);
        }