示例#1
0
        public IValueNode ParseResult(object?resultValue)
        {
            if (resultValue is null)
            {
                return(NullValueNode.Default);
            }

            if (resultValue is IReadOnlyDictionary <string, object> dict)
            {
                var list = new List <ObjectFieldNode>();

                foreach (InputField field in Fields)
                {
                    if (dict.TryGetValue(field.Name.Value, out object?value))
                    {
                        list.Add(new ObjectFieldNode(
                                     field.Name.Value,
                                     field.Type.ParseResult(value)));
                    }
                }

                return(new ObjectValueNode(list));
            }

            if (RuntimeType != typeof(object) && RuntimeType.IsInstanceOfType(resultValue))
            {
                return(ParseValue(resultValue));
            }

            throw new SerializationException(
                      TypeResourceHelper.Scalar_Cannot_ParseResult(Name, resultValue.GetType()),
                      this);
        }
示例#2
0
        public IValueNode ParseResult(object?resultValue)
        {
            if (resultValue is null)
            {
                return(NullValueNode.Default);
            }

            if (resultValue is string s &&
                _enumValues.TryGetValue(s, out IEnumValue? enumValue))
            {
                return(new EnumValueNode(enumValue.Name));
            }

            if (resultValue is NameString n &&
                _enumValues.TryGetValue(n, out enumValue))
            {
                return(new EnumValueNode(enumValue.Name));
            }

            if (_valueLookup.TryGetValue(resultValue, out enumValue))
            {
                return(new EnumValueNode(enumValue.Name));
            }

            throw new SerializationException(
                      TypeResourceHelper.Scalar_Cannot_ParseResult(Name, resultValue.GetType()),
                      this);
        }
示例#3
0
        public object Serialize(object value)
        {
            if (value == null)
            {
                return(null);
            }

            if (ClrType.IsInstanceOfType(value) &&
                _valueToValues.TryGetValue(value, out EnumValue enumValue))
            {
                return(enumValue.Name);
            }

            // schema first unbound enum type
            if (ClrType == typeof(object) &&
                _nameToValues.TryGetValue(
                    value.ToString().ToUpperInvariant(),
                    out enumValue))
            {
                return(enumValue.Name);
            }

            throw new ArgumentException(
                      TypeResourceHelper.Scalar_Cannot_Serialize(Name));
        }
示例#4
0
        public override object ParseLiteral(IValueNode literal, bool withDefaults = true)
        {
            switch (literal)
            {
            case StringValueNode svn:
                return(svn.Value);

            case IntValueNode ivn:
                return(long.Parse(ivn.Value, CultureInfo.InvariantCulture));

            case FloatValueNode fvn:
                return(decimal.Parse(fvn.Value, CultureInfo.InvariantCulture));

            case BooleanValueNode bvn:
                return(bvn.Value);

            case ListValueNode lvn:
                return(_objectValueToDictConverter.Convert(lvn));

            case ObjectValueNode ovn:
                return(_objectValueToDictConverter.Convert(ovn));

            case NullValueNode _:
                return(null);

            default:
                throw new SerializationException(
                          TypeResourceHelper.Scalar_Cannot_ParseLiteral(Name, literal.GetType()),
                          this);
            }
        }
示例#5
0
        protected sealed override object?ParseLiteral(IValueNode valueSyntax, bool withDefaults)
        {
            if (valueSyntax is NullValueNode)
            {
                return(null);
            }

            if (valueSyntax.Kind != SyntaxKind.ListValue &&
                InnerInputType !.IsInstanceOfType(valueSyntax))
            {
                return(CreateList(new ListValueNode(valueSyntax), withDefaults));
            }

            if (valueSyntax.Kind == SyntaxKind.ListValue)
            {
                if (_isNestedList)
                {
                    if (IsInstanceOfType(valueSyntax))
                    {
                        return(CreateList((ListValueNode)valueSyntax, withDefaults));
                    }
                }
                else
                {
                    return(CreateList((ListValueNode)valueSyntax, withDefaults));
                }
            }

            throw new SerializationException(
                      TypeResourceHelper.Scalar_Cannot_ParseLiteral(this.Print(), valueSyntax.GetType()),
                      this);
        }
