1 /** 2 * The module implements a universal structure UniNode and functions to work with it 3 * 4 * Copyright: (c) 2015-2016, Milofon Project. 5 * License: Subject to the terms of the BSD license, as written in the included LICENSE.txt file. 6 * Authors: Maksim Galanin 7 */ 8 9 module proped.uninode; 10 11 private 12 { 13 import std.typecons : Nullable; 14 import std.variant : Algebraic, This, VariantN; 15 } 16 17 18 /** 19 * The data type allows you to save a specified type that implements the mechanisms nesting 20 */ 21 alias UniNode(Types...) = Algebraic!(Types, This[], This[string]); 22 23 24 /** 25 * Check the type of the array of embedded objects of universal content object 26 */ 27 @property bool isArray(T : VariantN!(tsize, Types), size_t tsize, Types...)(T node) 28 { 29 return typeid(T[]) == node.type; 30 } 31 32 33 /** 34 * Check the type of associative array contents generic object 35 */ 36 @property bool isObject(T : VariantN!(tsize, Types), size_t tsize, Types...)(T node) 37 { 38 return typeid(T[string]) == node.type; 39 } 40 41 42 /** 43 * Check for zero content 44 */ 45 @property bool hasNull(T : VariantN!(tsize, Types), size_t tsize, Types...)(T node) 46 { 47 return !node.hasValue; 48 } 49 50 51 /** 52 * Converting generic object into an associative array 53 */ 54 T[string] toObject(T : VariantN!(tsize, Types), size_t tsize, Types...)(T node) 55 { 56 if (node.isObject) 57 return node.get!(T[string]); 58 return null; 59 } 60 61 62 /** 63 * Converting a universal object in the array 64 */ 65 T[] toArray(T : VariantN!(tsize, Types), size_t tsize, Types...)(T node) 66 { 67 if (node.isArray) 68 return node.get!(T[]); 69 return null; 70 } 71