From 7d1208c84b1ff5c81e4fa03e295b546456286e48 Mon Sep 17 00:00:00 2001 From: Yousuf Jawwad Date: Mon, 25 Sep 2023 03:34:40 +0500 Subject: [PATCH] feat(workflows): ignore uuid strings when normalizing id strings (#8) --- workflows/options.go | 45 +++++++++++++------------------------------- workflows/utils.go | 24 +++++++++++++++++++++++ 2 files changed, 37 insertions(+), 32 deletions(-) create mode 100644 workflows/utils.go diff --git a/workflows/options.go b/workflows/options.go index fe6dd21..d4cb2b7 100644 --- a/workflows/options.go +++ b/workflows/options.go @@ -22,7 +22,6 @@ package workflows import ( "strings" - "github.com/gobeam/stringy" "go.temporal.io/sdk/workflow" ) @@ -107,16 +106,13 @@ func WithParent(parent workflow.Context) Option { } // WithBlock sets the block name. -func WithBlock(block string) Option { +func WithBlock(val string) Option { return func(o Options) error { if o.(*options).block != "" { - return NewDuplicateIDPropError("block", o.(*options).block, block) + return NewDuplicateIDPropError("block", o.(*options).block, val) } - blockstr := stringy.New(block) - block := blockstr.SnakeCase("?", "", "#", "").ToLower() - - o.(*options).block = block + o.(*options).block = format(val) return nil } @@ -129,26 +125,20 @@ func WithBlockID(val string) Option { return NewDuplicateIDPropError("blockID", o.(*options).blockID, val) } - valstr := stringy.New(val) - val = valstr.SnakeCase("?", "", "#", "").ToLower() - - o.(*options).blockID = val + o.(*options).blockID = format(val) return nil } } // WithElement sets the element name. -func WithElement(element string) Option { +func WithElement(val string) Option { return func(o Options) error { if o.(*options).elm != "" { - return NewDuplicateIDPropError("element", o.(*options).elm, element) + return NewDuplicateIDPropError("element", o.(*options).elm, val) } - elementstr := stringy.New(element) - element := elementstr.SnakeCase("?", "", "#", "").ToLower() - - o.(*options).elm = element + o.(*options).elm = format(val) return nil } @@ -161,26 +151,20 @@ func WithElementID(val string) Option { return NewDuplicateIDPropError("element id", o.(*options).elmID, val) } - valstr := stringy.New(val) - val = valstr.SnakeCase("?", "", "#", "").ToLower() - - o.(*options).elmID = val + o.(*options).elmID = format(val) return nil } } // WithMod sets the modifier name. -func WithMod(modifier string) Option { +func WithMod(val string) Option { return func(o Options) error { if o.(*options).mod != "" { - return NewDuplicateIDPropError("modifier", o.(*options).mod, modifier) + return NewDuplicateIDPropError("modifier", o.(*options).mod, val) } - modifierstr := stringy.New(modifier) - modifier := modifierstr.SnakeCase("?", "", "#", "").ToLower() - - o.(*options).mod = modifier + o.(*options).mod = format(val) return nil } @@ -193,10 +177,7 @@ func WithModID(val string) Option { return NewDuplicateIDPropError("modifier id", o.(*options).modID, val) } - valstr := stringy.New(val) - val = valstr.SnakeCase("?", "", "#", "").ToLower() - - o.(*options).modID = val + o.(*options).modID = format(val) return nil } @@ -206,7 +187,7 @@ func WithModID(val string) Option { func WithProp(key, val string) Option { return func(o Options) error { o.(*options).propOrder = append(o.(*options).propOrder, key) - o.(*options).props[stringy.New(key).SnakeCase("?", "", "#", "").ToLower()] = stringy.New(val).SnakeCase("?", "", "#", "").ToLower() + o.(*options).props[format(key)] = format(val) return nil } diff --git a/workflows/utils.go b/workflows/utils.go new file mode 100644 index 0000000..3b0ac32 --- /dev/null +++ b/workflows/utils.go @@ -0,0 +1,24 @@ +package workflows + +import ( + "regexp" + + "github.com/gobeam/stringy" +) + +var ( + uuidrxstr = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$" // taken from github.com/go-playground/validator +) + +var ( + uuidrx = regexp.MustCompile(uuidrxstr) +) + +// format sanitizes a string and returns it. +func format(s string) string { + if uuidrx.MatchString(s) { + return s + } + + return stringy.New(s).SnakeCase("?", "", "#", "").ToLower() +}