示例#6
0
        public override object ParseLiteral(IValueNode literal)
        {
            if (literal == null)
            {
                throw new ArgumentNullException(nameof(literal));
            }

            if (literal is NullValueNode)
            {
                return(null);
            }

            if (literal is FloatValueNode floatLiteral &&
                TryParseDouble(floatLiteral.Value, out double d))
            {
                return(d);
            }

            // Input coercion rules specify that float values can be coerced
            // from IntValueNode and FloatValueNode:
            // http://facebook.github.io/graphql/June2018/#sec-Float

            if (literal is IntValueNode intLiteral &&
                TryParseDouble(intLiteral.Value, out d))
            {
                return(d);
            }

            throw new ScalarSerializationException(
                      TypeResourceHelper.Scalar_Cannot_ParseLiteral(
                          Name, literal.GetType()));
        }
示例#7
0
        public override object ParseLiteral(IValueNode literal)
        {
            if (literal == null)
            {
                throw new ArgumentNullException(nameof(literal));
            }

            if (literal is StringValueNode stringLiteral)
            {
                return(stringLiteral.Value);
            }

            if (literal is IntValueNode intLiteral)
            {
                return(intLiteral.Value);
            }

            if (literal is NullValueNode)
            {
                return(null);
            }

            throw new ScalarSerializationException(
                      TypeResourceHelper.Scalar_Cannot_ParseLiteral(
                          Name, literal.GetType()));
        }
        public override object ParseLiteral(IValueNode literal)
        {
            if (literal == null)
            {
                throw new ArgumentNullException(nameof(literal));
            }

            if (literal is StringValueNode stringLiteral)
            {
                if (!MultiplierPathString.IsValidName(stringLiteral.Value))
                {
                    throw new ScalarSerializationException(
                              string.Format(CultureInfo.InvariantCulture,
                                            AbstractionResources.Type_NameIsNotValid,
                                            stringLiteral.Value ?? "null"));
                }
                return(new MultiplierPathString(stringLiteral.Value));
            }

            if (literal is NullValueNode)
            {
                return(null);
            }

            throw new ScalarSerializationException(
                      TypeResourceHelper.Scalar_Cannot_ParseLiteral(
                          Name, literal.GetType()));
        }
示例#9
0
    public override IValueNode ParseResult(object?resultValue)
    {
        if (resultValue is null)
        {
            return(NullValueNode.Default);
        }

        if (resultValue is string s)
        {
            return(new StringValueNode(s));
        }

        if (resultValue is DateTimeOffset d)
        {
            return(ParseValue(d));
        }

        if (resultValue is DateTime dt)
        {
            return(ParseValue(new DateTimeOffset(dt.ToUniversalTime(), TimeSpan.Zero)));
        }

        throw new SerializationException(
                  TypeResourceHelper.Scalar_Cannot_ParseResult(Name, resultValue.GetType()),
                  this);
    }
示例#10
0
        public override object ParseLiteral(IValueNode literal)
        {
            if (literal == null)
            {
                throw new ArgumentNullException(nameof(literal));
            }

            if (literal is NullValueNode)
            {
                return(null);
            }

            if (literal is FloatValueNode floatLiteral &&
                TryParseDecimal(floatLiteral.Value, out var d))
            {
                return(d);
            }

            if (literal is IntValueNode intLiteral &&
                TryParseDecimal(intLiteral.Value, out d))
            {
                return(d);
            }

            throw new ScalarSerializationException(
                      TypeResourceHelper.Scalar_Cannot_ParseLiteral(
                          Name, literal.GetType()));
        }
示例#11
0
        public override object?ParseLiteral(IValueNode valueSyntax, bool withDefaults = true)
        {
            if (valueSyntax is null)
            {
                throw new ArgumentNullException(nameof(valueSyntax));
            }

            if (valueSyntax is NullValueNode)
            {
                return(null);
            }

            if (valueSyntax is FloatValueNode floatLiteral)
            {
                return(ParseLiteral(floatLiteral));
            }

            // Input coercion rules specify that float values can be coerced
            // from IntValueNode and FloatValueNode:
            // http://facebook.github.io/graphql/June2018/#sec-Float

            if (valueSyntax is IntValueNode intLiteral)
            {
                return(ParseLiteral(intLiteral));
            }

            throw new SerializationException(
                      TypeResourceHelper.Scalar_Cannot_ParseLiteral(Name, valueSyntax.GetType()),
                      this);
        }
