Skip to content

Commit

Permalink
Merge pull request #79 from torden/add_RegExpNamedGroups
Browse files Browse the repository at this point in the history
Improve func, support go.mod
  • Loading branch information
torden authored Jun 5, 2019
2 parents c70034f + 18daa98 commit 73aed51
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 28 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
*.test
*.report
report/
vendor/
vendor/
tt.go
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ sudo: false
language: go
dist: trusty
go:
- 1.12.x
- 1.11.x
- 1.10.x
- 1.9.x
- 1.8.x
- 1.7.x
- 1.6.x
- 1.5.x
- 1.4.x
- tip
# - tip
before_install:
- go get github.com/mattn/goveralls
- go get golang.org/x/tools/cmd/cover
Expand Down
53 changes: 30 additions & 23 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ CMD_ECHO :=$(shell which echo)
CMD_NM :=$(shell which nm)
CMD_GO :=$(shell which go)
CMD_GOLINT :=$(shell which golint)
CMD_GOMETALINTER :=$(shell which gometalinter)
CMD_GOIMPORTS :=$(shell which goimport)
CMD_MAKE2HELP :=$(shell which make2help)
CMD_GLIDE :=$(shell which glide)
CMD_GOVER :=$(shell which gover)
CMD_GOVERALLS :=$(shell which goveralls)
CMD_CILINT :=$(shell which golangci-lint)
CMD_CURL :=$(shell which curl)

PATH_REPORT=report
PATH_RACE_REPORT=$(PKG_NAME).race.report
Expand All @@ -50,42 +51,48 @@ PATH_PROF_MUTEX=$(PKG_NAME).mutex.prof

VER_GOLANG=$(shell go version | awk '{print $$3}' | sed -e "s/go//;s/\.//g")
GOLANGV18_OVER=$(shell [ "$(VER_GOLANG)" -ge "180" ] && echo 1 || echo 0)
GOMOD_FOUND=$(shell go --help 2>&1 | fgrep "module maintenance" | awk '{print $$1}')
GOMOD_SUPPORT=$(shell [ "$(GOMOD_FOUND)" = "mod" ] && echo 1 || echo 0)

all: clean setup

## Setup Build Environment
setup::
@$(CMD_ECHO) -e "\033[1;40;32mSetup Build Environment.\033[01;m\x1b[0m"
@$(CMD_ECHO) -e "\033[1;40;32mSetup Build Environment.\033[01;m\x1b[0m"
ifeq ($(GOMOD_SUPPORT),1)
@$(CMD_GO) mod tidy
@$(CMD_GO) mod verify
else
@$(CMD_GO) get github.com/Masterminds/glide
@$(CMD_GO) get github.com/Songmu/make2help/cmd/make2help
@$(CMD_GO) get github.com/davecgh/go-spew/spew
@$(CMD_GO) get github.com/k0kubun/pp
@$(CMD_GO) get github.com/alecthomas/gometalinter
@$(CMD_GO) get github.com/mattn/goveralls
@$(CMD_GO) get golang.org/x/tools/cmd/cover
@$(CMD_GO) get github.com/modocache/gover
@$(CMD_GO) get github.com/dustin/go-humanize
@$(CMD_GO) get github.com/golang/lint/golint
@$(CMD_GO) get golang.org/x/lint/golint
@$(CMD_GO) get github.com/awalterschulze/gographviz
@$(CMD_GO) get github.com/golang/dep/cmd/dep
@$(CMD_GOMETALINTER) install
@GO111MODULE=off $(CMD_GO) get github.com/golang/dep/cmd/dep
endif
@$(CMD_CURL) -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin v1.16.0
@$(CMD_ECHO) -e "\033[1;40;36mDone\033[01;m\x1b[0m"

## Build the go-strutil
build::
@$(CMD_ECHO) -e "\033[1;40;32mBuild the go-strutil.\033[01;m\x1b[0m"
@$(CMD_ECHO) -e "\033[1;40;32mBuild the go-strutil.\033[01;m\x1b[0m"
@$(CMD_GO) build
@$(CMD_ECHO) -e "\033[1;40;36mDone\033[01;m\x1b[0m"

## Build the go-strutil for development
devbuild::
@$(CMD_ECHO) -e "\033[1;40;32mBuild the go-strutil.\033[01;m\x1b[0m"
@$(CMD_ECHO) -e "\033[1;40;32mBuild the go-strutil.\033[01;m\x1b[0m"
@$(CMD_GO) build -x -v -gcflags="-N -l"
@$(CMD_ECHO) -e "\033[1;40;36mDone\033[01;m\x1b[0m"

## Run a LintChecker (Normal)
lint: setup
@$(CMD_ECHO) -e "\033[1;40;32mRun a LintChecker (Normal).\033[01;m\x1b[0m"
@$(CMD_ECHO) -e "\033[1;40;32mRun a LintChecker (Normal).\033[01;m\x1b[0m"
@$(CMD_GO) vet $$($(CMD_GLIDE) novendor)
@for pkg in $$($(CMD_GLIDE) novendor -x); do \
$(CMD_GOLINT) -set_exit_status $$pkg || exit $$?; \
Expand All @@ -94,35 +101,35 @@ lint: setup

## Run a LintChecker (Strict)
strictlint: setup
@$(CMD_ECHO) -e "\033[1;40;32mRun a LintChecker (Strict).\033[01;m\x1b[0m"
@$(CMD_GOMETALINTER) $$($(CMD_GLIDE) novendor)
@$(CMD_ECHO) -e "\033[1;40;32mRun a LintChecker (Strict).\033[01;m\x1b[0m"
@$(CMD_CILINT) run $$($(CMD_GLIDE) novendor)
@$(CMD_ECHO) -e "\033[1;40;36mDone\033[01;m\x1b[0m"

## Run Go Test with Data Race Detection
testassert: clean
@$(CMD_ECHO) -e "\033[1;40;32mRun Go Test.\033[01;m\x1b[0m"
@$(CMD_ECHO) -e "\033[1;40;32mRun Go Test.\033[01;m\x1b[0m"
@$(CMD_GO) test -v -test.parallel 4 -race -run Test_strutils_Assert*
@$(CMD_ECHO) -e "\033[1;40;36mGenerated a report of data race detection in $(PATH_REPORT)/doc/$(PATH_RACE_REPORT).pid\033[01;m\x1b[0m"
@$(CMD_ECHO) -e "\033[1;40;36mDone\033[01;m\x1b[0m"

## Run Go Test with Data Race Detection
test: clean
@$(CMD_MKDIR) -p $(PATH_REPORT)/raw/ $(PATH_REPORT)/doc/
@$(CMD_ECHO) -e "\033[1;40;32mRun Go Test.\033[01;m\x1b[0m"
@$(CMD_ECHO) -e "\033[1;40;32mRun Go Test.\033[01;m\x1b[0m"
@GORACE="log_path=$(PATH_REPORT)/doc/$(PATH_RACE_REPORT)" $(CMD_GO) test -tags unittest -v -test.parallel 4 -race -coverprofile=$(PATH_REPORT)/raw/$(PATH_CONVER_PROFILE)
@$(CMD_ECHO) -e "\033[1;40;36mGenerated a report of data race detection in $(PATH_REPORT)/doc/$(PATH_RACE_REPORT).pid\033[01;m\x1b[0m"
@$(CMD_ECHO) -e "\033[1;40;36mDone\033[01;m\x1b[0m"

## Send a report of coverage profile to coveralls.io
coveralls::
@$(CMD_GO) get github.com/mattn/goveralls
@$(CMD_ECHO) -e "\033[1;40;32mSend a report of coverage profile to coveralls.io.\033[01;m\x1b[0m"
@$(CMD_ECHO) -e "\033[1;40;32mSend a report of coverage profile to coveralls.io.\033[01;m\x1b[0m"
@$(CMD_GOVERALLS) -coverprofile=$(PATH_REPORT)/raw/$(PATH_CONVER_PROFILE) -service=travis-ci
@$(CMD_ECHO) -e "\033[1;40;36mDone\033[01;m\x1b[0m"

