Example #1
0
    public static string?FormatString(PropertyRoute route)
    {
        PropertyRoute simpleRoute = route.SimplifyToProperty();

        FormatAttribute?format = simpleRoute.PropertyInfo !.GetCustomAttribute <FormatAttribute>();

        if (format != null)
        {
            return(format.Format);
        }

        var pp = Validator.TryGetPropertyValidator(simpleRoute);

        if (pp != null)
        {
            DateTimePrecisionValidatorAttribute?datetimePrecision = pp.Validators.OfType <DateTimePrecisionValidatorAttribute>().SingleOrDefaultEx();
            if (datetimePrecision != null)
            {
                return(datetimePrecision.FormatString);
            }

            TimePrecisionValidatorAttribute?timeSpanPrecision = pp.Validators.OfType <TimePrecisionValidatorAttribute>().SingleOrDefaultEx();
            if (timeSpanPrecision != null)
            {
                return(route.Type.UnNullify() == typeof(TimeSpan) ? timeSpanPrecision.FormatString_TimeSpan :
                       route.Type.UnNullify() == typeof(TimeOnly) ? timeSpanPrecision.FormatString_TimeOnly :
                       throw new UnexpectedValueException(route.Type));
            }

            DecimalsValidatorAttribute?decimals = pp.Validators.OfType <DecimalsValidatorAttribute>().SingleOrDefaultEx();
            if (decimals != null)
            {
                return("N" + decimals.DecimalPlaces);
            }

            StringCaseValidatorAttribute?stringCase = pp.Validators.OfType <StringCaseValidatorAttribute>().SingleOrDefaultEx();
            if (stringCase != null)
            {
                return(stringCase.TextCase == StringCase.Lowercase ? "L" : "U");
            }
        }

        if (route.IsId() && ReflectionTools.IsNumber(PrimaryKey.Type(route.RootType)))
        {
            return("D");
        }

        return(FormatString(route.Type));
    }
Example #2
0
        public static string FormatString(PropertyRoute route)
        {
            PropertyRoute simpleRoute = route.SimplifyToProperty();

            FormatAttribute format = simpleRoute.PropertyInfo.GetCustomAttribute <FormatAttribute>();

            if (format != null)
            {
                return(format.Format);
            }

            var pp = Validator.TryGetPropertyValidator(simpleRoute);

            if (pp != null)
            {
                DateTimePrecissionValidatorAttribute datetimePrecission = pp.Validators.OfType <DateTimePrecissionValidatorAttribute>().SingleOrDefaultEx();
                if (datetimePrecission != null)
                {
                    return(datetimePrecission.FormatString);
                }

                TimeSpanPrecissionValidatorAttribute timeSpanPrecission = pp.Validators.OfType <TimeSpanPrecissionValidatorAttribute>().SingleOrDefaultEx();
                if (timeSpanPrecission != null)
                {
                    return(timeSpanPrecission.FormatString);
                }

                DecimalsValidatorAttribute decimals = pp.Validators.OfType <DecimalsValidatorAttribute>().SingleOrDefaultEx();
                if (decimals != null)
                {
                    return("N" + decimals.DecimalPlaces);
                }

                StringCaseValidatorAttribute stringCase = pp.Validators.OfType <StringCaseValidatorAttribute>().SingleOrDefaultEx();
                if (stringCase != null)
                {
                    return(stringCase.TextCase == StringCase.Lowercase ? "L" : "U");
                }
            }

            if (route.IsId() && IsNumber(PrimaryKey.Type(route.RootType)))
            {
                return("D");
            }

            return(FormatString(route.Type));
        }