示例#12
0
        public override object ParseLiteral(IValueNode literal)
        {
            if (literal == null)
            {
                throw new ArgumentNullException(nameof(literal));
            }

            if (literal is IntValueNode intLiteral &&
                int.TryParse(
                    intLiteral.Value,
                    NumberStyles.Integer,
                    CultureInfo.InvariantCulture, out int i) &&
                i >= _min && i <= _max)
            {
                return(i);
            }

            if (literal is NullValueNode)
            {
                return(null);
            }

            throw new ScalarSerializationException(
                      TypeResourceHelper.Scalar_Cannot_ParseLiteral(
                          Name, literal.GetType()));
        }
示例#13
0
        public object?ParseLiteral(IValueNode valueSyntax, bool withDefaults = true)
        {
            if (valueSyntax is null)
            {
                throw new ArgumentNullException(nameof(valueSyntax));
            }

            if (valueSyntax is EnumValueNode evn &&
                _enumValues.TryGetValue(evn.Value, out IEnumValue? ev))
            {
                return(ev.Value);
            }

            if (valueSyntax is StringValueNode svn &&
                _enumValues.TryGetValue(svn.Value, out ev))
            {
                return(ev.Value);
            }

            if (valueSyntax is NullValueNode)
            {
                return(null);
            }

            throw new SerializationException(
                      TypeResourceHelper.Scalar_Cannot_ParseLiteral(Name, valueSyntax.GetType()),
                      this);
        }
示例#14
0
        public object?Serialize(object?runtimeValue)
        {
            if (runtimeValue is null)
            {
                return(null);
            }

            if (RuntimeType.IsInstanceOfType(runtimeValue) &&
                _valueLookup.TryGetValue(runtimeValue, out IEnumValue? enumValue))
            {
                return(enumValue.Name);
            }

            // schema first unbound enum type
            if (RuntimeType == typeof(object))
            {
                string name = _naming.GetEnumValueName(runtimeValue);
                if (_enumValues.TryGetValue(name, out enumValue))
                {
                    return(enumValue.Name);
                }
            }

            throw new SerializationException(
                      TypeResourceHelper.Scalar_Cannot_Serialize(Name),
                      this);
        }
示例#15
0
        public object Deserialize(object serialized)
        {
            if (TryDeserialize(serialized, out object v))
            {
                return(v);
            }

            throw new ArgumentException(
                      TypeResourceHelper.Scalar_Cannot_Deserialize(Name));
        }
 /// <summary>
 /// Initializes a new instance of the
 /// <see cref="MultiplierPathString"/> struct.
 /// </summary>
 /// <param name="value">The actual type name string</param>
 public MultiplierPathString(string value)
 {
     if (!IsValidPath(value))
     {
         throw new ArgumentException(
                   TypeResourceHelper.Type_Name_IsNotValid(value),
                   nameof(value));
     }
     Value = value;
 }
示例#17
0
        /// <summary>
        /// Deserializes the serialized value to it`s .net value representation.
        /// </summary>
        /// <param name="serialized">
        /// The serialized value representation.
        /// </param>
        /// <returns>
        /// Returns the .net value representation.
        /// </returns>
        /// <exception cref="ScalarSerializationException">
        /// The specified <paramref name="value" /> cannot be deserialized
        /// by this scalar.
        /// </exception>
        public virtual object Deserialize(object serialized)
        {
            if (TryDeserialize(serialized, out object v))
            {
                return(v);
            }

            throw new ScalarSerializationException(
                      TypeResourceHelper.Scalar_Cannot_Deserialize(Name));
        }
示例#18
0
        /// <summary>
        /// Serializes the .net value representation.
        /// </summary>
        /// <param name="value">
        /// The .net value representation.
        /// </param>
        /// <returns>
        /// Returns the serialized value.
        /// </returns>
        /// <exception cref="ScalarSerializationException">
        /// The specified <paramref name="value" /> cannot be serialized
        /// by this scalar.
        /// </exception>
        public virtual object Serialize(object value)
        {
            if (TrySerialize(value, out object s))
            {
                return(s);
            }

            throw new ScalarSerializationException(
                      TypeResourceHelper.Scalar_Cannot_Serialize(Name));
        }
