tomasfabian / ksqlDB.RestApi.Client-DotNet

ksqlDb.RestApi.Client is a C# LINQ-enabled client API for issuing and consuming ksqlDB push and pull queries and executing statements.
MIT License
93 stars 24 forks source link

JsonPropertyNameAttribute support #27

Closed KovtunV closed 1 year ago

KovtunV commented 1 year ago

Hello, is it possible to add this attribute support when describing property names?

For example,

class Model
{
    [JsonPropertyName("data_id")]
    public string DataId { get; set; }
}

Data in topic contains property data_id, but you generate from property name DATAID CREATE STREAM EVENTS (DATAID STRING)

As a result I don't get data image

I expect: if there is JsonPropertyNameAttribute then your query will be like this CREATE STREAM EVENTS (DATA_ID STRING)

tomasfabian commented 1 year ago

Hi @KovtunV , I added support for JsonPropertyNameAttributes during Stream or Table generation.

Could you verify it with the release candidate 2.2.0-rc.1 I've just published, please?

Regards Tomas.

KovtunV commented 1 year ago

@tomasfabian thank you! It works

KovtunV commented 1 year ago

@tomasfabian Hi, I got an error on select SELECT column 'DATATIMESTAMP' cannot be resolved.

c# class is

public sealed class TestModel
{
    [JsonPropertyName("data_timestamp")]
    public string DataTimestamp { get; set; }
}

request is

 context.CreateQueryStream<TestModel>("tests")
            .Select(x => x.DataTimestamp)
            .Subscribe(x =>
            {

            },
            error => Console.WriteLine($"Exception: {error.Message}"),
            () => Console.WriteLine("Completed"));
tomasfabian commented 1 year ago

Hi @KovtunV, I published a new version ksqlDB.RestApi.Client 2.2.1 which adds support to use JsonPropertyAttribute for selects.

KovtunV commented 1 year ago

Hi @tomasfabian, but that doesn't work for join

tomasfabian commented 1 year ago

Hi @KovtunV, could you provide an example, please?

Do you mean in the ON part?

ON a.DataId = b.DataId;
KovtunV commented 1 year ago

@tomasfabian apparently in select part, moreover it generates strange query:

  var stream = context
            .CreateQueryStream<EventModel>(EndusersStreamName)
            .Join(Source.Of<EventModel>(BankingTransactionsStreamName).Within(Duration.OfDays(1)),
            endusers => K.Functions.ExtractJsonField(endusers.Data, "$.customer_id"),
            transactions => K.Functions.ExtractJsonField(transactions.Data, "$.customer_id"),
            (endusers, transactions) => new
            {
                EnduserId = endusers.DataId,
                TransactionsId = transactions.DataId,
                CustomerId = K.Functions.ExtractJsonField(endusers.Data, "$.customer_id"),
                EndusersData = endusers.Data,
                TransactionsData = transactions.Data
            });

DataId field with attribute  [JsonPropertyName("data_id")]

SELECT endusers.DataId AS EnduserId, transactions.DataId AS TransactionsId, EXTRACTJSONFIELD(endusers.data, '$.customer_id') CustomerId, endusers.Data AS EndusersData, transactions.Data AS TransactionsData FROM endusers_stream transactions INNER JOIN banking_transactions_stream transactions WITHIN 1 DAYS ON EXTRACTJSONFIELD(endusers.data, '$.customer_id') = EXTRACTJSONFIELD(transactions.data, '$.customer_id') EMIT CHANGES;

from alias with wrong transactions name instead of endusers and DataId isn't from attribute

tomasfabian commented 1 year ago

Hi @KovtunV, please try out version 2.3.0-rc.3. I added support for selecting fields with the attribute within anonymous objects and aliasing for fromItems of the same type with the Join extension method.

KovtunV commented 1 year ago

@tomasfabian Thank you :)