Skip to content

Commit

Permalink
Remove pcre dep
Browse files Browse the repository at this point in the history
  • Loading branch information
Stan Bondi committed Jul 2, 2018
1 parent bd28671 commit bf3d61b
Show file tree
Hide file tree
Showing 15 changed files with 27 additions and 731 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ so this package keeps great compatibility with Rack implementation.

## Usage

This package currently relies on libpcre++. Be sure to have it installed on your system before using it. See [here for more information](https://github.com/derekstavis/go-qs/tree/master/vendor/github.com/glenn-brown/golang-pkg-pcre).

### Unmarshal

To unmarshal query strings to a `map[string]interface{}`:
Expand Down
3 changes: 0 additions & 3 deletions glide.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package: github.com/derekstavis/go-qs
import:
- package: github.com/glenn-brown/golang-pkg-pcre
subpackages:
- src/pkg/pcre
- package: github.com/stretchr/testify
version: v1.1.4
subpackages:
Expand Down
41 changes: 21 additions & 20 deletions unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@ package qs
import (
"fmt"
"net/url"
"regexp"
"strings"

"github.com/glenn-brown/golang-pkg-pcre/src/pkg/pcre"
)

var nameRegex = pcre.MustCompile(`\A[\[\]]*([^\[\]]+)\]*`, 0)
var objectRegex1 = pcre.MustCompile(`^\[\]\[([^\[\]]+)\]$`, 0)
var objectRegex2 = pcre.MustCompile(`^\[\](.+)$`, 0)
var nameRegex = regexp.MustCompile(`\A[\[\]]*([^\[\]]+)\]*`)
var objectRegex1 = regexp.MustCompile(`^\[\]\[([^\[\]]+)\]$`)
var objectRegex2 = regexp.MustCompile(`^\[\](.+)$`)

func Unmarshal(qs string) (map[string]interface{}, error) {
components := strings.Split(qs, "&")
params := map[string]interface{}{}

for _, c := range components {
tuple := strings.Split(c, "=")

tuple := strings.Split(c, "=")
for i, item := range tuple {
if unesc, err := url.QueryUnescape(item); err == nil {
tuple[i] = unesc
Expand Down Expand Up @@ -46,25 +45,24 @@ func Unmarshal(qs string) (map[string]interface{}, error) {
}

func normalizeParams(params map[string]interface{}, key string, value interface{}) error {
nameMatcher := nameRegex.MatcherString(key, 0)
k := nameMatcher.GroupString(1)
after := ""

if pos := nameRegex.FindIndex([]byte(key), 0); len(pos) == 2 {
if pos := nameRegex.FindIndex([]byte(key)); len(pos) == 2 {
after = key[pos[1]:]
}

objectMatcher1 := objectRegex1.MatcherString(after, 0)
objectMatcher2 := objectRegex2.MatcherString(after, 0)

if k == "" {
matches := nameRegex.FindStringSubmatch(key)
if len(matches) < 2 {
return nil
}

} else if after == "" {
k := matches[1]
if after == "" {
params[k] = value
return nil
}

} else if after == "[]" {
if after == "[]" {
ival, ok := params[k]

if !ok {
Expand All @@ -80,15 +78,18 @@ func normalizeParams(params map[string]interface{}, key string, value interface{

params[k] = append(array, value)
return nil
}

} else if objectMatcher1.Matches() || objectMatcher2.Matches() {
object1Matches := objectRegex1.FindStringSubmatch(after)
object2Matches := objectRegex2.FindStringSubmatch(after)

if len(object1Matches) > 1 || len(object2Matches) > 1 {
childKey := ""

if objectMatcher1.Matches() {
childKey = objectMatcher1.GroupString(1)
} else if objectMatcher2.Matches() {
childKey = objectMatcher2.GroupString(1)
if len(object1Matches) > 1 {
childKey = object1Matches[1]
} else if len(object2Matches) > 1 {
childKey = object2Matches[1]
}

if childKey != "" {
Expand Down
7 changes: 6 additions & 1 deletion unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import (
)

func TestUnmarshal(t *testing.T) {
hash, err := Unmarshal("foo")
hash, err := Unmarshal("")
if assert.NoError(t, err) {
assert.Equal(t, hash, map[string]interface{}{})
}

hash, err = Unmarshal("foo")
if assert.NoError(t, err) {
assert.Equal(t, hash, map[string]interface{}{"foo": nil})
}
Expand Down
8 changes: 0 additions & 8 deletions vendor/github.com/glenn-brown/golang-pkg-pcre/.gitignore

This file was deleted.

21 changes: 0 additions & 21 deletions vendor/github.com/glenn-brown/golang-pkg-pcre/README.markdown

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

12 changes: 0 additions & 12 deletions vendor/github.com/glenn-brown/golang-pkg-pcre/debian/control

This file was deleted.

27 changes: 0 additions & 27 deletions vendor/github.com/glenn-brown/golang-pkg-pcre/debian/copyright

This file was deleted.

12 changes: 0 additions & 12 deletions vendor/github.com/glenn-brown/golang-pkg-pcre/debian/rules

This file was deleted.

This file was deleted.

Loading

0 comments on commit bf3d61b

Please sign in to comment.