Пример #1
0
 public void ExpectFalse()
 {
     expectedResult = ValueBoolean.FALSE;
 }
Пример #2
0
        public static bool TryCreateValue(string typeString, string valueString, out Value value)
        {
            value = null;

            var isNull = valueString.Equals("NULL") || valueString.Equals("null") || valueString.Equals("");

            switch (typeString)
            {
            case "integer":
                if (isNull)
                {
                    value = new ValueInt(null);
                    return(true);
                }
                if (!long.TryParse(valueString, out var intResult))
                {
                    return(false);
                }
                value = new ValueInt(intResult);
                return(true);

            case "string":
                if (isNull)
                {
                    value = new ValueString(null);
                    return(true);
                }
                value = new ValueString(valueString.Trim('\"'));
                return(true);

            case "time":
                if (isNull)
                {
                    value = new ValueTime(null as RefStruct <long>);
                    return(true);
                }
                value = new ValueTime(valueString);
                return(true);

            case "double":
                if (isNull)
                {
                    value = new ValueDouble(null);
                    return(true);
                }
                if (!double.TryParse(valueString, NumberStyles.Any, CultureInfo.InvariantCulture, out var doubleResult))
                {
                    return(false);
                }
                value = new ValueDouble(doubleResult);
                return(true);

            case "boolean":
                if (isNull)
                {
                    value = new ValueBoolean(null);
                    return(true);
                }
                if (!bool.TryParse(valueString, out var booleanResult))
                {
                    return(false);
                }
                value = new ValueBoolean(booleanResult);
                return(true);

            case "duration":
                if (isNull)
                {
                    value = new ValueDuration(null as RefStruct <long>);
                    return(true);
                }
                value = new ValueDuration(valueString);
                return(true);

            case "contact":
                if (isNull)
                {
                    value = new ValueContact(null, null);
                    return(true);
                }

                var split = valueString.Split(' ');
                if (split.Length != 2 && split.Length != 3)
                {
                    return(false);
                }
                if (!IPAddress.TryParse(split[1], out var address))
                {
                    return(false);
                }
                var port = 555;
                if (split.Length == 3 && !int.TryParse(split[2], out port))
                {
                    return(false);
                }
                if (!split[0].StartsWith("/"))
                {
                    split[0] = '/' + split[0];
                }
                value = new ValueContact(new PathName(split[0]), address, port);
                return(true);
            }

            var splitByOf = typeString.Split(" of ", 2);

            if (splitByOf.Length != 2)
            {
                return(false);
            }
            var splitBySpace = splitByOf[1].Split(' ', 2);

            if (splitBySpace.Length != 2)
            {
                return(false);
            }

            if (!int.TryParse(splitBySpace[0], out var count))
            {
                return(false);
            }

            // based on StackOverflow answer: https://stackoverflow.com/a/3147901
            var collectionStrings = Regex.Split(valueString.Trim('{', '}', '[', ']'), ",(?=(?:[^']*'[^']*')*[^']*$)")
                                    .Select(s => s.Trim(' ')).ToList();

            if (collectionStrings.Count != count)
            {
                if (count != 0 && (collectionStrings.Count != 1 || !string.IsNullOrEmpty(collectionStrings[0])))
                {
                    return(false);
                }
                collectionStrings = new List <string>(count);
            }

            var elements          = new List <Value>(count);
            var elementTypeString = splitBySpace[1];

            foreach (var element in collectionStrings)
            {
                if (!TryCreateValue(elementTypeString, element, out var elementVal))
                {
                    return(false);
                }
                elements.Add(elementVal);
            }

            if (!TryGetPrimitiveAttributeType(elementTypeString, out var elementType))
            {
                return(false);
            }

            switch (splitByOf[0])
            {
            case "set":
                value = new ValueSet(elements.ToHashSet(), elementType);
                return(true);

            case "list":
                value = new ValueList(elements, elementType);
                return(true);
            }

            return(false);
        }
Пример #3
0
 public void ExpectTrue()
 {
     expectedResult = ValueBoolean.TRUE;
 }