コード例 #1
0
 public void Write(OclAttribute attribute)
 {
     WriteNextLine();
     WriteIndent();
     WriteIdentifier(attribute.Name);
     writer.Write(" = ");
     WriteValue(attribute.Value);
 }
コード例 #2
0
        void WriteValue(object?value)
        {
            if (value == null)
            {
                writer.Write("null");
                return;
            }

            switch (value)
            {
            case bool b:
                writer.Write(b ? "true" : "false");
                return;

            case byte b:
                writer.Write(b);
                return;

            case ushort s:
                writer.Write(s);
                return;

            case short s:
                writer.Write(s);
                return;

            case uint i:
                writer.Write(i);
                return;

            case int i:
                writer.Write(i);
                return;

            case ulong l:
                writer.Write(l);
                return;

            case long l:
                writer.Write(l);
                return;

            case float f:
                writer.Write(f);
                return;

            case double d:
                writer.Write(d);
                return;

            case decimal d:
                writer.Write(d);
                return;

            case char c:
                WriteSingleLineStringLiteral(c.ToString());
                return;

            case string s:
                var literal = OclStringLiteral.Create(s);
                literal.HeredocTag = Options.DefaultHeredocTag;
                WriteValue(literal);
                return;

            case OclStringLiteral s:
                WriteValue(s);
                return;
            }

            var valueType = value.GetType();

            if (OclAttribute.IsObjectDictionary(valueType))
            {
                WriteValue((IReadOnlyDictionary <string, object?>)value);
                return;
            }

            if (OclAttribute.IsStringDictionary(valueType))
            {
                WriteValue((IReadOnlyDictionary <string, string>)value);
                return;
            }

            if (OclAttribute.IsSupportedValueCollectionType(valueType))
            {
                var enumerable = (IEnumerable)value;
                writer.Write('[');
                var isFirst = true;
                foreach (var item in enumerable)
                {
                    if (!isFirst)
                    {
                        writer.Write(", ");
                    }
                    isFirst = false;
                    WriteValue(item);
                }

                writer.Write(']');
                return;
            }

            throw new InvalidOperationException($"The type {value.GetType().FullName} is not a valid attribute value and can not be serialized");
        }