Skip to content

Customization

akovanev edited this page Nov 18, 2022 · 4 revisions

UIntGenerator

public class UIntGenerator : GeneratorBase
{
    protected override object CreateImpl(PropertyObject propertyObject)
    {
        Random random = GetRandomInstance(propertyObject, nameof(CreateImpl));
        return random.GetInt(0, 1000);    
    }

    protected override object CreateRangeFailureImpl(PropertyObject propertyObject)
    {
        Random random = GetRandomInstance(propertyObject, nameof(CreateRangeFailureImpl));
        return random.GetInt(-100, -1);
    }
}

public class ExtendedGeneratorFactory : GeneratorFactory
{
    public override Dictionary GetGeneratorDictionary()
    {
        Dictionary generators = base.GetGeneratorDictionary();
        generators.Add("uint", new UIntGenerator());
        return generators;
    }
}

//...
var dg = new DG(new ExtendedGeneratorFactory());
DataScheme scheme = dg.GetFromFile("data.json");
string output = dg.GenerateJson(scheme);

Calling the GetRandomInstance method, and passing nameof(method) as a parameter, should return the unique Random instance. Please do use it, rather than create your own Random object, as in this case, the randomness of the data will be lost at some extent.

CalcGenerator

public class StudentCalcGenerator : CalcGeneratorBase
{
    protected override object CreateImpl(CalcPropertyObject propertyObject)
    {
        if (string.Equals(propertyObject.Property.Name, "fullname", StringComparison.OrdinalIgnoreCase))
        {
            var val1 = propertyObject.Values
                .Single(v => String.Equals(v.Name, "firstname", StringComparison.OrdinalIgnoreCase));
            var val2 = propertyObject.Values
                .Single(v => String.Equals(v.Name, "lastname", StringComparison.OrdinalIgnoreCase));
           
            return $"{val1.Value} {val2.Value}";
        }

        if (string.Equals(propertyObject.Property.Name, "count", StringComparison.OrdinalIgnoreCase))
        {
            var val1 = propertyObject.Values
                .Single(v => String.Equals(v.Name, "students", StringComparison.OrdinalIgnoreCase))
                .Value as List<NameValueObject>;

            return val1!.Count;
        }

        throw new NotSupportedException("Not expected calculated property");
    }

    protected override object CreateRangeFailureImpl(CalcPropertyObject propertyObject)
    {
        throw new NotSupportedException("Range failure not supported");
    }
}

public class StudentGeneratorFactory : GeneratorFactory
{
    protected override Dictionary<string, GeneratorBase> GetGeneratorDictionary()
    {
        Dictionary<string, GeneratorBase> generators = base.GetGeneratorDictionary();
        generators.Add(TemplateType.Calc, new StudentCalcGenerator());
        return generators;
    }
}

//
var dg = new DG(new StudentGeneratorFactory());
string output = dg.GenerateJson<StudentCollection>(new StudentTestsProfiles());
Clone this wiki locally