Ejemplo n.º 1
0
            /// <summary>
            /// Gets a class name as a CodeTypeReference for the given schema of the form "IntenalClassN" where
            /// N is an integer. Given the same JsonSchema this will return the same classname.
            /// </summary>
            public CodeTypeReference GetClassName(JsonSchema definition, SchemaImplementationDetails details)
            {
                if (knownSubschemas.ContainsKey(definition))
                {
                    return(knownSubschemas[definition].Reference);
                }

                string name = null;

                // First, try to generate a name based upon the environment.
                if (typeDeclaration != null && details != null && !string.IsNullOrEmpty(details.ProposedName))
                {
                    string proposedName = details.ProposedName;
                    IEnumerable <string> forbiddenWords = GeneratorUtils.GetWordContextListFromClass(typeDeclaration);
                    forbiddenWords =
                        forbiddenWords.Concat(from KnownSubschema k in knownSubschemas.Values select k.ClassName);

                    string generatedName = GeneratorUtils.GetClassName(proposedName, forbiddenWords);
                    if (generatedName.IsNotNullOrEmpty())
                    {
                        // The generated name is valid -> take it.
                        name = generatedName;
                    }
                }

                // If this name collides with an existing type, generate a unique name.
                if (name == null)
                {
                    name = GetSchemaName(knownSubschemas.Count + 1);
                }

                // If the current class is not null, set the prefix to the class name as it will be required for
                // addressing this nested type.
                string prefix = "";

                if (typeDeclaration != null)
                {
                    prefix = typeDeclaration.Name + ".";
                }

                var newSubschema = new KnownSubschema();

                newSubschema.Reference = new CodeTypeReference(prefix + name);
                newSubschema.ClassName = name;
                knownSubschemas.Add(definition, newSubschema);
                return(newSubschema.Reference);
            }
Ejemplo n.º 2
0
            /// <summary>
            /// Gets a class name as a CodeTypeReference for the given schema of the form "IntenalClassN" where 
            /// N is an integer. Given the same JsonSchema this will return the same classname.
            /// </summary>
            public CodeTypeReference GetClassName(JsonSchema definition, SchemaImplementationDetails details)
            {
                if (knownSubschemas.ContainsKey(definition))
                {
                    return knownSubschemas[definition].Reference;
                }

                string name = null;
                
                // First, try to generate a name based upon the environment.
                if (typeDeclaration != null && details != null && !string.IsNullOrEmpty(details.ProposedName))
                {
                    string proposedName = details.ProposedName;
                    IEnumerable<string> forbiddenWords = GeneratorUtils.GetWordContextListFromClass(typeDeclaration);
                    forbiddenWords =
                        forbiddenWords.Concat(from KnownSubschema k in knownSubschemas.Values select k.ClassName);

                    string generatedName = GeneratorUtils.GetClassName(proposedName, forbiddenWords);
                    if (generatedName.IsNotNullOrEmpty())
                    {
                        // The generated name is valid -> take it.
                        name = generatedName;
                    }
                }

                // If this name collides with an existing type, generate a unique name.
                if (name == null)
                {
                    name = GetSchemaName(knownSubschemas.Count+1);
                }

                // If the current class is not null, set the prefix to the class name as it will be required for
                // addressing this nested type.
                string prefix = "";
                if (typeDeclaration != null)
                {
                    prefix = typeDeclaration.Name + ".";
                }

                var newSubschema = new KnownSubschema();
                newSubschema.Reference = new CodeTypeReference(prefix + name);
                newSubschema.ClassName = name;
                knownSubschemas.Add(definition, newSubschema);
                return newSubschema.Reference;
            }