## Generate a report about coverage
cover: test
@$(CMD_ECHO) -e "\033[1;40;32mGenerate a report about coverage.\033[01;m\x1b[0m"
@$(CMD_ECHO) -e "\033[1;40;32mGenerate a report about coverage.\033[01;m\x1b[0m"
@$(CMD_GO) tool cover -func=$(PATH_CONVER_PROFILE) -o $(PATH_CONVER_PROFILE).txt
@$(CMD_GO) tool cover -html=$(PATH_CONVER_PROFILE) -o $(PATH_CONVER_PROFILE).html
@$(CMD_ECHO) -e "\033[1;40;36mGenerated a report file : $(PATH_CONVER_PROFILE).html\033[01;m\x1b[0m"
Expand All @@ -131,15 +138,15 @@ cover: test
## Profiling
pprof: clean
@$(CMD_MKDIR) -p $(PATH_REPORT)/raw/ $(PATH_REPORT)/doc/
@$(CMD_ECHO) -e "\033[1;40;32mGenerate profiles.\033[01;m\x1b[0m"
@$(CMD_ECHO) -e "\033[1;40;33mGenerate a CPU profile.\033[01;m\x1b[0m"
@$(CMD_ECHO) -e "\033[1;40;32mGenerate profiles.\033[01;m\x1b[0m"
@$(CMD_ECHO) -e "\033[1;40;33mGenerate a CPU profile.\033[01;m\x1b[0m"
@$(CMD_GO) test -tags unittest -test.parallel 4 -bench . -benchmem -cpuprofile=$(PATH_REPORT)/raw/$(PATH_PROF_CPU)
@$(CMD_ECHO) -e "\033[1;40;33mGenerate a Memory profile.\033[01;m\x1b[0m"
@$(CMD_ECHO) -e "\033[1;40;33mGenerate a Memory profile.\033[01;m\x1b[0m"
@$(CMD_GO) test -tags unittest -test.parallel 4 -bench . -benchmem -memprofile=$(PATH_REPORT)/raw/$(PATH_PROF_MEM)
@$(CMD_ECHO) -e "\033[1;40;33mGenerate a Block profile.\033[01;m\x1b[0m"
@$(CMD_ECHO) -e "\033[1;40;33mGenerate a Block profile.\033[01;m\x1b[0m"
@$(CMD_GO) test -tags unittest -test.parallel 4 -bench . -benchmem -blockprofile=$(PATH_REPORT)/raw/$(PATH_PROF_BLOCK)
ifeq ($(GOLANGV18_OVER),1)
@$(CMD_ECHO) -e "\033[1;40;33mGenerate a Mutex profile.\033[01;m\x1b[0m"
@$(CMD_ECHO) -e "\033[1;40;33mGenerate a Mutex profile.\033[01;m\x1b[0m"
@$(CMD_GO) test -tags unittest -test.parallel 4 -bench . -benchmem -mutexprofile=$(PATH_REPORT)/raw/$(PATH_PROF_MUTEX)
endif
@$(CMD_MV) -f *.test $(PATH_REPORT)/raw/
Expand All @@ -148,14 +155,14 @@ endif
## Generate report fo profiling
report: pprof
@$(CMD_MKDIR) -p $(PATH_REPORT)/raw/ $(PATH_REPORT)/doc/
@$(CMD_ECHO) -e "\033[1;40;33mGenerate all report in text format.\033[01;m\x1b[0m"
@$(CMD_ECHO) -e "\033[1;40;33mGenerate all report in text format.\033[01;m\x1b[0m"
@$(CMD_GO) tool pprof -text $(PATH_REPORT)/raw/$(PKG_NAME).test $(PATH_REPORT)/raw/$(PATH_PROF_CPU) > $(PATH_REPORT)/doc/$(PATH_PROF_CPU).txt
@$(CMD_GO) tool pprof -text $(PATH_REPORT)/raw/$(PKG_NAME).test $(PATH_REPORT)/raw/$(PATH_PROF_MEM) > $(PATH_REPORT)/doc/$(PATH_PROF_MEM).txt
@$(CMD_GO) tool pprof -text $(PATH_REPORT)/raw/$(PKG_NAME).test $(PATH_REPORT)/raw/$(PATH_PROF_BLOCK) > $(PATH_REPORT)/doc/$(PATH_PROF_BLOCK).txt
ifeq ($(GOLANGV18_OVER),1)
@$(CMD_GO) tool pprof -text $(PATH_REPORT)/raw/$(PKG_NAME).test $(PATH_REPORT)/raw/$(PATH_PROF_MUTEX) > $(PATH_REPORT)/doc/$(PATH_PROF_MUTEX).txt
endif
@$(CMD_ECHO) -e "\033[1;40;33mGenerate all report in pdf format.\033[01;m\x1b[0m"
@$(CMD_ECHO) -e "\033[1;40;33mGenerate all report in pdf format.\033[01;m\x1b[0m"
@$(CMD_GO) tool pprof -pdf $(PATH_REPORT)/raw/$(PKG_NAME).test $(PATH_REPORT)/raw/$(PATH_PROF_CPU) > $(PATH_REPORT)/doc/$(PATH_PROF_CPU).pdf
@$(CMD_GO) tool pprof -pdf $(PATH_REPORT)/raw/$(PKG_NAME).test $(PATH_REPORT)/raw/$(PATH_PROF_MEM) > $(PATH_REPORT)/doc/$(PATH_PROF_MEM).pdf
@$(CMD_GO) tool pprof -pdf $(PATH_REPORT)/raw/$(PKG_NAME).test $(PATH_REPORT)/raw/$(PATH_PROF_BLOCK) > $(PATH_REPORT)/doc/$(PATH_PROF_BLOCK).pdf
Expand All @@ -170,7 +177,7 @@ help::