示例#19
0
        protected override DateTimeOffset ParseLiteral(StringValueNode literal)
        {
            if (TryDeserializeFromString(literal.Value, out DateTimeOffset? value))
            {
                return(value.Value);
            }

            throw new ScalarSerializationException(
                      TypeResourceHelper.Scalar_Cannot_ParseLiteral(
                          Name, literal.GetType()));
        }
示例#20
0
        public sealed override IValueNode ParseValue(object value)
        {
            if (TryParseValue(value, out IValueNode valueNode))
            {
                return(valueNode);
            }

            throw new ScalarSerializationException(
                      TypeResourceHelper.Scalar_Cannot_ParseValue(
                          Name, value.GetType()));
        }
示例#21
0
        protected override NameString ParseLiteral(StringValueNode literal)
        {
            if (IsInstanceOfType(literal))
            {
                return(new NameString(literal.Value));
            }

            throw new ScalarSerializationException(
                      TypeResourceHelper.Scalar_Cannot_ParseLiteral(
                          Name, literal.GetType()));
        }
示例#22
0
        protected override Uri ParseLiteral(StringValueNode literal)
        {
            if (TryParseUri(literal.Value, out Uri uri))
            {
                return(uri);
            }

            throw new ScalarSerializationException(
                      TypeResourceHelper.Scalar_Cannot_ParseLiteral(
                          Name, literal.GetType()));
        }
示例#23
0
        protected override Uri ParseLiteral(StringValueNode valueSyntax)
        {
            if (TryParseUri(valueSyntax.Value, out Uri uri))
            {
                return(uri);
            }

            throw new SerializationException(
                      TypeResourceHelper.Scalar_Cannot_ParseLiteral(Name, valueSyntax.GetType()),
                      this);
        }
示例#24
0
        protected override NameString ParseLiteral(StringValueNode valueSyntax)
        {
            if (IsInstanceOfType(valueSyntax))
            {
                return(new NameString(valueSyntax.Value));
            }

            throw new SerializationException(
                      TypeResourceHelper.Scalar_Cannot_ParseLiteral(Name, valueSyntax.GetType()),
                      this);
        }
示例#25
0
        protected override DateTime ParseLiteral(StringValueNode valueSyntax)
        {
            if (TryDeserializeFromString(valueSyntax.Value, out DateTime? value))
            {
                return(value.Value);
            }

            throw new SerializationException(
                      TypeResourceHelper.Scalar_Cannot_ParseLiteral(Name, valueSyntax.GetType()),
                      this);
        }
示例#26
0
        public object?Deserialize(object?resultValue)
        {
            if (TryDeserialize(resultValue, out object?runtimeValue))
            {
                return(runtimeValue);
            }

            throw new SerializationException(
                      TypeResourceHelper.Scalar_Cannot_Deserialize(Name),
                      this);
        }
示例#27
0
        protected override Guid ParseLiteral(StringValueNode valueSyntax)
        {
            if (Utf8Parser.TryParse(valueSyntax.AsSpan(), out Guid g, out _, _format[0]))
            {
                return(g);
            }

            throw new SerializationException(
                      TypeResourceHelper.Scalar_Cannot_ParseLiteral(Name, valueSyntax.GetType()),
                      this);
        }
示例#28
0
        protected override Guid ParseLiteral(StringValueNode literal)
        {
            if (Utf8Parser.TryParse(literal.AsSpan(), out Guid g, out int _, _format))
            {
                return(g);
            }

            throw new ScalarSerializationException(
                      TypeResourceHelper.Scalar_Cannot_ParseLiteral(
                          Name, literal.GetType()));
        }
示例#29
0
        protected override TimeSpan ParseLiteral(StringValueNode valueSyntax)
        {
            if (TryDeserializeFromString(valueSyntax.Value, _format, out TimeSpan? value) &&
                value != null)
            {
                return(value.Value);
            }

            throw new SerializationException(
                      TypeResourceHelper.Scalar_Cannot_ParseLiteral(Name, valueSyntax.GetType()),
                      this);
        }
示例#30
0
        public override object Serialize(object value)
        {
            if (value == null)
            {
                return(null);
            }

            if (value is Guid guid)
            {
                return(guid);
            }

            throw new ScalarSerializationException(
                      TypeResourceHelper.Scalar_Cannot_Serialize(Name));
        }