Skip to content
This repository has been archived by the owner on Oct 19, 2020. It is now read-only.

Anomaly with Add() vs. Update() with array of tables #44

Closed
jgriffitts opened this issue Mar 9, 2018 · 1 comment
Closed

Anomaly with Add() vs. Update() with array of tables #44

jgriffitts opened this issue Mar 9, 2018 · 1 comment
Assignees
Labels
Milestone

Comments

@jgriffitts
Copy link

jgriffitts commented Mar 9, 2018

The following short program demonstrates a problem with Update() that I have not been able to understand or resolve. The call to Add() works, but Update() does not.

using System;
using System.Collections.Generic;
using Nett;

namespace ConsoleApp_TOML
{
    class Program
    {
        public class test_tbl1
        {
            public string label { get; set; }
            public DateTimeOffset timestamp { get; set; }

            public test_tbl1(string l, DateTimeOffset t) { label = l; timestamp = t; }
            public test_tbl1() { }
        };

        static void Main(string[] args)
        {
            /* Investigate problem with array update */
            var tb = Nett.Toml.Create();
            var tList = new List<test_tbl1>();
            tList.Add(new test_tbl1("first", DateTimeOffset.Now));
            tb.Add("test", tList);
            tList.Add(new test_tbl1("second", DateTimeOffset.MinValue));
            tb.Update("test", tList);
        }
    }
}

I get the following run-time exception:

Unhandled Exception: System.InvalidCastException: Unable to cast object of type 'Nett.TomlTableArray' to type 'Nett.TomlTable'.
   at Nett.TomlTable.CreateFromClass[T](ITomlRoot root, T obj, TableTypes tableType)
   at Nett.TomlObjectFactory.Update(TomlTable table, String key, Object obj, TableTypes type)
   at ConsoleApp_TOML.Program.Main(String[] args) in C:\Users\jcg\source\repos\ConsoleApp_TOML\ConsoleApp_TOML\Program.cs:line 26
Press any key to continue . . .

The same thing happens when using a C# array instead of a list:

            var tb = Nett.Toml.Create();
            var tArray = new test_tbl1[] {
                new test_tbl1("first", DateTimeOffset.Now),
                new test_tbl1("second", DateTimeOffset.MinValue)
            };
            tb.Add("test", tArray);
            tArray[1].label = "modified";
            tb.Update("test", tArray);

I have worked around by replacing the Update() with a Remove() followed by Add(), and this works OK but has the annoying side-effect of re-ordering the keys in the table. #

@paiden paiden self-assigned this Mar 11, 2018
@paiden paiden added the bug label Mar 11, 2018
@paiden paiden added this to the 0.9.0 milestone Mar 11, 2018
@paiden
Copy link
Owner

paiden commented Mar 11, 2018

This is because there is no overload of update for the TableArray case. Somehow I always miss them.

So the compiler falls back to the best matching overload that is the table overload with argument type object. As a IEnumerable cannot be converted to a TomlTable an exception is thrown.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants