<commit_before>﻿namespace NerdCats.JsonSchemaDefaults
{
    using Newtonsoft.Json.Linq;
    using Newtonsoft.Json.Schema;
    using System.Linq;

    public class SchemaDefaultGenerator
    {
        public SchemaDefaultGenerator()
        { }

        public JObject GetDefaults(JObject schema)
        {
            return GetDefaults(schema.ToString());
        }

        public JObject GetDefaults(string schema)
        {
            var schemaObj = JSchema.Parse(schema);
            var schemaProperties = schemaObj.Properties;

            // TODO: Expecting to parse only Object JSON schemas for now, arrays are coming soon
            if (schemaProperties == null || !schema.Any())
                return JObject.Parse("{}");

            var returnObject = new JObject();

            foreach (var property in schemaProperties.Keys)
            {
                returnObject[property] = GetDefaultValue(schemaProperties[property]);
            }

            return returnObject;
        }

        private JToken GetDefaultValue(JSchema jSchema)
        {
            var defaultVal =  jSchema.Default;
            return defaultVal;
        }
    }<commit_msg>Little refactor, ready to recurse<commit_after>﻿namespace NerdCats.JsonSchemaDefaults
{
    using Newtonsoft.Json.Linq;
    using Newtonsoft.Json.Schema;
    using System;
    using System.Linq;

    public class SchemaDefaultGenerator
    {
        public SchemaDefaultGenerator()
        { }

        public JObject GetDefaults(JObject schema)
        {
            return GetDefaults(schema.ToString());
        }

        public JObject GetDefaults(string schema)
        {
            var schemaObj = JSchema.Parse(schema);
            var schemaProperties = schemaObj.Properties;

            // TODO: Expecting to parse only Object JSON schemas for now, arrays are coming soon
            if (schemaProperties == null || !schema.Any())
                return JObject.Parse("{}");

            return GetDefaultsFromSchema(schemaObj);
        }

        private JObject GetDefaultsFromSchema(JSchema schemaObj)
        {
            var returnObject = new JObject();
            switch (schemaObj.Type)
            {
                case JSchemaType.Object:
                    returnObject = GetDefaultsFromObject(schemaObj);
                    break;
                default:
                    throw new NotImplementedException(schemaObj.Type.ToString());
            }

            return returnObject;
        }

        private JObject GetDefaultsFromObject(JSchema schemaObj)
        {
            var schemaProperties = schemaObj.Properties;
            var returnObject = new JObject();
            foreach (var property in schemaProperties.Keys)
            {
                returnObject[property] = GetDefaultValue(schemaProperties[property]);
            }

            return returnObject;
        }

        private JToken GetDefaultValue(JSchema jSchema)
        {
            var defaultVal = jSchema.Default;
            return defaultVal;
        }
    }
{'fraction_non_alphanumeric': 0.07231675392670157, 'fraction_numerical': 0.0, 'mean_word_length': 1.8149171270718232, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4038693', 'n_tokens_mistral': 741, 'n_tokens_neox': 703, 'n_words': 178}