/// <summary> /// Creates a structurally equivalent simple value from a given value. /// </summary> /// <param name="value">The value to copy the structure of.</param> /// <returns>A structurally equivalent copy of the given value.</returns> public static IStonSimpleValue Copy(IStonSimpleValue value) { if (value == null) { throw new ArgumentNullException("value"); } return(new StonSimpleValue(value)); }
/// <summary> /// Checks the validity of a given STON simple value. /// </summary> /// <param name="value">The simple value to check the validity of.</param> public static void ValidateSimpleValue(IStonSimpleValue value) { if (value == null) { throw new ArgumentNullException("value"); } // handling the null value if (value.DataType == StonDataType.Null) { if (!string.IsNullOrEmpty(value.Content)) { throw new StonException("A null simple value must be represented with a non-existing or empty content."); } else { return; } } else if (value.Content == null) { throw new StonException("A non-null simple value must be represented with an existing content."); } switch (value.DataType) { // significand and exponent are expected // or zero, if the number represented is zero case StonDataType.Number: ValidateNumberContent(value.Content); break; // hexadecimal digits pairs with optional initial minus are expected case StonDataType.Binary: ValidateBinaryContent(value.Content); break; // a CANUN path is expected case StonDataType.Named: ValidateNamedValueContent(value.Content); break; // all non-empty text is valid case StonDataType.Text: break; // all non-empty code is valid case StonDataType.Code: break; default: throw new StonException("Unknown simple value data type."); } }
/// <summary> /// Creates a new simple-valued entity, with a given value and optional declared type and global identifier. /// </summary> /// <param name="value">The value of the entity.</param> /// <param name="type">The declared type of the entity.</param> /// <param name="globalIdentifier">The global identifier of the entity.</param> public StonSimpleEntity(IStonSimpleValue value, IStonType type = null, string globalIdentifier = null) : base(type, globalIdentifier) { if (value == null) { throw new ArgumentNullException("value"); } Value = StonSimpleValue.Copy(value); Helpers.Validator.ValidateEntity(this); }
// writes a simple value, depending on its data type private void WriteSimpleValue(StonTokenWriter writer, IStonSimpleValue value) { if (value == null) { throw new StonException("A simple-valued entity cannot have a non-existing value."); } switch (value.DataType) { case StonDataType.Null: writer.Write("null"); break; case StonDataType.Number: writer.WriteCanonicalNumberLiteral(value.Content); break; case StonDataType.Binary: writer.WriteBinaryLiteral(value.Content); break; case StonDataType.Named: writer.Write(value.Content); break; case StonDataType.Text: writer.WriteStringLiteral(value.Content, '"'); break; case StonDataType.Code: writer.WriteStringLiteral(value.Content, '`'); break; default: throw new StonException($"Unknown simple value data type: { (int)value.DataType }."); } }
/// <summary> /// Creates a structurally equivalent simple value from a given value. /// </summary> /// <param name="value">The value to copy the structure of.</param> public StonSimpleValue(IStonSimpleValue value) : this(value.DataType, value.Content) { }
/// <summary> /// Creates a new simple-valued entity, with a given value and optional declared type and global identifier. /// </summary> /// <param name="value">The value of the entity.</param> /// <param name="type">The declared type of the entity.</param> /// <param name="globalIdentifier">The global identifier of the entity.</param> /// <returns>The new STON entity.</returns> public IStonSimpleEntity CreateSimpleEntity(IStonSimpleValue value, IStonType type = null, string globalIdentifier = null) => new StonSimpleEntity(value, type, globalIdentifier);
public ASimpleEntity(IStonSimpleValue value, IStonType type = null, string globalIdentifier = null) { GlobalIdentifier = globalIdentifier; Type = type; Value = value; }
// creates an object from a STON value private static object CreateFromSton <TObject>(IStonDocument document, IStonValuedEntity entity, IDictionary <IStonValuedEntity, object> builtObjects) { var clrType = typeof(TObject); if (clrType == typeof(string)) { return((entity as IStonSimpleEntity).Value.Content); } else if (clrType == typeof(int) || clrType == typeof(int?) || clrType == typeof(long) || clrType == typeof(long?)) { // miiiight wanna make this a built-in part of STON library at some point IStonSimpleValue value = (entity as IStonSimpleEntity).Value; long result = 0; if (value.Content != "0") { int eidx = value.Content.IndexOf('e'); string sstr = value.Content.Remove(eidx); string estr = value.Content.Substring(eidx + 1); result = long.Parse(sstr); var exponent = int.Parse(estr); while (exponent-- > 0) { result *= 10; } } if (clrType == typeof(int) || clrType == typeof(int?)) { return((int)result); } else { return(result); } } else if (clrType == typeof(IPlaylistItem)) { switch ((entity.Type as IStonNamedType).Name) { case "Playlist": return(new Playlist()); case "Track": return(new Track()); default: throw new NotSupportedException(); } } else if (clrType == typeof(IStreamProvider)) { switch ((entity.Type as IStonNamedType).Name) { case "Loop": return(new LoopStreamProvider(builtObjects[document.GetParentContext(entity)] as Track)); default: throw new NotSupportedException(); } } else { throw new NotSupportedException(); } }