Skip to content

Commit

Permalink
Add duration format data converter
Browse files Browse the repository at this point in the history
  • Loading branch information
arberkatellari authored and danbogos committed Nov 15, 2023
1 parent 6161d33 commit e32ebe8
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions utils/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,7 @@ const (
ResourceUsage = "ResourceUsage"
MetaStrip = "*strip"
MetaDuration = "*duration"
MetaDurationFormat = "*durfmt"
MetaLibPhoneNumber = "*libphonenumber"
MetaTimeString = "*time_string"
MetaIP2Hex = "*ip2hex"
Expand Down
26 changes: 26 additions & 0 deletions utils/dataconverter.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ func NewDataConverter(params string) (conv DataConverter, err error) {
return new(JSONConverter), nil
case params == MetaDuration:
return NewDurationConverter(EmptyString)
case strings.HasPrefix(params, MetaDurationFormat):
return NewDurationFormatConverter(params[len(MetaDurationFormat)+1:])
case params == MetaIP2Hex:
return new(IP2HexConverter), nil
case params == MetaString2Hex:
Expand Down Expand Up @@ -274,6 +276,30 @@ func (mS *DurationConverter) Convert(in any) (
return IfaceAsDuration(in)
}

func NewDurationFormatConverter(params string) (hdlr DataConverter, err error) {
return &DurationFormatConverter{Layout: params}, nil
}

// DurationFormatConverter formats duration in the same way Time is formatted as string
type DurationFormatConverter struct {
Layout string
}

func (dfc *DurationFormatConverter) Convert(in any) (
out any, err error) {
z := time.Unix(0, 0).UTC()
dur, err := IfaceAsDuration(in)
if err != nil {
return nil, err
}
if dfc.Layout == EmptyString {
out = z.Add(time.Duration(dur)).Format("15:04:05")
return
}
out = z.Add(time.Duration(dur)).Format(dfc.Layout)
return
}

// NewPhoneNumberConverter create a new phoneNumber converter
// If the format isn't specify by default we use NATIONAL
// Possible fromats are : E164(0) , INTERNATIONAL(1) , NATIONAL(2) ,RFC3966(3)
Expand Down
43 changes: 43 additions & 0 deletions utils/dataconverter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,42 @@ func TestDataConvertersConvertString(t *testing.T) {
}
}

func TestConvertDurationFormat1(t *testing.T) {
dcs := &DataConverters{
&DurationFormatConverter{
Layout: "15:04:05",
},
}
if rcv, err := dcs.ConvertString("15m"); err != nil {
t.Error(err)
} else if rcv != "00:15:00" {
t.Errorf("Expecting: <%+q>, received: <%+q>", "00:15:00", rcv)
}
}
func TestConvertDurationFormat2(t *testing.T) {
dcs := &DataConverters{
&DurationFormatConverter{
Layout: "15-04-05.999999999",
},
}
if rcv, err := dcs.ConvertString("20s423ns"); err != nil {
t.Error(err)
} else if rcv != "00-00-20.000000423" {
t.Errorf("Expecting: <%+q>, received: <%+q>", "00-00-20.000000423", rcv)
}
}

func TestConvertDurationFormatDefault(t *testing.T) {
dcs := &DataConverters{
&DurationFormatConverter{},
}
if rcv, err := dcs.ConvertString("15m"); err != nil {
t.Error(err)
} else if rcv != "00:15:00" {
t.Errorf("Expecting: <%+q>, received: <%+q>", "00:15:00", rcv)
}
}

func TestNewDataConverter(t *testing.T) {
a, err := NewDataConverter(MetaDurationSeconds)
if err != nil {
Expand Down Expand Up @@ -169,6 +205,13 @@ func TestNewDataConverter(t *testing.T) {
if !reflect.DeepEqual(tm, expTime) {
t.Errorf("Expected %+v received: %+v", expTime, tm)
}
expected := &DurationFormatConverter{Layout: "15:04:05"}
if durFmt, err := NewDataConverter(MetaDurationFormat + ":15:04:05"); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(durFmt, expected) {
t.Errorf("Expected %+v received: %+v", expected, durFmt)
}

}

func TestNewDataConverterMustCompile(t *testing.T) {
Expand Down

0 comments on commit e32ebe8

Please sign in to comment.