Compare commits

...
Sign in to create a new pull request.

4 commits

Author SHA1 Message Date
0b2e178066 🐛 Only trim bang on out 2024-10-31 15:09:42 -05:00
a8cbfd087f Allow value prefixed with bang to skip parsing 2024-10-31 14:31:16 -05:00
d0b6c40445 🔀 Merge branch 'stable' into develop 2024-10-09 15:19:06 -05:00
379819dde5 🔀 Merge tag 'v0.0.6' into develop
⬆️ Upgrade go-dateparser to new version
2024-10-07 15:55:31 -05:00
4 changed files with 23 additions and 0 deletions

View file

@ -17,6 +17,10 @@ func ParseString(in string) any {
return s return s
} }
if strings.HasPrefix(s, "!") {
return s
}
yesno := regexp.MustCompile("^(y|Y|yes|Yes|YES|n|N|no|No|NO|true|True|TRUE|false|False|FALSE|on|On|ON|off|Off|OFF)$") yesno := regexp.MustCompile("^(y|Y|yes|Yes|YES|n|N|no|No|NO|true|True|TRUE|false|False|FALSE|on|On|ON|off|Off|OFF)$")
yes := regexp.MustCompile("^(y|Y|yes|Yes|YES|true|True|TRUE|on|On|ON)$") yes := regexp.MustCompile("^(y|Y|yes|Yes|YES|true|True|TRUE|on|On|ON)$")
null := regexp.MustCompile("^(~|null|Null|NULL|none|None|NONE|nil|Nil|NIL)$") null := regexp.MustCompile("^(~|null|Null|NULL|none|None|NONE|nil|Nil|NIL)$")

View file

@ -38,6 +38,10 @@ func TestParse(t *testing.T) {
{"on-value", "on", true}, {"on-value", "on", true},
{"no-value", "no", false}, {"no-value", "no", false},
{"off-value", "off", false}, {"off-value", "off", false},
{"skip-parsing-num", "!42", "!42"},
{"skip-parsing-bool", "!false", "!false"},
{"skip-parsing-time", "!" + when.Format(time.RFC3339), "!" + when.Format(time.RFC3339)},
{"skip-parsing-duration", "!15 mins", "!15 mins"},
} }
for _, tt := range tests { for _, tt := range tests {

View file

@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"strconv" "strconv"
"strings"
"time" "time"
) )
@ -27,6 +28,9 @@ func WriteValue(buff *bytes.Buffer, val any) (n int, err error) {
return buff.Write(o) return buff.Write(o)
} }
case string: case string:
if strings.HasPrefix(v, "!") {
return buff.WriteString(strings.TrimPrefix(v, "!"))
}
return buff.WriteString(v) return buff.WriteString(v)
case int: case int:
return buff.WriteString(strconv.Itoa(v)) return buff.WriteString(strconv.Itoa(v))
@ -39,6 +43,9 @@ func WriteValue(buff *bytes.Buffer, val any) (n int, err error) {
case json.RawMessage: case json.RawMessage:
return buff.Write(v) return buff.Write(v)
case []byte: case []byte:
if v[0] == '!' {
return buff.Write(v[1:])
}
return buff.Write(v) return buff.Write(v)
case byte: case byte:
err = buff.WriteByte(v) err = buff.WriteByte(v)

View file

@ -33,6 +33,14 @@ func TestWriteBuffer(t *testing.T) {
{"slice", []any{1, 2, "foo"}, `[1,2,"foo"]`, nil}, {"slice", []any{1, 2, "foo"}, `[1,2,"foo"]`, nil},
{"map", map[string]any{"baz": 42, "foo": "bar"}, `{"baz":42,"foo":"bar"}`, nil}, {"map", map[string]any{"baz": 42, "foo": "bar"}, `{"baz":42,"foo":"bar"}`, nil},
{"struct", struct{}{}, "", errors.New("Unsupported type struct {}")}, {"struct", struct{}{}, "", errors.New("Unsupported type struct {}")},
{"skip-bang-num", "!42", "42", nil},
{"skip-bang-bool", "!false", "false", nil},
{"skip-bang-time", "!" + when.Format(time.RFC3339), when.Format(time.RFC3339), nil},
{"skip-bang-duration", "!15 mins", "15 mins", nil},
{"skip-bang-bytes-num", []byte("!42"), "42", nil},
{"skip-bang-bytes-bool", []byte("!false"), "false", nil},
{"skip-bang-bytes-time", []byte("!" + when.Format(time.RFC3339)), when.Format(time.RFC3339), nil},
{"skip-bang-bytes-duration", []byte("!15 mins"), "15 mins", nil},
} }
for _, tt := range tests { for _, tt := range tests {