This commit is contained in:
@@ -2,13 +2,14 @@ package arg
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/value"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
const Name = "arg"
|
||||
@@ -117,13 +118,15 @@ func (p *Provider) Value(ctx context.Context, path ...string) (config.Value, err
|
||||
case len(val) == 1:
|
||||
return value.JString(val[0]), nil
|
||||
default:
|
||||
var yNode yaml.Node
|
||||
|
||||
if err := yaml.Unmarshal([]byte("["+strings.Join(val, ",")+"]"), &yNode); err != nil {
|
||||
return nil, fmt.Errorf("arg: failed unmarshal yaml:%w", err)
|
||||
data, jerr := json.Marshal(val)
|
||||
if jerr != nil {
|
||||
return nil, fmt.Errorf("failed load data:%w", jerr)
|
||||
}
|
||||
|
||||
return value.Decode(yNode.Decode), nil
|
||||
return value.Decode(func(v interface{}) error {
|
||||
log.Println(string(data))
|
||||
return json.Unmarshal(data, v)
|
||||
}), nil
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package arg_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -35,7 +37,7 @@ func TestProvider(t *testing.T) {
|
||||
test.NewRead("config.hcl", "config"),
|
||||
test.NewRead(test.Time("2010-01-02T15:04:05Z"), "start-at"),
|
||||
test.NewReadUnmarshal(&[]string{"http://4devs.io", "https://4devs.io"}, &[]string{}, "url"),
|
||||
test.NewReadUnmarshal(&[]time.Duration{time.Minute, time.Hour}, &[]time.Duration{}, "timeout"),
|
||||
test.NewReadUnmarshal(&[]Duration{{time.Minute}, {time.Hour}}, &[]Duration{}, "timeout"),
|
||||
test.NewReadUnmarshal(&[]time.Time{
|
||||
test.Time("2009-01-02T15:04:05Z"),
|
||||
test.Time("2008-01-02T15:04:05+03:00"),
|
||||
@@ -46,3 +48,21 @@ func TestProvider(t *testing.T) {
|
||||
|
||||
test.Run(t, prov, read)
|
||||
}
|
||||
|
||||
type Duration struct {
|
||||
time.Duration
|
||||
}
|
||||
|
||||
func (d *Duration) UnmarshalJSON(in []byte) error {
|
||||
o, err := time.ParseDuration(strings.Trim(string(in), `"`))
|
||||
if err != nil {
|
||||
return fmt.Errorf("parse:%w", err)
|
||||
}
|
||||
d.Duration = o
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Duration) MarshalJSON() ([]byte, error) {
|
||||
return []byte(fmt.Sprintf("%q", d)), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user