-
Notifications
You must be signed in to change notification settings - Fork 14
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
Changes from 1 commit
e8baddf
f851192
85f69f1
7b253af
29e2d5d
5ffea2d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Signed-off-by: junya koyama <[email protected]>
There are no files selected for viewing
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. | ||||||
// 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. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) | ||||||
} |
There was a problem hiding this comment.
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:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed.