Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dcarbone/go-strdur
An implementation of time.Duration with prettier serialization and stuff
https://github.com/dcarbone/go-strdur
go golang hcl hcl2
Last synced: 12 days ago
JSON representation
An implementation of time.Duration with prettier serialization and stuff
- Host: GitHub
- URL: https://github.com/dcarbone/go-strdur
- Owner: dcarbone
- License: mit
- Created: 2020-11-13T14:44:42.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-06-24T23:24:34.000Z (5 months ago)
- Last Synced: 2024-06-25T00:32:42.960Z (5 months ago)
- Topics: go, golang, hcl, hcl2
- Language: Go
- Homepage:
- Size: 38.1 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# go-strdur
[![Tests](https://github.com/dcarbone/go-strdur/actions/workflows/tests.yaml/badge.svg)](https://github.com/dcarbone/go-strdur/actions/workflows/tests.yaml)
A highly inefficient implementation of `time.Duration` based on a string, rather than an int64. It is designed purely
soo that hcl configuration files can contain a string representation of a duration for config values, rather than an
int64.# Installation
```shell
go get -u github.com/dcarbone/go-strdur/v2
```## Example
```go
package mainimport (
"fmt"
"github.com/dcarbone/go-strdur/v2"
"github.com/hashicorp/hcl/v2/hclsimple"
)const exampleConfig = `
duration_value = "24h"
`type MyConfig struct {
DurationValue strdur.StringDuration `hcl:"duration_value"`
}func main() {
myCnf := new(MyConfig)
if err := hclsimple.Decode("example.hcl", []byte(exampleConfig), nil, new(MyConfig)); err != nil {
panic(fmt.Sprintf("Error decoding hcl: %v", err))
}
fmt.Println(myCnf.DurationValue.String())
}
```## Explanation
I created this type specifically because, as of the time of this writing, https://github.com/hashicorp/hcl does not have
a great way to handle embedded types.Given this example:
```go
package mainimport (
"fmt"
"time""github.com/hashicorp/hcl/v2/hclsimple"
)const confValue = `
duration_value = "24h"
`type MyDuration time.Duration
type MyConfig struct {
DurationValue MyDuration `hcl:"duration_value"`
}func main() {
if err := hclsimple.Decode("example.hcl", []byte(confValue), nil, new(MyConfig)); err != nil {
panic(fmt.Sprintf("Error decoding hcl: %v", err))
}
}
```The above will always fail. Its possible I am missing something in the
[cty](https://pkg.go.dev/github.com/zclconf/go-cty/cty) package which can handle this, but I haven't been able to find
it.I offer zero guarantees of performance on this type as it is intended entirely to be used as a value in a config object.