Code Monkey home page Code Monkey logo

Comments (3)

MarkPflug avatar MarkPflug commented on June 19, 2024

Looks like there is a bug in the Sylvan.Data binder when dealing with the new DateOnly/TimeOnly types. I've got a fix for that ready, and will push it shortly as 0.2.9.

However, there will also be a modification that needs to be made to your code. The issue is that the CsvDataReader doesn't "know" that any of the columns are a specific type. There are two ways to fix this.

One option is to specifically tell the CsvDataReader the types of the columns, by providing a "schema". Here is some test code demonstrating how to define the schema:

var data = "Name,Date\na,\"24/12/2022\"\nb,\"25/12/2022\"\n";

var schema =
	new Schema.Builder()
	.Add<string>("Name")
	.Add<DateOnly>("Date")
	// etc
	.Build();

var opts = new CsvDataReaderOptions
{
	DateOnlyFormat = "dd/MM/yyyy",
	Schema = new CsvSchema(schema)
};

using var csv = CsvDataReader.Create(new StringReader(data), opts);
var records = csv.GetRecords<DateOnlyRecord>().ToList();
Assert.Equal(new DateOnly(2022, 12, 24), records[0].Date);
Assert.Equal(new DateOnly(2022, 12, 25), records[1].Date);

The other option is to tell the databinder to use the type of the property being bound to identify the types of the columns in the source data. The GetRecords<T> method doesn't enable this by default, but it is easy to create your own GetRecords extension method that does:

public static IEnumerable<T> GetRecords<T>(this DbDataReader data)
	where T : class, new()
{
	var opts = new DataBinderOptions { 
		// tells the binder to use the member type
		// to determine the appropriate data accessor
		InferColumnTypeFromMember = true 
	};
	var binder = DataBinder.Create<T>(data, opts);
	while (data.Read())
	{
		var record = new T();
		binder.Bind(data, record);
		yield return record;
	}
}

I'll post an update when the package is published.

from sylvan.

MarkPflug avatar MarkPflug commented on June 19, 2024

Okay, 0.2.9 has been published. It might take a few minutes to show up on nuget.

from sylvan.

zejji avatar zejji commented on June 19, 2024

Many thanks for the reply, that makes perfect sense

from sylvan.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.