public static TomlTable TransformToSourceTable(this TomlTable table, IConfigSource source, TomlObject rootSource = null) { table.CheckNotNull(nameof(table)); var sourcesTable = rootSource == null ? Toml.Create(table.TableType) : TomlObjectFactory.CreateEmptyAttachedTable(rootSource, table.TableType); foreach (var r in table.Rows) { if (r.Value.TomlType == TomlObjectType.Table) { sourcesTable[r.Key] = ((TomlTable)r.Value).TransformToSourceTable(source, sourcesTable); } else if (r.Value.TomlType == TomlObjectType.ArrayOfTables) { var arr = (TomlTableArray)r.Value; var sourcesArray = new TomlTableArray(table.Root, arr.Items.Select(t => t.TransformToSourceTable(source, sourcesTable))); sourcesTable[r.Key] = sourcesArray; } else { sourcesTable[r.Key] = new TomlSource(sourcesTable.Root, source); } } return(sourcesTable); }
public static T ResolveKeyChain <T>(this TomlTable table, IList <string> keyChain) where T : TomlObject { var current = table.CheckNotNull(nameof(table)); for (int i = 0; i < keyChain.Count - 1; i++) { current = (TomlTable)current[keyChain[i]]; } return((T)current[keyChain.Last()]); }
public bool ClearFrom(TomlTable from) { from.CheckNotNull(nameof(from)); var keySegment = this.segment as KeySegment; if (keySegment == null) { throw new InvalidOperationException("Final segment of path needs to be a key"); } var targetTable = (TomlTable)this.prefixPath.Apply(from); return(keySegment.ClearFrom(targetTable)); }
public static TomlTable Clone(this TomlTable input) { input.CheckNotNull(nameof(input)); TomlTable cloned = Toml.Create(input.Root.Settings); cloned.TableType = input.TableType; foreach (var r in input.InternalRows) { switch (r.Value.TomlType) { case TomlObjectType.Table: cloned.AddRow(r.Key, ((TomlTable)r.Value).Clone()); break; case TomlObjectType.ArrayOfTables: cloned.AddRow(r.Key, ((TomlTableArray)r.Value).Clone()); break; default: cloned[r.Key.Value] = r.Value; break; } } return(cloned); }