/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ namespace Microsoft.Rest.ClientRuntime.PowerShell { using static Microsoft.Rest.ClientRuntime.Extensions; [System.ComponentModel.TypeConverter(typeof(AsyncOperationResponseTypeConverter))] public class AsyncOperationResponse { private string _target; public string Target { get => _target; set => _target = value; } public AsyncOperationResponse() { } internal AsyncOperationResponse(Carbon.Json.JsonObject json) { // pull target { Target = If(json?.PropertyT("target"), out var _v) ? (string)_v : (string)Target; } } public string ToJsonString() { return $"{{ \"target\" : \"{this.Target}\" }}"; } public static AsyncOperationResponse FromJson(Carbon.Json.JsonNode node) { return node is Carbon.Json.JsonObject json ? new AsyncOperationResponse(json) : null; } /// /// Creates a new instance of , deserializing the content from a json string. /// /// a string containing a JSON serialized instance of this model. /// an instance of the model class. public static AsyncOperationResponse FromJsonString(string jsonText) => FromJson(Carbon.Json.JsonNode.Parse(jsonText)); } public partial class AsyncOperationResponseTypeConverter : System.Management.Automation.PSTypeConverter { /// /// Determines if the converter can convert the parameter to the /// parameter. /// /// the to convert from /// the to convert to /// /// true if the converter can convert the parameter to the /// parameter, otherwise false. /// public override bool CanConvertFrom(object sourceValue, global::System.Type destinationType) => CanConvertFrom(sourceValue); /// /// Determines if the converter can convert the parameter to a type /// parameter. /// /// the instance to check if it can be converted to the type. /// /// true if the instance could be converted to a type, otherwise false /// public static bool CanConvertFrom(dynamic sourceValue) { if (null == sourceValue) { return true; } global::System.Type type = sourceValue.GetType(); if (typeof(System.Management.Automation.PSObject).IsAssignableFrom(type)) { // we say yest to PSObjects return true; } if (typeof(global::System.Collections.IDictionary).IsAssignableFrom(type)) { // we say yest to Hashtables/dictionaries return true; } try { if (null != sourceValue.ToJsonString()) { return true; } } catch { // Not one of our objects } try { string text = sourceValue.ToString()?.Trim(); return true == text?.StartsWith("{") && true == text?.EndsWith("}") && Carbon.Json.JsonNode.Parse(text).Type == Microsoft.Rest.ClientRuntime.Json.JsonType.Object; } catch { // Doesn't look like it can be treated as JSON } return false; } /// /// Determines if the parameter can be converted to the parameter /// /// the to convert from /// the to convert to /// /// true if the converter can convert the parameter to the /// parameter, otherwise false /// public override bool CanConvertTo(object sourceValue, global::System.Type destinationType) => false; /// /// Converts the parameter to the parameter using and /// /// the to convert from /// the to convert to /// not used by this TypeConverter. /// when set to true, will ignore the case when converting. /// /// an instance of , or null if there is no suitable conversion. /// public override object ConvertFrom(object sourceValue, global::System.Type destinationType, global::System.IFormatProvider formatProvider, bool ignoreCase) => ConvertFrom(sourceValue); /// /// Converts the parameter into an instance of /// /// the value to convert into an instance of . /// /// an instance of , or null if there is no suitable conversion. /// public static object ConvertFrom(dynamic sourceValue) { if (null == sourceValue) { return null; } global::System.Type type = sourceValue.GetType(); if (typeof(AsyncOperationResponse).IsAssignableFrom(type)) { return sourceValue; } try { return AsyncOperationResponse.FromJsonString(typeof(string) == sourceValue.GetType() ? sourceValue : sourceValue.ToJsonString()); ; } catch { // Unable to use JSON pattern } if (typeof(System.Management.Automation.PSObject).IsAssignableFrom(type)) { return new AsyncOperationResponse { Target = (sourceValue as System.Management.Automation.PSObject).GetValueForProperty("target", "", global::System.Convert.ToString) }; } if (typeof(global::System.Collections.IDictionary).IsAssignableFrom(type)) { return new AsyncOperationResponse { Target = (sourceValue as global::System.Collections.IDictionary).GetValueForProperty("target", "", global::System.Convert.ToString) }; } return null; } /// NotImplemented -- this will return null /// the to convert from /// the to convert to /// not used by this TypeConverter. /// when set to true, will ignore the case when converting. /// will always return null. public override object ConvertTo(object sourceValue, global::System.Type destinationType, global::System.IFormatProvider formatProvider, bool ignoreCase) => null; } }