static RedStatusResult RedisFunction2(ICursor cursor, RedisValue[] args, RedisKey[] keys) { var count = cursor.Get(keys[0]).AsInt() ?? 0; var toAdd = (int)args[0]; for (var i = 0; i < count; i++) { var key = $"{keys[0]}_{i}"; var currentValue = cursor.Get(key).AsLong() ?? 0; var res = cursor.Set(key, currentValue + toAdd); if (!res.IsOk) { // Reverting for (var j = i - 1; j >= 0; j--) { key = keys[0] + "_" + j; var newVal = cursor.Get(key).AsLong(); if (newVal != null) { cursor.Set(key, newVal - toAdd); } } return(RedResult.Error()); } } return(RedResult.Ok); }
void LoadFromCursor2(ICursor source, IEditable destination) { if (source.IsNull) { destination.Set(null); return; } var obj = destination as IEditableObject; if (obj != null) { var schema = source.Schema; for (int i = 0; i < schema.FieldCount; ++i) { LoadFromCursor2(source.GoTo(i, false), obj.GetField(i)); } } else { var v = destination as IEditableVariable; if (v != null && source.Schema.Id == (int)BuiltInSchema.Variable) { ICursor innerSource = source.Get <ICursor>(); v.DataSchema = innerSource.Schema; LoadFromCursor2(innerSource, v.Get <IEditable>()); } else { var list = destination as IEditableList; if (list != null) { list.Clear(); // ensure list has been created (is not null) if (source.Count > 0) { foreach (var c in source.Children) { LoadFromCursor2(c, list.Add()); } } } else { destination.Set(source.Get()); } } } }
public override bool TryConvert(ConvertBinder binder, out object result) { if (cursor == null || cursor.IsNull) { result = null; return(!binder.Type.GetTypeInfo().IsValueType); } if (binder.Type == typeof(System.Collections.IEnumerable)) { result = cursor.Children.Select(Wrap).ToList(); return(true); } if (binder.Type == typeof(ICursor) || binder.Type.GetTypeInfo().IsAssignableFrom(cursor.GetType())) { result = cursor; return(true); } var v = cursor.Get(); if (v != null && binder.Type.GetTypeInfo().IsAssignableFrom(v.GetType())) { result = v; return(true); } return(base.TryConvert(binder, out result)); }
public static T Get <T>(this ICursor cursor, T defaultValue) { if (cursor.IsNull) { return(defaultValue); } return((T)cursor.Get()); }
public static T Get <T>(this ICursor cursor) { object value = cursor.Get(); if (value == null) { return(default(T)); } return((T)value); }
ICursor ICursor.GoTo(int index, bool unwrapVariable) { ICursor item = GetItem(index); if (unwrapVariable && item is IEditableVariable) { item = item.Get <ICursor>(); } return(item); }
public async Task Export(DateTime cursor) { using (_logger.Scope("Exporting service status.")) { var rootComponent = _componentExporter.Export(); var recentEvents = _eventExporter.Export(cursor); var lastUpdated = await _cursor.Get(StatusUpdater.LastUpdatedCursorName); await _serializer.Serialize(cursor, lastUpdated, rootComponent, recentEvents); } }
private async Task <DateTime> ProcessCursor(string name, Func <DateTime, Task <DateTime?> > processCursor) { var lastCursor = await _cursor.Get(name); var nextCursor = await processCursor(lastCursor); if (nextCursor.HasValue) { await _cursor.Set(name, nextCursor.Value); } return(nextCursor ?? lastCursor); }
public async Task <DateTime> FetchLatest() { var lastCursor = await _cursor.Get(Name); var nextCursor = await _processor.FetchSince(lastCursor); if (nextCursor.HasValue) { await _cursor.Set(Name, nextCursor.Value); } return(nextCursor ?? lastCursor); }
public static void WriteTo(this ICursor source, JsonWriter writer, bool stripNullValues = true) { if (source == null || source.IsNull) { writer.WriteNull(); } else { switch (source.Schema.DataType) { case DataType.Class: if (source.Schema.Id == (int)BuiltInSchema.Variable) { var content = source.GoTo((int)VariableLayout.Data, true); if (!content.IsNull && content.Schema.DataType != DataType.String && content.Schema.DataType != DataType.Boolean && (content.Schema.DataType == DataType.List || content.Schema.DataType == DataType.MultiChoice || EditablePrimitive.IsSupportedType(content.Schema.DataType) ) ) { writer.WriteStartObject(); writer.WritePropertyName("@schema"); writer.WriteValue(source.Schema.Name); writer.WritePropertyName("DataSchema"); writer.WriteValue(content.Schema.Name); writer.WritePropertyName("Data"); content.WriteTo(writer, stripNullValues); writer.WriteEndObject(); } else { content.WriteTo(writer, stripNullValues); } } else { writer.WriteStartObject(); writer.WritePropertyName("@schema"); writer.WriteValue(source.Schema.Name); foreach (var f in source.Schema.Fields) { var field = source.GoTo(f, false); if (!stripNullValues || !field.IsNull) { writer.WritePropertyName(f.Name); field.WriteTo(writer, stripNullValues); } } writer.WriteEndObject(); } break; case DataType.MultiChoice: case DataType.List: { writer.WriteStartArray(); for (int i = 0; i < source.Count; ++i) { var field = source.GoTo(i, false); if (!stripNullValues || !field.IsNull) { field.WriteTo(writer, stripNullValues); } } writer.WriteEndArray(); } break; default: { EditablePrimitive.PrimitiveType typeHelper; if (EditablePrimitive.TryGetPrimitiveTypeHelper(source.Schema, out typeHelper)) { typeHelper.JsonWrite(writer, source.Get()); } else { throw new Exception(string.Format("Unable to convert data type '{0}' to JSON string.", source.Schema.DataType)); } } break; } } }
object FromCursorInternal(ICursor cursor, Type targetType) { if (cursor == null || cursor.IsNull) { return(null); } if (EditablePrimitive.IsSupportedType(cursor.Schema.DataType)) { return(cursor.Get()); } else if (cursor.Schema.Id == (int)BuiltInSchema.DateTimeOffset) { return(new DateTimeOffset(cursor.GoTo("Time").Get <DateTime>(), cursor.GoTo("Offset").Get <TimeSpan>())); } else if (cursor.Schema.Id == (int)BuiltInSchema.Version) { int major = cursor.GoTo("Major").Get <int>(); int minor = cursor.GoTo("Minor").Get <int>(); int build = cursor.GoTo("Build").Get <int>(); int revision = cursor.GoTo("Revision").Get <int>(); if (revision >= 0) { return(new Version(major, minor, build, revision)); } if (build >= 0) { return(new Version(major, minor, build)); } return(new Version(major, minor)); } else if (cursor.Schema.Id == (int)BuiltInSchema.Item) { return(DeserializeItem(cursor)); } Type type; if (!schemaTypeMap.TryGetTypeForSchema(cursor.Schema, out type)) { if (cursor.Schema.DataType == DataType.Choice) { return(new Choice(cursor.Schema, cursor.Get <int?>(), true)); } else { if (targetType != null && (targetType.GetTypeInfo().IsAssignableFrom(typeof(IEditable)) || targetType.GetTypeInfo().IsAssignableFrom(typeof(IEditableVariable)))) { return(editableFactory.Create(cursor)); } throw new MissingTypeMappingException(cursor.Schema); } } else { if (targetType != null && !targetType.GetTypeInfo().IsAssignableFrom(type)) { if (targetType.GetTypeInfo().IsAssignableFrom(typeof(IEditable)) || targetType.GetTypeInfo().IsAssignableFrom(typeof(IEditableVariable))) { return(editableFactory.Create(cursor)); } else if (targetType.IsArray && typeof(System.Collections.IEnumerable).GetTypeInfo().IsAssignableFrom(type)) { type = targetType; } else { throw new InvalidCastException(string.Format("Cannot cast from {0} to {1}.", type.Name, targetType.Name)); } } } object obj = Construct(type, cursor); ApplyToObject(obj, cursor); return(obj); }
bool TryGetValueForProperty(ICursor source, string name, Type propertyType, out object value) { Field f; if (!source.Schema.TryLookup(name, out f)) { value = null; return(false); } ICursor propertyCursor = source.GoTo(f.Index, !(propertyType == typeof(IEditableVariable))); Type targetType = Nullable.GetUnderlyingType(propertyType) ?? propertyType; // null value handling if (propertyCursor.IsNull) { value = null; if (!propertyType.GetTypeInfo().IsValueType || propertyType.GetTypeInfo().IsGenericType&& propertyType.GetGenericTypeDefinition() == typeof(Nullable <>)) { if (targetType == typeof(Choice)) { value = new Choice(propertyCursor.Schema, null, f.Nullable); } return(true); } else if (typeof(INullable).GetTypeInfo().IsAssignableFrom(propertyType)) // null handling for SqlTypes (e.g. SqlHierarchyId) { value = propertyType.GetTypeInfo().GetProperty("Null").GetValue(null); // get static null value of type return(true); } // skipping null value for non nullable property... return(false); } // now handle non-null record field values... if (propertyCursor.Schema.DataType == DataType.Class || propertyCursor.Schema.DataType == DataType.List || propertyCursor.Schema.DataType == DataType.MultiChoice) { if (propertyCursor.Schema.FieldCount > 0 && propertyCursor.Schema[0].Schema.Id == (int)BuiltInSchema.Variable) { // special handling for interface type IList<> when deserializing from record type List<Variable> if (targetType.GetTypeInfo().IsInterface&& targetType.GetTypeInfo().IsGenericType && (targetType.GetGenericTypeDefinition() == typeof(IList <>) || targetType.GetGenericTypeDefinition() == typeof(IEnumerable <>))) { targetType = typeof(List <>).MakeGenericType(targetType.GetTypeInfo().GetGenericArguments()); } value = Construct(targetType, propertyCursor); ApplyToObject(value, propertyCursor); } else { value = this.FromCursorInternal(propertyCursor, targetType); } } else { if (propertyType.GetTypeInfo().IsAssignableFrom(typeof(IEditable)) || propertyType.GetTypeInfo().IsAssignableFrom(typeof(IEditableVariable))) { value = editableFactory.Create(propertyCursor); } else if (propertyType.GetTypeInfo().IsAssignableFrom(typeof(ICursor))) { value = propertyCursor; } else { value = propertyCursor.Get(); } } if (targetType.GetTypeInfo().IsEnum) { var underlyingEnumType = Enum.GetUnderlyingType(targetType); if (!underlyingEnumType.GetTypeInfo().IsAssignableFrom(value.GetType())) { value = Convert.ChangeType(value, underlyingEnumType); } value = Enum.ToObject(targetType, value); } else if (targetType == typeof(Choice)) { System.Diagnostics.Debug.Assert(value != null); if (!typeof(int).GetTypeInfo().IsAssignableFrom(value.GetType())) { value = Convert.ChangeType(value, typeof(int)); } value = new Choice(propertyCursor.Schema, (int?)value, f.Nullable); } if (!targetType.GetTypeInfo().IsAssignableFrom(value.GetType())) { value = Convert.ChangeType(value, targetType); } return(true); }
public override bool Equals(object obj) { if (obj == null) { return(this.IsNull); } if (base.Equals(obj)) { return(true); } ICursor cursor = obj as ICursor; if (cursor == null) { return(ComparePrimitive(obj)); } if (this.IsNull != cursor.IsNull) { return(false); } if (this.IsNull) { return(true); } switch (this.schema.DataType) { case DataType.Void: return(cursor.Schema.DataType == DataType.Void); case DataType.MultiChoice: case DataType.List: return(this.Cast <ICursor>().SequenceEqual(cursor.Children)); case DataType.Class: { if (this.Schema == cursor.Schema) { return(this.Cast <ICursor>().SequenceEqual(cursor.Children)); } else { foreach (var f in this.Schema.Fields) { ICursor otherField; if (!cursor.TryGoTo(f.Name, out otherField)) { return(false); } if (!this.GoTo(f).Equals(otherField)) { return(false); } } } } break; default: return(ComparePrimitive(cursor.Get())); } return(true); }