public void Command_arguments_are_bound_by_name_to_property_setters(
            Type type,
            string commandLine,
            object expectedValue)
        {
            var targetType = typeof(ClassWithSetter <>).MakeGenericType(type);
            var binder     = new TypeBinder(targetType);

            var command = new Command("the-command")
            {
                Argument = new Argument
                {
                    Name         = "value",
                    ArgumentType = type
                }
            };
            var parser = new Parser(command);

            var invocationContext = new InvocationContext(parser.Parse(commandLine), parser);

            var instance = binder.CreateInstance(invocationContext);

            object valueReceivedValue = ((dynamic)instance).Value;

            valueReceivedValue.Should().Be(expectedValue);
        }
Beispiel #2
0
 public PocoToTableEntityConverter()
 {
     _info = PocoTypeBinder.Shared.GetBinderInfo(typeof(TInput));
     ValidateGetter("PartitionKey", PocoTypeBinder.PartitionKeyTypes);
     ValidateGetter("RowKey", PocoTypeBinder.RowKeyTypes);
     ValidateGetter("ETag", PocoTypeBinder.ETagTypes);
     ValidateGetter("Timestamp", PocoTypeBinder.TimestampTypes);
 }
        public void Single_character_constructor_arguments_generate_aliases_with_a_single_dash_prefix()
        {
            var binder = new TypeBinder(typeof(ClassWithSingleLetterCtorParameter));

            var options = binder.BuildOptions().ToArray();

            options.Should().Contain(o => o.HasRawAlias("-x"));
            options.Should().Contain(o => o.HasRawAlias("-y"));
        }
        public void Single_character_setters_generate_aliases_that_accept_a_single_dash_prefix()
        {
            var binder = new TypeBinder(typeof(ClassWithSingleLetterProperty));

            var options = binder.BuildOptions().ToArray();

            options.Should().Contain(o => o.HasRawAlias("-x"));
            options.Should().Contain(o => o.HasRawAlias("-y"));
        }
Beispiel #5
0
        public PocoToTableEntityConverter()
        {
            _info = PocoTypeBinder.Shared.GetBinderInfo(typeof(TInput));

            ConvertsPartitionKey = HasGetter <string>("PartitionKey");
            ConvertsRowKey       = HasGetter <string>("RowKey");
            ConvertsETag         = HasGetter <string>("ETag");
            HasGetter <DateTimeOffset>("Timestamp");
        }
        public void Options_are_not_built_for_infrastructure_types_exposed_by_constructor_parameters(Type type)
        {
            var binder = new TypeBinder(typeof(ClassWithCtorParameter <>).MakeGenericType(type));

            var options = binder.BuildOptions();

            options.Should()
            .NotContain(o => o.Argument.ArgumentType == type);
        }
        public PurrplingCoreProxy(PurrplingCoreMod purrplingCoreMod, Mod forMod)
        {
            this.purrplingCoreMod = purrplingCoreMod;
            this.forMod           = forMod;
            this._typeBinder      = new TypeBinder(forMod.ModManifest, forMod.Monitor);
            this._dataHelper      = new DataHelper(forMod.Helper.Data, this._typeBinder, forMod.Monitor);

            this.Data = this._dataHelper;
        }
        public void Multi_character_setters_generate_aliases_that_accept_a_single_dash_prefix()
        {
            var binder = new TypeBinder(typeof(ClassWithMultiLetterSetters));

            var options = binder.BuildOptions().ToArray();

            options.Should().Contain(o => o.HasRawAlias("--int-option"));
            options.Should().Contain(o => o.HasRawAlias("--string-option"));
            options.Should().Contain(o => o.HasRawAlias("--bool-option"));
        }
        public void When_both_constructor_parameters_and_setters_are_present_then_BuildOptions_creates_options_for_all_of_them()
        {
            var binder = new TypeBinder(typeof(ClassWithSettersAndCtorParametersWithDifferentNames));

            var options = binder.BuildOptions();

            options.Should().Contain(o => o.HasRawAlias("--int-option"));
            options.Should().Contain(o => o.HasRawAlias("--string-option"));
            options.Should().Contain(o => o.HasRawAlias("--bool-option"));

            options.Should().Contain(o => o.HasRawAlias("-i"));
            options.Should().Contain(o => o.HasRawAlias("-s"));
            options.Should().Contain(o => o.HasRawAlias("-b"));
        }
        public static bool TryRefine <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly, Expression, Variable, LogOptions>
            (APC pc, Variable goal, bool tryRefineToMethodEntry,
            IMethodDriver <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly, Expression, Variable, LogOptions> mdriver,
            out BoxedExpression result)
            where Expression : IEquatable <Expression>
            where Variable : IEquatable <Variable>
            where LogOptions : IFrameworkLogOptions
            where Type : IEquatable <Type>
        {
            Contract.Requires(mdriver != null);

            var goalExp = BoxedExpression.Convert(mdriver.ExpressionLayer.Decoder.Context.AssumeNotNull().ExpressionContext.Refine(pc, goal), mdriver.ExpressionDecoder, Int32.MaxValue);

            return(TypeBinder <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly, Expression, Variable, LogOptions> .TryRefineInternal(pc, goalExp, tryRefineToMethodEntry, mdriver, out result));
        }
Beispiel #11
0
            public BoundTypeInfo(Type type, TypeBinder <TExchange> binderImplementation)
            {
                _binderImplementation = binderImplementation;
                Type innerType = type;

                if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    innerType = type.GetGenericArguments()[0];
                }

                _isPrimitive = innerType == typeof(string) ||
                               innerType == typeof(DateTimeOffset) ||
                               innerType == typeof(TimeSpan) ||
                               innerType == typeof(Guid) ||
                               innerType == typeof(decimal) ||
                               innerType.IsPrimitive;

                if (!_isPrimitive)
                {
                    List <BoundMemberInfo> members = new List <BoundMemberInfo>();
                    foreach (var memberInfo in type.GetMembers(BindingFlags.Public | BindingFlags.Instance))
                    {
                        if (memberInfo.IsDefined(typeof(IgnoreDataMemberAttribute)))
                        {
                            continue;
                        }

                        switch (memberInfo)
                        {
                        case PropertyInfo propertyInfo:
                            if (propertyInfo.GetIndexParameters().Length > 0)
                            {
                                continue;
                            }
                            members.Add((BoundMemberInfo)Activator.CreateInstance(typeof(BoundMemberInfo <>).MakeGenericType(typeof(TExchange), propertyInfo.PropertyType), propertyInfo));
                            break;

                        case FieldInfo fieldInfo:
                            members.Add((BoundMemberInfo)Activator.CreateInstance(typeof(BoundMemberInfo <>).MakeGenericType(typeof(TExchange), fieldInfo.FieldType), fieldInfo));
                            break;
                        }
                    }

                    _members = members.ToArray();
                }
            }
        public void Default_option_values_are_based_on_constructor_parameter_defaults()
        {
            var binder = new TypeBinder(typeof(ClassWithMultiLetterCtorParameters));

            var options = binder.BuildOptions().ToArray();

            options.Single(o => o.HasRawAlias("--int-option"))
            .Argument
            .GetDefaultValue()
            .Should()
            .Be(123);

            options.Single(o => o.HasRawAlias("--string-option"))
            .Argument
            .GetDefaultValue()
            .Should()
            .Be("the default");
        }
        public static TypeMapping CurrentType(ControllerContext controllerContext)
        {
            var bindingContext = new ModelBindingContext
            {
                ModelState = controllerContext.Controller.ViewData.ModelState,
                ModelMetadata = controllerContext.Controller.ViewData.ModelMetadata,
                ModelName = TypeBinder.ModeTypeKey,
                ValueProvider = controllerContext.Controller.ValueProvider
            };

            var type = new TypeBinder().BindModel(controllerContext, bindingContext) as Type;
            if (type != null)
            {
                var mapping = FindByType(type);
                if (mapping != null)
                    return mapping;
            }
            return null;
        }
        public void Property_setters_with_no_default_value_and_no_matching_option_are_not_called()
        {
            var command = new Command("the-command");

            var binder = new TypeBinder(typeof(ClassWithSettersAndCtorParametersWithDifferentNames));

            foreach (var option in binder.BuildOptions())
            {
                command.Add(option);
            }

            var parser            = new Parser(command);
            var invocationContext = new InvocationContext(
                parser.Parse(""),
                parser);

            var instance = (ClassWithSettersAndCtorParametersWithDifferentNames)binder.CreateInstance(invocationContext);

            instance.StringOption.Should().Be("the default");
        }
        public void Option_arguments_are_bound_by_name_to_constructor_parameters()
        {
            var argument = new Argument <string>("the default");

            var option = new Option("--string-option",
                                    argument: argument);

            var command = new Command("the-command");

            command.AddOption(option);
            var binder = new TypeBinder(typeof(ClassWithMultiLetterCtorParameters));

            var parser            = new Parser(command);
            var invocationContext = new InvocationContext(
                parser.Parse("--string-option not-the-default"),
                parser);

            var instance = (ClassWithMultiLetterCtorParameters)binder.CreateInstance(invocationContext);

            instance.StringOption.Should().Be("not-the-default");
        }
        public void Explicitly_configured_default_values_can_be_bound_to_property_setters()
        {
            var argument = new Argument <string>("the default");

            var option = new Option("--string-option",
                                    argument: argument);

            var command = new Command("the-command");

            command.AddOption(option);
            var binder = new TypeBinder(typeof(ClassWithMultiLetterSetters));

            var parser            = new Parser(command);
            var invocationContext = new InvocationContext(
                parser.Parse(""),
                parser);

            var instance = (ClassWithMultiLetterSetters)binder.CreateInstance(invocationContext);

            instance.StringOption.Should().Be("the default");
        }
        public void Option_arguments_are_bound_by_name_to_property_setters()
        {
            var argument = new Argument <bool>();

            var option = new Option("--bool-option",
                                    argument: argument);

            var command = new Command("the-command");

            command.AddOption(option);
            var binder = new TypeBinder(typeof(ClassWithMultiLetterSetters));

            var parser            = new Parser(command);
            var invocationContext = new InvocationContext(
                parser.Parse("--bool-option"),
                parser);

            var instance = (ClassWithMultiLetterSetters)binder.CreateInstance(invocationContext);

            instance.BoolOption.Should().BeTrue();
        }
        public static TypeMapping CurrentType(ControllerContext controllerContext)
        {
            var bindingContext = new ModelBindingContext
            {
                ModelState    = controllerContext.Controller.ViewData.ModelState,
                ModelMetadata = controllerContext.Controller.ViewData.ModelMetadata,
                ModelName     = TypeBinder.ModeTypeKey,
                ValueProvider = controllerContext.Controller.ValueProvider
            };

            var type = new TypeBinder().BindModel(controllerContext, bindingContext) as Type;

            if (type != null)
            {
                var mapping = FindByType(type);
                if (mapping != null)
                {
                    return(mapping);
                }
            }
            return(null);
        }
