private static ChangeType ( IEnumerable |
||
values | IEnumerable |
|
conversionType | System | |
conversionCulture | ||
return | Maybe |
public static ParserResult <T> Build <T>( Maybe <Func <T> > factory, Func <IEnumerable <string>, IEnumerable <OptionSpecification>, Result <IEnumerable <Token>, Error> > tokenizer, IEnumerable <string> arguments, StringComparer nameComparer, bool ignoreValueCase, CultureInfo parsingCulture, bool autoHelp, bool autoVersion, IEnumerable <ErrorType> nonFatalErrors) { var typeInfo = factory.MapValueOrDefault(f => f().GetType(), typeof(T)); var specProps = typeInfo.GetSpecifications(pi => SpecificationProperty.Create( Specification.FromProperty(pi), pi, Maybe.Nothing <object>())) .Memorize(); var specs = from pt in specProps select pt.Specification; var optionSpecs = specs .ThrowingValidate(SpecificationGuards.Lookup) .OfType <OptionSpecification>() .Memorize(); Func <T> makeDefault = () => typeof(T).IsMutable() ? factory.MapValueOrDefault(f => f(), Activator.CreateInstance <T>()) : ReflectionHelper.CreateDefaultImmutableInstance <T>( (from p in specProps select p.Specification.ConversionType).ToArray()); Func <IEnumerable <Error>, ParserResult <T> > notParsed = errs => new NotParsed <T>(makeDefault().GetType().ToTypeInfo(), errs); var argumentsList = arguments.Memorize(); Func <ParserResult <T> > buildUp = () => { var tokenizerResult = tokenizer(argumentsList, optionSpecs); var tokens = tokenizerResult.SucceededWith().Memorize(); var partitions = TokenPartitioner.Partition( tokens, name => TypeLookup.FindTypeDescriptorAndSibling(name, optionSpecs, nameComparer)); var optionsPartition = partitions.Item1.Memorize(); var valuesPartition = partitions.Item2.Memorize(); var errorsPartition = partitions.Item3.Memorize(); var optionSpecPropsResult = OptionMapper.MapValues( (from pt in specProps where pt.Specification.IsOption() select pt), optionsPartition, (vals, type, isScalar) => TypeConverter.ChangeType(vals, type, isScalar, parsingCulture, ignoreValueCase), nameComparer); var valueSpecPropsResult = ValueMapper.MapValues( (from pt in specProps where pt.Specification.IsValue() orderby((ValueSpecification)pt.Specification).Index select pt), valuesPartition, (vals, type, isScalar) => TypeConverter.ChangeType(vals, type, isScalar, parsingCulture, ignoreValueCase)); var missingValueErrors = from token in errorsPartition select new MissingValueOptionError( optionSpecs.Single(o => token.Text.MatchName(o.ShortName, o.LongName, nameComparer)) .FromOptionSpecification()); var specPropsWithValue = optionSpecPropsResult.SucceededWith().Concat(valueSpecPropsResult.SucceededWith()).Memorize(); var setPropertyErrors = new List <Error>(); //build the instance, determining if the type is mutable or not. T instance; if (typeInfo.IsMutable() == true) { instance = BuildMutable(factory, specPropsWithValue, setPropertyErrors); } else { instance = BuildImmutable(typeInfo, factory, specProps, specPropsWithValue, setPropertyErrors); } var validationErrors = specPropsWithValue.Validate(SpecificationPropertyRules.Lookup(tokens)); var allErrors = tokenizerResult.SuccessfulMessages() .Concat(missingValueErrors) .Concat(optionSpecPropsResult.SuccessfulMessages()) .Concat(valueSpecPropsResult.SuccessfulMessages()) .Concat(validationErrors) .Concat(setPropertyErrors) .Memorize(); var warnings = from e in allErrors where nonFatalErrors.Contains(e.Tag) select e; return(allErrors.Except(warnings).ToParserResult(instance)); }; var preprocessorErrors = ( argumentsList.Any() ? arguments.Preprocess(PreprocessorGuards.Lookup(nameComparer, autoHelp, autoVersion)) : Enumerable.Empty <Error>() ).Memorize(); var result = argumentsList.Any() ? preprocessorErrors.Any() ? notParsed(preprocessorErrors) : buildUp() : buildUp(); return(result); }
public static ParserResult <T> Build <T>( Maybe <Func <T> > factory, Func <IEnumerable <string>, IEnumerable <OptionSpecification>, Result <IEnumerable <Token>, Error> > tokenizer, IEnumerable <string> arguments, StringComparer nameComparer, CultureInfo parsingCulture, IEnumerable <ErrorType> nonFatalErrors) { var typeInfo = factory.MapValueOrDefault(f => f().GetType(), typeof(T)); var specProps = typeInfo.GetSpecifications(pi => SpecificationProperty.Create( Specification.FromProperty(pi), pi, Maybe.Nothing <object>())); var specs = from pt in specProps select pt.Specification; var optionSpecs = specs .ThrowingValidate(SpecificationGuards.Lookup) .OfType <OptionSpecification>(); Func <T> makeDefault = () => typeof(T).IsMutable() ? factory.MapValueOrDefault(f => f(), Activator.CreateInstance <T>()) : ReflectionHelper.CreateDefaultImmutableInstance <T>( (from p in specProps select p.Specification.ConversionType).ToArray()); Func <IEnumerable <Error>, ParserResult <T> > notParsed = errs => new NotParsed <T>(makeDefault().GetType().ToTypeInfo(), errs); Func <ParserResult <T> > buildUp = () => { var tokenizerResult = tokenizer(arguments, optionSpecs); var tokens = tokenizerResult.SucceededWith(); var partitions = TokenPartitioner.Partition( tokens, name => TypeLookup.FindTypeDescriptorAndSibling(name, optionSpecs, nameComparer)); var optionsPartition = partitions.Item1; var valuesPartition = partitions.Item2; var errorsPartition = partitions.Item3; var optionSpecPropsResult = OptionMapper.MapValues( (from pt in specProps where pt.Specification.IsOption() select pt), optionsPartition, (vals, type, isScalar) => TypeConverter.ChangeType(vals, type, isScalar, parsingCulture), nameComparer); var valueSpecPropsResult = ValueMapper.MapValues( (from pt in specProps where pt.Specification.IsValue() select pt), valuesPartition, (vals, type, isScalar) => TypeConverter.ChangeType(vals, type, isScalar, parsingCulture)); var missingValueErrors = from token in errorsPartition select new MissingValueOptionError( optionSpecs.Single(o => token.Text.MatchName(o.ShortName, o.LongName, nameComparer)) .FromOptionSpecification()); var specPropsWithValue = optionSpecPropsResult.SucceededWith().Concat(valueSpecPropsResult.SucceededWith()); Func <T> buildMutable = () => { var mutable = factory.MapValueOrDefault(f => f(), Activator.CreateInstance <T>()); mutable = mutable.SetProperties(specPropsWithValue, sp => sp.Value.IsJust(), sp => sp.Value.FromJustOrFail()) .SetProperties( specPropsWithValue, sp => sp.Value.IsNothing() && sp.Specification.DefaultValue.IsJust(), sp => sp.Specification.DefaultValue.FromJustOrFail()) .SetProperties( specPropsWithValue, sp => sp.Value.IsNothing() && sp.Specification.TargetType == TargetType.Sequence && sp.Specification.DefaultValue.MatchNothing(), sp => sp.Property.PropertyType.GetGenericArguments().Single().CreateEmptyArray()); return(mutable); }; Func <T> buildImmutable = () => { var ctor = typeInfo.GetConstructor((from sp in specProps select sp.Property.PropertyType).ToArray()); var values = (from prms in ctor.GetParameters() join sp in specPropsWithValue on prms.Name.ToLower() equals sp.Property.Name.ToLower() select sp.Value.MapValueOrDefault( v => v, sp.Specification.DefaultValue.MapValueOrDefault( d => d, sp.Specification.ConversionType.CreateDefaultForImmutable()))).ToArray(); var immutable = (T)ctor.Invoke(values); return(immutable); }; var instance = typeInfo.IsMutable() ? buildMutable() : buildImmutable(); var validationErrors = specPropsWithValue.Validate(SpecificationPropertyRules.Lookup(tokens)); var allErrors = tokenizerResult.SuccessfulMessages() .Concat(missingValueErrors) .Concat(optionSpecPropsResult.SuccessfulMessages()) .Concat(valueSpecPropsResult.SuccessfulMessages()) .Concat(validationErrors) .Memorize(); var warnings = from e in allErrors where nonFatalErrors.Contains(e.Tag) select e; return(allErrors.Except(warnings).ToParserResult(instance)); }; var preprocessorErrors = arguments.Any() ? arguments.Preprocess(PreprocessorGuards.Lookup(nameComparer)) : Enumerable.Empty <Error>(); var result = arguments.Any() ? preprocessorErrors.Any() ? notParsed(preprocessorErrors) : buildUp() : buildUp(); return(result); }
public static ParserResult <T> Build <T>( Maybe <Func <T> > factory, Func <IEnumerable <string>, IEnumerable <OptionSpecification>, StatePair <IEnumerable <Token> > > tokenizer, IEnumerable <string> arguments, StringComparer nameComparer, CultureInfo parsingCulture) { var typeInfo = factory.Return(f => f().GetType(), typeof(T)); var specProps = typeInfo.GetSpecifications(pi => SpecificationProperty.Create( Specification.FromProperty(pi), pi, Maybe.Nothing <object>())); var specs = from pt in specProps select pt.Specification; var optionSpecs = specs .ThrowingValidate(SpecificationGuards.Lookup) .OfType <OptionSpecification>(); Func <T> makeDefault = () => typeof(T).IsMutable() ? factory.Return(f => f(), Activator.CreateInstance <T>()) : ReflectionHelper.CreateDefaultImmutableInstance <T>( (from p in specProps select p.Specification.ConversionType).ToArray()); if (arguments.Any()) { var preprocessorErrors = arguments.Preprocess(PreprocessorGuards.Lookup(nameComparer)); if (preprocessorErrors.Any()) { return(new NotParsed <T>(makeDefault(), preprocessorErrors)); } } var tokenizerResult = tokenizer(arguments, optionSpecs); var tokens = tokenizerResult.Value; var partitions = TokenPartitioner.Partition( tokens, name => TypeLookup.FindTypeDescriptorAndSibling(name, optionSpecs, nameComparer)); var optionSpecProps = OptionMapper.MapValues( (from pt in specProps where pt.Specification.IsOption() select pt), partitions.Options, (vals, type, isScalar) => TypeConverter.ChangeType(vals, type, isScalar, parsingCulture), nameComparer); var valueSpecProps = ValueMapper.MapValues( (from pt in specProps where pt.Specification.IsValue() select pt), partitions.Values, (vals, type, isScalar) => TypeConverter.ChangeType(vals, type, isScalar, parsingCulture)); var missingValueErrors = from token in partitions.Errors select new MissingValueOptionError( optionSpecs.Single(o => token.Text.MatchName(o.ShortName, o.LongName, nameComparer)).FromOptionSpecification()); var specPropsWithValue = optionSpecProps.Value.Concat(valueSpecProps.Value); T instance; if (typeInfo.IsMutable()) { instance = factory.Return(f => f(), Activator.CreateInstance <T>()); instance = instance .SetProperties(specPropsWithValue, sp => sp.Value.IsJust(), sp => sp.Value.FromJust()) .SetProperties(specPropsWithValue, sp => sp.Value.IsNothing() && sp.Specification.DefaultValue.IsJust(), sp => sp.Specification.DefaultValue.FromJust()) .SetProperties(specPropsWithValue, sp => sp.Value.IsNothing() && sp.Specification.TargetType == TargetType.Sequence && sp.Specification.DefaultValue.MatchNothing(), sp => sp.Property.PropertyType.GetGenericArguments().Single().CreateEmptyArray()); } else { var ctor = typeInfo.GetConstructor((from sp in specProps select sp.Property.PropertyType).ToArray()); var values = (from prms in ctor.GetParameters() join sp in specPropsWithValue on prms.Name.ToLower() equals sp.Property.Name.ToLower() select sp.Value.Return(v => v, sp.Specification.DefaultValue.Return(d => d, sp.Specification.ConversionType.CreateDefaultForImmutable()))).ToArray(); instance = (T)ctor.Invoke(values); } var validationErrors = specPropsWithValue.Validate( SpecificationPropertyRules.Lookup(tokens)); var allErrors = tokenizerResult.Errors.Concat(missingValueErrors) .Concat(optionSpecProps.Errors) .Concat(valueSpecProps.Errors) .Concat(validationErrors); if (allErrors.Any()) { return(new NotParsed <T>(instance, allErrors)); } return(new Parsed <T>(instance)); }