Exemplo n.º 1
0
        private void CheckClass(XmlClassItem data)
        {
            Assert.IsNotNull(data);

            var memberNames = new HashSet <string>();

            foreach (var item in data.members)
            {
                if (string.IsNullOrEmpty(item.name))
                {
                    throw new InvalidSchemaException("Member name cannot be empty - " + data.name);
                }
                if (string.IsNullOrEmpty(item.type))
                {
                    throw new InvalidSchemaException("Member type cannot be empty - " + data.name + "." + item.name);
                }

                if (string.IsNullOrEmpty(item.caseValue))
                {
                    if (memberNames.Contains(item.name))
                    {
                        throw new InvalidSchemaException("Duplicate member name - " + data.name + "." + item.name);
                    }
                    memberNames.Add(item.name);
                }
            }
        }
Exemplo n.º 2
0
        private void CheckMutableMembers(XmlClassItem classItem)
        {
            HashSet <string> enumValues = null;
            var isMutable = !string.IsNullOrEmpty(classItem.switchEnum);

            if (isMutable)
            {
                var enumMember = classItem.members.FirstOrDefault(item => item.name.Equals(classItem.switchEnum, StringComparison.Ordinal));
                if (enumMember == null)
                {
                    throw new InvalidSchemaException("Switch member not found - " + classItem.name + "." + classItem.switchEnum);
                }
                if (enumMember.type != Constants.TypeEnum)
                {
                    throw new InvalidSchemaException("Switch member should have enum type - " + classItem.name + "." + classItem.switchEnum);
                }

                if (!_enums.TryGetValue(enumMember.typeid, out var switchEnum))
                {
                    throw new InvalidSchemaException("Unknown enum type - " + classItem.name + "." + classItem.switchEnum);
                }
                enumValues = new HashSet <string>(switchEnum.items.Select(item => item.name));
            }

            foreach (var item in classItem.members)
            {
                if (string.IsNullOrEmpty(item.caseValue))
                {
                    continue;
                }

                if (!isMutable)
                {
                    throw new InvalidSchemaException("Member has case attribute but switch attribute hasn't been defined " + classItem.name + "." + item.name);
                }

                var caseValues = item.caseValue.Split(Constants.ValueSeparators, StringSplitOptions.RemoveEmptyEntries);
                var wrongValue = caseValues.FirstOrDefault(value => !enumValues.Contains(value));
                if (wrongValue != null)
                {
                    throw new InvalidSchemaException("Unknown case value in " + classItem.name + "." + item.name + " - " + wrongValue);
                }
            }
        }