FormFlow/Extensions.cs
1 //
2 // Copyright (c) Microsoft. All rights reserved.
3 // Licensed under the MIT license.
4 //
5 // Microsoft Bot Framework: http://botframework.com
6 //
7 // Bot Builder SDK GitHub:
8 // https://github.com/Microsoft/BotBuilder
9 //
10 // Copyright (c) Microsoft Corporation
11 // All rights reserved.
12 //
13 // MIT License:
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 //
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 //
25 // THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33 
34 using System;
35 using System.Collections.Generic;
36 using System.Linq;
37 
38 namespace Microsoft.Bot.Builder.FormFlow.Advanced
39 {
40  public static partial class Extensions
41  {
42  internal static IStep<T> Step<T>(this IForm<T> form, string name) where T : class
43  {
44  IStep<T> result = null;
45  foreach (var step in form.Steps)
46  {
47  if (step.Name == name)
48  {
49  result = step;
50  break;
51  }
52  }
53  return result;
54  }
55 
56  internal static int StepIndex<T>(this IForm<T> form, IStep<T> step) where T : class
57  {
58  var index = -1;
59  for (var i = 0; i < form.Steps.Count; ++i)
60  {
61  if (form.Steps[i] == step)
62  {
63  index = i;
64  break;
65  }
66  }
67  return index;
68  }
69 
70  internal static IField<T> BuildCommandRecognizer<T>(this IForm<T> form) where T : class
71  {
72  var field = new Field<T>("__commands__", FieldRole.Value);
73  field.SetPrompt(new PromptAttribute(""));
74  foreach (var entry in form.Configuration.Commands)
75  {
76  field.AddDescription(entry.Key, entry.Value.Description);
77  field.AddTerms(entry.Key, entry.Value.Terms);
78  }
79  foreach (var nav in form.Fields)
80  {
81  var fterms = nav.FieldTerms;
82  if (fterms != null)
83  {
84  field.AddDescription(nav.Name, nav.FieldDescription);
85  field.AddTerms(nav.Name, fterms.ToArray());
86  }
87  }
88  field.Form = form;
89  return field;
90  }
91 
92  internal static IEnumerable<string> Dependencies<T>(this IForm<T> form, int istep)
93  where T : class
94  {
95  for (var i = 0; i < istep; ++i)
96  {
97  if (form.Steps[i].Type == StepType.Field)
98  {
99  yield return form.Steps[i].Name;
100  }
101  }
102  }
103 
109  public static bool IsICollection(this Type type)
110  {
111  return Array.Exists(type.GetInterfaces(), IsGenericCollectionType);
112  }
113 
119  public static bool IsIEnumerable(this Type type)
120  {
121  return Array.Exists(type.GetInterfaces(), IsGenericEnumerableType);
122  }
123 
129  public static bool IsIList(this Type type)
130  {
131  return Array.Exists(type.GetInterfaces(), IsListCollectionType);
132  }
133 
139  public static bool IsGenericCollectionType(this Type type)
140  {
141  return type.IsGenericType && (typeof(ICollection<>) == type.GetGenericTypeDefinition());
142  }
143 
149  public static bool IsGenericEnumerableType(this Type type)
150  {
151  return type.IsGenericType && (typeof(IEnumerable<>) == type.GetGenericTypeDefinition());
152  }
153 
159  public static bool IsIntegral(this Type type)
160  {
161  return (type == typeof(sbyte) ||
162  type == typeof(byte) ||
163  type == typeof(short) ||
164  type == typeof(ushort) ||
165  type == typeof(int) ||
166  type == typeof(uint) ||
167  type == typeof(long) ||
168  type == typeof(ulong));
169  }
170 
176  public static bool IsDouble(this Type type)
177  {
178  return type == typeof(float) || type == typeof(double);
179  }
180 
186  public static bool IsListCollectionType(this Type type)
187  {
188  return type.IsGenericType && (typeof(IList<>) == type.GetGenericTypeDefinition());
189  }
190 
196  public static bool IsNullable(this Type type)
197  {
198  return (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>));
199  }
200 
206  public static Type GetGenericElementType(this Type type)
207  {
208  return (from i in type.GetInterfaces()
209  where i.IsGenericType && typeof(IEnumerable<>) == i.GetGenericTypeDefinition()
210  select i.GetGenericArguments()[0]).FirstOrDefault();
211  }
212  }
213 }
FieldRole
The role the field plays in a form.
Definition: IField.cs:129
static Type GetGenericElementType(this Type type)
Return the underlying type of generic IEnumerable.
Define the prompt used when asking about a field.
Definition: Attributes.cs:294
static bool IsNullable(this Type type)
Type is nullable.
static bool IsIntegral(this Type type)
Type is integral.
static bool IsIEnumerable(this Type type)
Type implements IEnumerable.
static bool IsGenericCollectionType(this Type type)
Type implements generic ICollection.
Interface for all the information about a specific field.
Definition: IField.cs:404
Field< T > SetPrompt(PromptAttribute prompt)
Sets the field prompt.
Definition: Field.cs:546
static bool IsICollection(this Type type)
Type implements ICollection.
Form definition interface.
Definition: IForm.cs:47
static bool IsGenericEnumerableType(this Type type)
Type implements generic IEnumerable.
Base class with declarative implementation of IField.
Definition: Field.cs:67
static bool IsDouble(this Type type)
Type is float or double.
static bool IsListCollectionType(this Type type)
Type implements generic IList.
static bool IsIList(this Type type)
Type implements IList.