Source/NFX.Erlang/DataAccess/ErlSchemaUtils.cs
/// <summary>
/// Convert Row to a hierarchical term that corresponds to the row's schema.
/// The Erlang term is in the form:
/// <code>{SchemaName::atom(), [{FieldName::atom(), Value}]}.</code>
/// </summary>
/// <param name="row">Row to convert to Erlang object</param>
/// <param name="targetName">Target to use to lookup field attributes in the row</param>
/// <param name="schemaName">Alternative schema name to use for the outermost name of the schema in the output.</param>
/// <param name="outputDefVals">When false no field values are included in output if they are equal to default values</param>
Convert Row to a hierarchical term that corresponds to the row's schema.
Convert Row to a hierarchical term that corresponds to the row ' s schema .
public static IErlObject ToErlObject(this Row row, string targetName = null, string schemaName = null, bool outputDefVals = true)
    {
      var list = new ErlList();
      for (int i = 0; i < row.Count(); ++i)
      {
        var fld    = row.Schema[i];
        var atr    = fld[targetName];
        var clrVal = row[i];
        IErlObject erlVal;
        if (clrVal is Row)
        {
          var child = (Row)clrVal;
          if (ts_VisitedRows == null)
            ts_VisitedRows = new HashSet<Row>(ReferenceEqualityComparer<Row>.Instance);
          if (ts_VisitedRows.Contains(child))
            throw new ErlDataAccessException(StringConsts.ERL_CANNOT_CONVERT_TYPES_CYCLE_ERROR
                                             .Args(row.Schema.Name, fld.Name));
          ts_VisitedRows.Add(child);
          try { erlVal = child.ToErlObject(targetName, null, outputDefVals); }
          finally { ts_VisitedRows.Remove(child); }
          list.Add(erlVal);
        }
        else if (clrVal != null && (outputDefVals || atr.Default == null || !clrVal.Equals(atr.Default)))
        {
          erlVal = SchemaMap.ClrToErlValue(atr.BackendType, clrVal);
          list.Add(new ErlTuple(new ErlAtom(fld.Name), erlVal));
        }
        else if (clrVal == null && atr.Default != null)
        {
          erlVal = SchemaMap.ClrToErlValue(atr.BackendType, atr.Default);
          list.Add(new ErlTuple(new ErlAtom(fld.Name), erlVal));
        }
      }
      return new ErlTuple(new ErlAtom(schemaName ?? row.Schema.Name), list);
    }
public static IErlObject ToErlObject ( this Row row , string targetName = null , string schemaName = null , bool outputDefVals = true ) { var list = new ErlList ( ) ; for ( int i = 0 ; i < row . Count ( ) ; ++ i ) { var fld = row . Schema [ i ] ; var atr = fld [ targetName ] ; var clrVal = row [ i ] ; IErlObject erlVal ; if ( clrVal is Row ) { var child = ( Row ) clrVal ; if ( ts_VisitedRows == null ) ts_VisitedRows = new HashSet < Row > ( ReferenceEqualityComparer < Row > . Instance ) ; if ( ts_VisitedRows . Contains ( child ) ) throw new ErlDataAccessException ( StringConsts . ERL_CANNOT_CONVERT_TYPES_CYCLE_ERROR . Args ( row . Schema . Name , fld . Name ) ) ; ts_VisitedRows . Add ( child ) ; try { erlVal = child . ToErlObject ( targetName , null , outputDefVals ) ; } finally { ts_VisitedRows . Remove ( child ) ; } list . Add ( erlVal ) ; } else if ( clrVal != null && ( outputDefVals || atr . Default == null || ! clrVal . Equals ( atr . Default ) ) ) { erlVal = SchemaMap . ClrToErlValue ( atr . BackendType , clrVal ) ; list . Add ( new ErlTuple ( new ErlAtom ( fld . Name ) , erlVal ) ) ; } else if ( clrVal == null && atr . Default != null ) { erlVal = SchemaMap . ClrToErlValue ( atr . BackendType , atr . Default ) ; list . Add ( new ErlTuple ( new ErlAtom ( fld . Name ) , erlVal ) ) ; } } return new ErlTuple ( new ErlAtom ( schemaName ?? row . Schema . Name ) , list ) ; }
Convert Row to a hierarchical term that corresponds to the row's schema.
Convert Row to a hierarchical term that corresponds to the row ' s schema .
// Primitive types are converted using the schema type mapping functions // If the row is nested, traverse the hierarchy recursively: // Skip default values if instructed to do so with outputDefVals
Row
this
targetName
string
Target to use to lookup field attributes in the row
Target to use to lookup field attributes in the row
schemaName
string
Alternative schema name to use for the outermost name of the schema in the output.
Alternative schema name to use for the outermost name of the schema in the output .
outputDefVals
bool
When false no field values are included in output if they are equal to default values
When false no field values are included in output if they are equal to default values
row
Row to convert to Erlang object
Row to convert to Erlang object