## Clean-up
clean::
@$(CMD_ECHO) -e "\033[1;40;32mClean-up.\033[01;m\x1b[0m"
@$(CMD_ECHO) -e "\033[1;40;32mClean-up.\033[01;m\x1b[0m"
@$(CMD_RM) -rfv *.coverprofile *.swp *.core *.html *.prof *.test *.report ./$(PATH_REPORT)/*
@$(CMD_ECHO) -e "\033[1;40;36mDone\033[01;m\x1b[0m"

Expand Down
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ README.md haven't contain all the examples. Please refer to the the XXXtest.go f
- [ReverseUnicode](#reverseunicode)
- [FileMD5Hash](#filemd5hash)
- [MD5Hash](#md5hash)
- [RegExpNamedGroups](#RegExpNamedGroups)
- [Validation Methods](#validation-methods)
- [IsValidEmail](#isvalidemail)
- [IsValidDomain](#isvaliddomain)
Expand Down Expand Up @@ -784,6 +785,35 @@ The above example will output:
15f764f21d09b11102eb015fc8824d00
```


### RegExpNamedGroups

RegExpNamedGroups is Captures the text matched by regex into the group name

```go
func (s *StringProc) RegExpNamedGroups(regex *regexp.Regexp, val string) (map[string]string, error)
```

Example:

```go
strproc := strutils.NewStringProc()

regexGoVersion := regexp.MustCompile(`go(?P<major>([0-9]{1,3}))\.(?P<minor>([0-9]{1,3}))(\.(?P<rev>([0-9]{1,3})))?`)
retval, err := getGroupMatched(regexGoVersion, runtime.Version())
if err != nil {
return 0, err
}
fmt.Println(retval)
```

The above example will output:

```bash
map[major:1 minor:11 rev:5]
```

----

## Validation Methods
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/torden/go-strutil

require github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4
require github.com/dustin/go-humanize v1.0.0
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4 h1:qk/FSDDxo05wdJH28W+p5yivv7LuLYLRXPPD8KQCtZs=
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
30 changes: 30 additions & 0 deletions stringproc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1156,4 +1156,34 @@ func (s *StringProc) closeFd(fd *os.File) {
if err != nil {
fmt.Printf("Error : %+v\n", err)
}

}

// RegExpNamedGroups is Captures the text matched by regex into the group name
// NOTE : Not Support the Multiple Groups with The Same Name
func (s *StringProc) RegExpNamedGroups(regex *regexp.Regexp, val string) (map[string]string, error) {

ok := false
err := errors.New("not all success patterns were matched")

retval := map[string]string{}
extractSubExpNames := regex.SubexpNames()

ret := regex.FindStringSubmatch(val)
if len(ret) > 0 {
for no, val := range ret {
if no != 0 && val != "" {
if extractSubExpNames[no] != "" {
retval[extractSubExpNames[no]] = val
ok = true
}
}
}
}

if ok {
err = nil
}

return retval, err
}
21 changes: 21 additions & 0 deletions stringproc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"math"
"net/url"
"os"
"regexp"
"runtime"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -1501,3 +1503,22 @@ func Test_strutils_MD5Hash(t *testing.T) {
assert.AssertEquals(t, retval, v, "Return Value mismatch.\nExpected: %v\nActual: %v", retval, v)
}
}

func Test_strutils_RegExpNamedGroups(t *testing.T) {

t.Parallel()

var ok bool

//refer : https://golang.org/doc/devel/release.html#policy
var regexGoVersion = regexp.MustCompile(`go(?P<major>([0-9]{1,3}))\.(?P<minor>([0-9]{1,3}))(\.(?P<rev>([0-9]{1,3})))?`)

verdic, err := strproc.RegExpNamedGroups(regexGoVersion, runtime.Version())
assert.AssertNil(t, err, "Error : %v", err)

_, ok = verdic["major"]
assert.AssertTrue(t, ok, "Not Exists Major ver. in Return Value")

_, ok = verdic["minor"]
assert.AssertTrue(t, ok, "Not Exists Minor ver. in Return Value")
}

0 comments on commit 73aed51

Please sign in to comment.