Beispiel #19
0
        public static CommandLineBuilder ConfigureFromType <T>(
            this CommandLineBuilder builder,
            MethodInfo onExecuteMethod = null)
            where T : class
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            var typeBinder = new TypeBinder(typeof(T));

            foreach (var option in typeBinder.BuildOptions())
            {
                builder.AddOption(option);
            }

            builder.Handler = new TypeBindingCommandHandler(
                onExecuteMethod,
                typeBinder);

            return(builder);
        }
Beispiel #20
0
            protected override void DrawPropertyLayout(IPropertyValueEntry <TypeEntry> entry, GUIContent label)
            {
                var value        = entry.SmartValue;
                var valueChanged = false;
                var rect         = EditorGUILayout.GetControlRect();
                var toggleRect   = rect.SetWidth(20);

                rect.xMin += 20;

                // Init
                if (string.IsNullOrEmpty(value.NiceTypeName) && value.Type != null)
                {
                    value.NiceTypeName = value.Type.GetNiceName();
                }

                if (value.IsNew || value.IsCustom || value.Type == null)
                {
                    rect.xMax -= 78;
                }

                // Toggle
                GUIHelper.PushGUIEnabled(value.Type != null);
                valueChanged = value.Emit != (value.Emit = EditorGUI.Toggle(toggleRect, value.Emit));
                GUIHelper.PopGUIEnabled();

                // TextField
                rect.y += 2;
                GUI.SetNextControlName(entry.Property.Path);
                bool isEditing = GUI.GetNameOfFocusedControl() == entry.Property.Path || value.Type == null;

                GUIHelper.PushColor(isEditing ? Color.white : new Color(0, 0, 0, 0));
                var newName = EditorGUI.TextField(rect, value.TypeName, EditorStyles.textField);

                GUIHelper.PopColor();

                // TextField overlay
                if (!isEditing)
                {
                    GUI.Label(rect, value.NiceTypeName);
                }

                if (value.IsNew || value.IsCustom || value.Type == null)
                {
                    rect.xMin  = rect.xMax + 3;
                    rect.width = 75;
                }

                // Labels
                if (value.Type == null)
                {
                    EditorGUI.LabelField(rect, GUIHelper.TempContent("MISSING"), MissingLabelStyle);
                }
                else if (value.IsNew)
                {
                    EditorGUI.LabelField(rect, GUIHelper.TempContent("NEW"), NewLabelStyle);
                }
                else if (value.IsCustom)
                {
                    EditorGUI.LabelField(rect, GUIHelper.TempContent("MODIFIED"), ChangedLabelStyle);
                }

                // Set values
                if ((newName ?? "") != (value.TypeName ?? ""))
                {
                    value.TypeName     = newName;
                    value.IsCustom     = true;
                    value.Type         = TypeBinder.BindToType(value.TypeName);
                    value.NiceTypeName = value.Type == null ? value.TypeName : value.Type.GetNiceName();
                    valueChanged       = true;
                }

                if (valueChanged)
                {
                    value.IsCustom = true;
                    entry.Values.ForceMarkDirty();
                }
            }
            protected override void DrawPropertyLayout(GUIContent label)
            {
                var entry        = this.ValueEntry;
                var value        = entry.SmartValue;
                var valueChanged = false;
                var rect         = EditorGUILayout.GetControlRect();
                var toggleRect   = rect.SetWidth(20);

                rect.xMin += 20;

                if (value.Type == null)
                {
                    this.isEditing = true;
                }

                bool wasEditing = this.isEditing;

                // Init
                if (string.IsNullOrEmpty(value.NiceTypeName) && value.Type != null)
                {
                    value.NiceTypeName = value.Type.GetNiceName();
                }

                // Toggle
                GUIHelper.PushGUIEnabled(value.Type != null);
                valueChanged = value.Emit != (value.Emit = EditorGUI.Toggle(toggleRect, value.Emit));
                GUIHelper.PopGUIEnabled();

                rect.y     += 2;
                rect.width -= 30;

                var textBoxRect = rect;

                if (value.IsNew || value.IsCustom || value.Type == null)
                {
                    textBoxRect.xMax -= 78;
                }

                // Labels
                //if (value.IsNew || value.IsCustom || value.Type == null)
                {
                    var lblRect = rect;

                    lblRect.xMin  = lblRect.xMax - 75;
                    lblRect.width = 75;

                    if (value.Type == null)
                    {
                        EditorGUI.LabelField(lblRect, GUIHelper.TempContent("INVALID"), MissingLabelStyle);
                    }
                    else if (value.IsCustom)
                    {
                        EditorGUI.LabelField(lblRect, GUIHelper.TempContent("MODIFIED"), ChangedLabelStyle);
                    }
                    else if (value.IsNew)
                    {
                        EditorGUI.LabelField(lblRect, GUIHelper.TempContent("NEW"), NewLabelStyle);
                    }
                    else
                    {
                        EditorGUI.LabelField(lblRect, GUIHelper.TempContent(""));
                    }
                }

                var newName = value.TypeName;

                if (this.isEditing)
                {
                    GUI.SetNextControlName(entry.Property.Path);

                    // TextField
                    newName = EditorGUI.TextField(textBoxRect, value.TypeName, EditorStyles.textField);

                    if (GUI.GetNameOfFocusedControl() == entry.Property.Path && (Event.current.Equals(Event.KeyboardEvent("return")) || Event.current.OnKeyUp(KeyCode.Return)))
                    {
                        this.isEditing = false;
                    }
                }
                else
                {
                    // TextField overlay
                    if (GUI.Button(textBoxRect, value.NiceTypeName, EditorStyles.label))
                    {
                        this.isEditing = true;
                    }

                    if (Event.current.type == EventType.Repaint && rect.Contains(Event.current.mousePosition))
                    {
                        EditorIcons.Pen.Draw(rect.AlignRight(30).AddX(30), 16);
                    }
                }

                //GUIHelper.PushColor(new Color(0.2f, 1, 0.2f, 1));
                if (this.isEditing && SirenixEditorGUI.IconButton(rect.AlignRight(30).AddX(30), EditorIcons.Checkmark))
                {
                    this.isEditing = false;
                }
                //GUIHelper.PopColor();

                // Set values
                if ((newName ?? "") != (value.TypeName ?? ""))
                {
                    value.TypeName     = newName;
                    value.IsCustom     = true;
                    value.Type         = GetTypeFromName(value.TypeName);
                    value.NiceTypeName = value.Type == null ? value.TypeName : value.Type.GetNiceName();
                    valueChanged       = true;
                }

                if (wasEditing && !this.isEditing)
                {
                    if (value.Type != null)
                    {
                        value.TypeName = TypeBinder.BindToName(value.Type);
                    }

                    entry.Values.ForceMarkDirty();
                }

                if (valueChanged)
                {
                    value.IsCustom = true;
                    entry.Values.ForceMarkDirty();
                }
            }
        /// <summary>
        /// Registers Unity-specifc data type conversions in the global type binder (UsdIo.Bindings).
        /// </summary>
        static public void RegisterTypes()
        {
            if (isInitialized)
            {
                return;
            }
            isInitialized = true;

            TypeBinder binder = USD.NET.UsdIo.Bindings;

            //
            // Quaternion
            //
            binder.BindType(typeof(Quaternion), new UsdTypeBinding(
                                (object obj) => UnityTypeConverter.QuaternionToQuatf((Quaternion)obj),
                                (pxr.VtValue value) => UnityTypeConverter.QuatfToQuaternion(value),
                                SdfValueTypeNames.Quatf));
            binder.BindArrayType <UnityTypeConverter>(typeof(Quaternion[]), typeof(pxr.VtQuatfArray), SdfValueTypeNames.QuatfArray);
            binder.BindArrayType <UnityTypeConverter>(typeof(List <Quaternion>), typeof(pxr.VtQuatfArray), SdfValueTypeNames.QuatfArray, "List");

            //
            // Scalar Vector{2,3,4}
            //
            binder.BindType(typeof(Vector2), new UsdTypeBinding(
                                (object obj) => UnityTypeConverter.Vector2ToVec2f((Vector2)obj),
                                (pxr.VtValue value) => UnityTypeConverter.Vec2fToVector2(value),
                                SdfValueTypeNames.Float2));
            binder.BindType(typeof(Vector3), new UsdTypeBinding(
                                (object obj) => UnityTypeConverter.Vector3ToVec3f((Vector3)obj),
                                (pxr.VtValue value) => UnityTypeConverter.Vec3fToVector3(value),
                                SdfValueTypeNames.Float3));
            binder.BindType(typeof(Vector4), new UsdTypeBinding(
                                (object obj) => UnityTypeConverter.Vector4ToVec4f((Vector4)obj),
                                (pxr.VtValue value) => UnityTypeConverter.Vec4fToVector4(value),
                                SdfValueTypeNames.Float4));

            //
            // Scaler Rect <-> GfVec4f
            //
            binder.BindType(typeof(Rect), new UsdTypeBinding(
                                (object obj) => UnityTypeConverter.RectToVtVec4((Rect)obj),
                                (pxr.VtValue value) => UnityTypeConverter.Vec4fToRect(value),
                                SdfValueTypeNames.Float4));

            //
            // Vector {2,3,4} {[],List<>}
            //
            binder.BindArrayType <UnityTypeConverter>(typeof(Vector2[]), typeof(pxr.VtVec2fArray), SdfValueTypeNames.Float2Array);
            binder.BindArrayType <UnityTypeConverter>(typeof(List <Vector2>), typeof(pxr.VtVec2fArray), SdfValueTypeNames.Float2Array, "List");

            binder.BindArrayType <UnityTypeConverter>(typeof(Vector3[]), typeof(pxr.VtVec3fArray), SdfValueTypeNames.Float3Array);
            binder.BindArrayType <UnityTypeConverter>(typeof(List <Vector3>), typeof(pxr.VtVec3fArray), SdfValueTypeNames.Float3Array, "List");

            binder.BindArrayType <UnityTypeConverter>(typeof(Vector4[]), typeof(pxr.VtVec4fArray), SdfValueTypeNames.Float4Array);
            binder.BindArrayType <UnityTypeConverter>(typeof(List <Vector4>), typeof(pxr.VtVec4fArray), SdfValueTypeNames.Float4Array, "List");

            //
            // Color / Color32
            //
            binder.BindType(typeof(Color[]), new UsdTypeBinding(
                                (object obj) => UnityTypeConverter.ToVtArray(((Color[])obj)),
                                (pxr.VtValue vtVal) => UnityTypeConverter.ColorFromVtArray(vtVal),
                                SdfValueTypeNames.Color4fArray));

            binder.BindType(typeof(Color), new UsdTypeBinding(
                                (object obj) => UnityTypeConverter.ColorToVec4f(((Color)obj)),
                                (pxr.VtValue vtVal) => UnityTypeConverter.Vec4fToColor(vtVal),
                                SdfValueTypeNames.Color4f));

            binder.BindType(typeof(Color32), new UsdTypeBinding(
                                (object obj) => UnityTypeConverter.Color32ToVec4f(((Color32)obj)),
                                (pxr.VtValue vtVal) => UnityTypeConverter.Vec4fToColor32(vtVal),
                                SdfValueTypeNames.Color4f));

            binder.BindType(typeof(Bounds), new UsdTypeBinding(
                                (object obj) => UnityTypeConverter.BoundsToVtArray(((Bounds)obj)),
                                (pxr.VtValue vtVal) => UnityTypeConverter.BoundsFromVtArray(vtVal),
                                SdfValueTypeNames.Float3Array));

            binder.BindArrayType <UnityTypeConverter>(typeof(List <Color>), typeof(pxr.VtVec4fArray), SdfValueTypeNames.Color4fArray);
            binder.BindArrayType <UnityTypeConverter>(typeof(Color32[]), typeof(pxr.VtVec4fArray), SdfValueTypeNames.Color4fArray);

            //
            // Matrix4x4
            //

            // Note that UsdGeom exclusively uses double-precision matrices for storage.
            // In the future, it may be nice to support single-precision for clients who aren't targeting
            // UsdGeom.
            binder.BindType(typeof(Matrix4x4), new UsdTypeBinding(
                                (object obj) => UnityTypeConverter.ToGfMatrix(((Matrix4x4)obj)),
                                (pxr.VtValue vtVal) => UnityTypeConverter.FromMatrix((pxr.GfMatrix4d)vtVal),
                                SdfValueTypeNames.Matrix4d));

            binder.BindArrayType <UnityTypeConverter>(typeof(Matrix4x4[]), typeof(pxr.VtMatrix4dArray), SdfValueTypeNames.Matrix4dArray);
            binder.BindArrayType <UnityTypeConverter>(typeof(List <Matrix4x4>), typeof(pxr.VtMatrix4dArray), SdfValueTypeNames.Matrix4dArray, "List");
        }
Beispiel #23
0
 public abstract void Serialize(object o, TExchange destination, TypeBinder <TExchange> binderImplementation);
Beispiel #24
0
 public abstract void Deserialize(TExchange source, object o, TypeBinder <TExchange> binderImplementation);