Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
33 changes: 0 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -467,39 +467,6 @@ func main() {
- Call `SetDefaults()` on any field whose pointer type implements it
- Recurse into nested structs, maps, and slices

### `types` - Custom Types

#### `Duration`

A duration type that works seamlessly with TOML, YAML, JSON, and plain integer values (interpreted as seconds).

```go
package main

import (
"fmt"

"github.com/tinyauthapp/paerser/types"
)

func main() {
var d types.Duration

d.Set("30s") // 30 seconds
d.Set("5m30s") // 5 minutes and 30 seconds
d.Set("120") // 120 seconds (suffix-less integers are treated as seconds)

fmt.Println(d.String()) // "2m0s"
}
```

It implements `encoding.TextMarshaler`, `encoding.TextUnmarshaler`, `json.Marshaler`, and `json.Unmarshaler`, so it works out of the box in configuration files:

```yaml
timeout: 30s
interval: 120
```

### `parser` - Low-Level Parsing Engine

The `parser` package is the foundation that all other packages build on. It provides a tree-based intermediate representation (`Node`) for configuration data, along with encoding/decoding utilities.
Expand Down
26 changes: 0 additions & 26 deletions flag/flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/stretchr/testify/require"
"github.com/tinyauthapp/paerser/generator"
"github.com/tinyauthapp/paerser/parser"
"github.com/tinyauthapp/paerser/types"
)

func TestDecode(t *testing.T) {
Expand All @@ -23,18 +22,6 @@ func TestDecode(t *testing.T) {
args: nil,
expected: nil,
},
{
desc: "types.Duration value",
args: []string{"--foo=1"},
element: &struct {
Foo types.Duration
}{},
expected: &struct {
Foo types.Duration
}{
Foo: types.Duration(1 * time.Second),
},
},
{
desc: "time.Duration value",
args: []string{"--foo=1"},
Expand Down Expand Up @@ -905,19 +892,6 @@ func TestEncode(t *testing.T) {
Default: "1s",
}},
},
{
desc: "time duration field",
element: &struct {
Field types.Duration `description:"field description"`
}{
Field: types.Duration(180 * time.Second),
},
expected: []parser.Flat{{
Name: "field",
Description: "field description",
Default: "180",
}},
},
{
desc: "slice of struct",
element: &struct {
Expand Down
4 changes: 0 additions & 4 deletions parser/element_fill.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"strconv"
"strings"
"time"

"github.com/tinyauthapp/paerser/types"
)

const defaultRawSliceSeparator = ","
Expand Down Expand Up @@ -352,8 +350,6 @@ func (f filler) setMap(field reflect.Value, node *Node) error {

func setInt(field reflect.Value, value string, bitSize int) error {
switch field.Type() {
case reflect.TypeOf(types.Duration(0)):
return setDuration(field, value, bitSize, time.Second)
case reflect.TypeOf(time.Duration(0)):
return setDuration(field, value, bitSize, time.Nanosecond)
default:
Expand Down
25 changes: 0 additions & 25 deletions parser/element_fill_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tinyauthapp/paerser/types"
)

func TestFill(t *testing.T) {
Expand Down Expand Up @@ -390,30 +389,6 @@ func TestFill(t *testing.T) {
element: &struct{ Foo time.Duration }{},
expected: expected{element: &struct{ Foo time.Duration }{Foo: 4 * time.Nanosecond}},
},
{
desc: "types.Duration with unit",
node: &Node{
Name: "traefik",
Kind: reflect.Struct,
Children: []*Node{
{Name: "Foo", FieldName: "Foo", Value: "4s", Kind: reflect.Int64},
},
},
element: &struct{ Foo types.Duration }{},
expected: expected{element: &struct{ Foo types.Duration }{Foo: types.Duration(4 * time.Second)}},
},
{
desc: "types.Duration without unit",
node: &Node{
Name: "traefik",
Kind: reflect.Struct,
Children: []*Node{
{Name: "Foo", FieldName: "Foo", Value: "4", Kind: reflect.Int64},
},
},
element: &struct{ Foo types.Duration }{},
expected: expected{element: &struct{ Foo types.Duration }{Foo: types.Duration(4 * time.Second)}},
},
{
desc: "bool",
node: &Node{
Expand Down
4 changes: 0 additions & 4 deletions parser/flat_encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"strconv"
"strings"
"time"

"github.com/tinyauthapp/paerser/types"
)

const defaultPtrValue = "false"
Expand Down Expand Up @@ -143,8 +141,6 @@ func (e encoderToFlat) getNodeValue(field reflect.Value, node *Node) string {
i, _ := strconv.ParseInt(node.Value, 10, 64)

switch field.Type() {
case reflect.TypeOf(types.Duration(time.Second)):
return strconv.Itoa(int(i) / int(time.Second))
case reflect.TypeOf(time.Second):
return time.Duration(i).String()
}
Expand Down
29 changes: 0 additions & 29 deletions parser/flat_encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tinyauthapp/paerser/types"
)

func TestEncodeToFlat(t *testing.T) {
Expand Down Expand Up @@ -1121,34 +1120,6 @@ func TestEncodeToFlat(t *testing.T) {
Default: "1s",
}},
},
{
desc: "time duration field",
element: &struct {
Field types.Duration `description:"field description"`
}{
Field: types.Duration(180 * time.Second),
},
node: &Node{
Name: "traefik",
FieldName: "",
Kind: reflect.Struct,
Children: []*Node{
{
Name: "Field",
Description: "field description",
FieldName: "Field",
Value: "180000000000",
Kind: reflect.Int64,
Tag: `description:"field description"`,
},
},
},
expected: []Flat{{
Name: "field",
Description: "field description",
Default: "180",
}},
},
{
desc: "slice of struct",
element: &struct {
Expand Down
62 changes: 0 additions & 62 deletions types/duration.go

This file was deleted.

Loading