Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: type-safe conversion for line-protocol Addfield #43 #42

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: type-safe conversion for line-protocol
Signed-off-by: junya koyama <[email protected]>
arukiidou committed Oct 26, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit e8baddf32a89977bb52119b6410417bef16c867e
20 changes: 20 additions & 0 deletions influxdb3/point.go
Original file line number Diff line number Diff line change
@@ -155,6 +155,26 @@ func (m *Point) AddField(k string, v interface{}) *Point {
return m
}

// AddFieldFromValue adds a [github.com/influxdata/line-protocol/v2/lineprotocol.Value] to the Point.
//
// Parameters:
// - k: The key of the field.
// - v: The value of the line protocol format.
//
// Returns:
// - The updated Point with the field added.
func (m *Point) AddFieldFromValue(k string, v lineprotocol.Value) *Point {
for i, field := range m.Fields {
if k == field.Key {
m.Fields[i].Value = v
return m
}
}

m.Fields = append(m.Fields, Field{Key: k, Value: v})
return m
}

// AddField adds a field to the Point.
//
// Parameters:
194 changes: 194 additions & 0 deletions influxdb3/value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
/*
The MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package influxdb3

import (
"fmt"
"time"

"github.com/influxdata/line-protocol/v2/lineprotocol"
)

// NativeType are unions of type sets that can converted to [lineprotocol.NewValue].
//
// [lineprotocol.NewValue]: https://pkg.go.dev/github.com/influxdata/line-protocol/v2/lineprotocol#NewValue
//
// [line-protocol]: https://docs.influxdata.com/influxdb/cloud-serverless/reference/syntax/line-protocol/#field-set
type NativeType interface {
float64 | int64 | uint64 | string | []byte | bool
}

// [Float] is IEEE-754 64-bit floating-point numbers. Default numerical type. InfluxDB supports scientific notation in float field values.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add Float as a prefix to docs:

Suggested change
// [Float] is IEEE-754 64-bit floating-point numbers. Default numerical type. InfluxDB supports scientific notation in float field values.
// Float [Float] is IEEE-754 64-bit floating-point numbers. Default numerical type. InfluxDB supports scientific notation in float field values.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

// Non-finite floating-point field values (+/- infinity and NaN from IEEE 754) are not currently supported.
//
// [Float]: https://docs.influxdata.com/influxdb/cloud-serverless/reference/syntax/line-protocol/#float
type Float interface {
~float32 | ~float64
}

// [Integer] is signed 64-bit integers.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add Integer as a prefix to docs:

Suggested change
// [Integer] is signed 64-bit integers.
// Integer [Integer] is signed 64-bit integers.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

//
// [Integer]: https://docs.influxdata.com/influxdb/cloud-serverless/reference/syntax/line-protocol/#integer
type Integer interface {
~int | ~int8 | ~int16 | ~int32 | ~int64
}

// [UInteger] is unsigned 64-bit integers.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add UInteger as a prefix to docs:

Suggested change
// [UInteger] is unsigned 64-bit integers.
// UInteger [UInteger] is unsigned 64-bit integers.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

//
// [UInteger]: https://docs.influxdata.com/influxdb/cloud-serverless/reference/syntax/line-protocol/#uinteger
type UInteger interface {
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}

// [String] is plain text string. Length limit 64KB.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add String as a prefix to docs:

Suggested change
// [String] is plain text string. Length limit 64KB.
// String [String] is plain text string. Length limit 64KB.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

//
// [String]: https://docs.influxdata.com/influxdb/cloud-serverless/reference/syntax/line-protocol/#string
type String interface {
~string | ~[]byte
}

// [Boolean] is true or false values.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add Boolean as a prefix to docs:

Suggested change
// [Boolean] is true or false values.
// Boolean [Boolean] is true or false values.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

//
// [Boolean]: https://docs.influxdata.com/influxdb/cloud-serverless/reference/syntax/line-protocol/#boolean
type Boolean interface {
~bool
}

// NewValueFromNative is a convenient function for creating a [lineprotocol.Value] from NativeType.
//
// Parameters:
// - v: The value of the field value.
//
// Returns:
// - The created [lineprotocol.Value].
//
// [lineprotocol.Value]: https://pkg.go.dev/github.com/influxdata/line-protocol/v2/lineprotocol#Value
func NewValueFromNative[N NativeType](v N) lineprotocol.Value {
return lineprotocol.MustNewValue(v)
}

// NewValueFromFloat is a convenient function for creating a [lineprotocol.Value] from Float.
//
// Parameters:
// - v: The value of the Float value.
//
// Returns:
// - The created [lineprotocol.Value].
//
// [lineprotocol.Value]: https://pkg.go.dev/github.com/influxdata/line-protocol/v2/lineprotocol#Value
func NewValueFromFloat[F Float](v F) lineprotocol.Value {
data, ok := lineprotocol.FloatValue(float64(v))
if !ok {
panic(fmt.Errorf("invalid float value for NewValueFromFloat: %T (%#v)", v, v))
}
return data
}

// NewValueFromInt is a convenient function for creating a [lineprotocol.Value] from Integer.
//
// Parameters:
// - v: The value of the Integer value.
//
// Returns:
// - The created [lineprotocol.Value].
//
// [lineprotocol.Value]: https://pkg.go.dev/github.com/influxdata/line-protocol/v2/lineprotocol#Value
func NewValueFromInt[I Integer](v I) lineprotocol.Value {
return lineprotocol.IntValue(int64(v))
}

// NewValueFromUInt is a convenient function for creating a [lineprotocol.Value] from UInteger.
//
// Parameters:
// - v: The value of the UInteger value.
//
// Returns:
// - The created [lineprotocol.Value].
//
// [lineprotocol.Value]: https://pkg.go.dev/github.com/influxdata/line-protocol/v2/lineprotocol#Value
func NewValueFromUInt[U UInteger](v U) lineprotocol.Value {
return lineprotocol.UintValue(uint64(v))
}

// NewValueFromString is a convenient function for creating a [lineprotocol.Value] from String.
//
// Parameters:
// - v: The value of the String value.
//
// Returns:
// - The created [lineprotocol.Value].
//
// [lineprotocol.Value]: https://pkg.go.dev/github.com/influxdata/line-protocol/v2/lineprotocol#Value
func NewValueFromString[S String](v S) lineprotocol.Value {
data, ok := lineprotocol.StringValue(string(v))
if !ok {
panic(fmt.Errorf("invalid utf-8 string value for NewValueFromString: %T (%#v)", v, v))
}
return data
}

// NewValueFromUInt is a convenient function for creating a [lineprotocol.Value] from [fmt.Stringer].
//
// Examples:
//
// func example() {
// p := NewPoint("measurement", map[string]string{}, map[string]interface{}{}, time.Now())
// p.AddFieldFromValue("supports time.Duration", NewValueFromStringer(4*time.Hour))
// }
//
// Parameters:
// - v: The value of the UInteger value.
//
// Returns:
// - The created [lineprotocol.Value].
//
// [lineprotocol.Value]: https://pkg.go.dev/github.com/influxdata/line-protocol/v2/lineprotocol#Value
func NewValueFromStringer[S fmt.Stringer](v S) lineprotocol.Value {
return NewValueFromString(v.String())
}

// NewValueFromBoolean is a convenient function for creating a [lineprotocol.Value] from Boolean.
//
// Parameters:
// - v: The value of the Boolean value.
//
// Returns:
// - The created [lineprotocol.Value].
//
// [lineprotocol.Value]: https://pkg.go.dev/github.com/influxdata/line-protocol/v2/lineprotocol#Value
func NewValueFromBoolean[B Boolean](v B) lineprotocol.Value {
return lineprotocol.BoolValue(bool(v))
}

// NewValueFromBoolean is a convenient function for creating a [lineprotocol.Value] from [time.Time].
//
// Parameters:
// - v: The value of the [time.Time] value.
//
// Returns:
// - The created [lineprotocol.Value].
//
// [lineprotocol.Value]: https://pkg.go.dev/github.com/influxdata/line-protocol/v2/lineprotocol#Value
func NewValueFromTime(v time.Time) lineprotocol.Value {
return NewValueFromString(v.Format(time.RFC3339Nano))
}