BadScript 2
Loading...
Searching...
No Matches
ValueMapper.cs
Go to the documentation of this file.
1// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
2
3using System;
4using System.Collections.Generic;
5using System.Linq;
6
8
9using CSharpx;
10
12
13namespace CommandLine.Core
14{
15 internal static class ValueMapper
16 {
17 public static Result<
18 IEnumerable<SpecificationProperty>, Error>
19 MapValues(IEnumerable<SpecificationProperty> specProps,
20 IEnumerable<string> values,
21 Func<IEnumerable<string>, Type, bool, Maybe<object>> converter)
22 {
23 IEnumerable<Tuple<SpecificationProperty, Maybe<Error>>> propAndErrors =
24 MapValuesImpl(specProps, values, converter);
25
26 return Result.Succeed(propAndErrors.Select(pe => pe.Item1),
27 propAndErrors.Select(pe => pe.Item2)
28 .OfType<Just<Error>>()
29 .Select(e => e.Value)
30 );
31 }
32
33 private static IEnumerable<Tuple<SpecificationProperty, Maybe<Error>>> MapValuesImpl(
34 IEnumerable<SpecificationProperty> specProps,
35 IEnumerable<string> values,
36 Func<IEnumerable<string>, Type, bool, Maybe<object>> converter)
37 {
38 if (specProps.Empty())
39 {
40 yield break;
41 }
42
43 SpecificationProperty pt = specProps.First();
44
45 IEnumerable<string> taken = values.Take(pt.Specification.CountOfMaxNumberOfValues()
46 .GetValueOrDefault(values.Count())
47 );
48
49 if (taken.Empty())
50 {
51 yield return
52 Tuple.Create(pt, pt.Specification.MakeErrorInCaseOfMinConstraint());
53
54 yield break;
55 }
56
57 Maybe<SpecificationProperty> next = specProps.Skip(1)
58 .FirstOrDefault(s => s.Specification.IsValue())
59 .ToMaybe();
60
61 if (pt.Specification.Max.IsJust() &&
62 next.IsNothing() &&
63 values.Skip(taken.Count())
64 .Any())
65 {
66 yield return
67 Tuple.Create<SpecificationProperty, Maybe<Error>>(pt,
68 Maybe
69 .Just<
70 Error>(new
72 .EmptyName
73 )
74 )
75 );
76
77 yield break;
78 }
79
80 yield return
81 converter(taken, pt.Property.PropertyType, pt.Specification.TargetType != TargetType.Sequence)
82 .MapValueOrDefault(converted =>
83 Tuple.Create(pt.WithValue(Maybe.Just(converted)), Maybe.Nothing<Error>()),
84 Tuple.Create<SpecificationProperty, Maybe<Error>>(pt,
86 )
87 );
88
89 foreach (Tuple<SpecificationProperty, Maybe<Error>> value in MapValuesImpl(specProps.Skip(1),
90 values.Skip(taken.Count()),
91 converter
92 ))
93 {
94 yield return value;
95 }
96 }
97
98 private static Maybe<int> CountOfMaxNumberOfValues(this Specification specification)
99 {
100 switch (specification.TargetType)
101 {
102 case TargetType.Scalar:
103 return Maybe.Just(1);
104 case TargetType.Sequence:
105 if (specification.Max.IsJust())
106 {
107 return Maybe.Just(specification.Max.FromJustOrFail());
108 }
109
110 break;
111 }
112
113 return Maybe.Nothing<int>();
114 }
115
117 {
118 return specification.Min.IsJust()
120 : Maybe.Nothing<Error>();
121 }
122 }
123}
Models a CSharpx.Maybe when contains a value.
Definition Maybe.cs:88
The Maybe type models an optional value. A value of type Maybe a either contains a value of type a (r...
Definition Maybe.cs:33
Models an error generated when a value conversion fails.
Definition Error.cs:418
static SpecificationProperty Create(Specification specification, PropertyInfo property, Maybe< object > value)
static IEnumerable< Tuple< SpecificationProperty, Maybe< Error > > > MapValuesImpl(IEnumerable< SpecificationProperty > specProps, IEnumerable< string > values, Func< IEnumerable< string >, Type, bool, Maybe< object > > converter)
static Maybe< Error > MakeErrorInCaseOfMinConstraint(this Specification specification)
static Maybe< int > CountOfMaxNumberOfValues(this Specification specification)
static Result< IEnumerable< SpecificationProperty >, Error > MapValues(IEnumerable< SpecificationProperty > specProps, IEnumerable< string > values, Func< IEnumerable< string >, Type, bool, Maybe< object > > converter)
Base type of all errors.
Definition Error.cs:110
Models name information, used in CommandLine.Error instances.
Definition NameInfo.cs:11
static readonly NameInfo EmptyName
Represents an empty name information. Used when CommandLine.Error are tied to values,...
Definition NameInfo.cs:16
Models an error generated when a sequence value lacks elements.
Definition Error.cs:427
Represents the result of a computation.