InfluxCommunity / influxdb3-go

The go package that provides a simple and convenient way to interact with InfluxDB 3.
https://pkg.go.dev/github.com/InfluxCommunity/influxdb3-go
MIT License
21 stars 11 forks source link

write: a collection of generic interfaces #62

Closed karel-rehor closed 2 months ago

karel-rehor commented 5 months ago

Use Case

Currently the Write API has the methods.

func (c *Client) WriteData(ctx context.Context, points ...interface{}) error { ... }

and

func (c *Client) WriteDataWithOptions(ctx context.Context, options *WriteOptions, points ...interface{}) error {...}

It would also be useful to be able to write batches/collections of a generic interface type.

Expected behavior

When I've a collection of objects of a local type, I would like to write them in one call using the points ...inteface{} idiom.

Actual behavior

Currently this requires repeated calls, in a loop to one of the methods mentioned under Use Case

Additional info

I've made a simple sequential implementation of this locally.

func WriteBatchDataSeq[T any](client *influxdb3.Client,
    ctx context.Context,
    opts *influxdb3.WriteOptions,
    ifaces []T) error {

    for _, iface := range ifaces {
        fmt.Printf("%s %v\n", reflect.TypeOf(iface).Kind().String(), iface)
        err := client.WriteDataWithOptions(ctx, opts, iface)
        if err != nil {
            return err
        }
    }
    return nil
}
karel-rehor commented 4 months ago

Only as a reminder

werr := client.WriteDataWithOptions(context.Background(), &wopts, []interface{})

... throws error like...

error encoding point: cannot use []main.Nautilus as point

and

werr := client.WriteDataWithOptions(context.Background(), &wopts, []*interface{})

...throws error like...

error encoding point: cannot use []*main.Nautilus as point
thulasirajkomminar commented 3 months ago

Any update on this? Would love to have a batch write option.

bednar commented 3 months ago

@thulasirajkomminar thanks for clarify your interesting, we will prioritise this issue.

Is this something you would be willing to help with? All PR is welcome and we will be happy to review your submission.

thulasirajkomminar commented 3 months ago

@thulasirajkomminar thanks for clarify your interesting, we will prioritise this issue.

Is this something you would be willing to help with? All PR is welcome and we will be happy to review your submission.

I think @karel-rehor already made some commits for this https://github.com/InfluxCommunity/influxdb3-go/compare/feat/writeIfaceArray

alespour commented 3 months ago

User can use client.WriteData(ctx, points...) when the collection of annotated structs is a []interface{} slice. If there is a need to keep the points in strongly typed slice ([]T), conversion to slice of interfaces is straightforward.

I'm for closing this with wontfix label. Other opinions?

bednar commented 3 months ago

User can use client.WriteData(ctx, points...) when the collection of annotated structs is a []interface{} slice. If there is a need to keep the points in strongly typed slice ([]T), conversion to slice of interfaces is straightforward.

@alespour nice hint 👍

I'm for closing this with wontfix label. Other opinions?

I agree

thulasirajkomminar commented 2 months ago

@bednar @alespour I created my own function to convert my custom slice to slice of any so I think the issue can be closed.