1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-06 08:16:03 +02:00
go-micro/util/qson
Shubhendra Singh Chauhan 26b859c4f9
improve code quality (#2128)
* Fix inefficient string comparison

* Fix unnecessary calls to Printf

* Canonicalize header key

* Replace `t.Sub(time.Now())` with `time.Until`

* Remove unnecessary blank (_) identifier

* Remove unnecessary use of slice

* Remove unnecessary comparison with bool
2021-02-25 08:30:35 +00:00
..
LICENSE bundle qson lib in util (#1561) 2020-04-23 11:08:09 +03:00
merge_test.go bundle qson lib in util (#1561) 2020-04-23 11:08:09 +03:00
merge.go bundle qson lib in util (#1561) 2020-04-23 11:08:09 +03:00
qson_test.go improve code quality (#2128) 2021-02-25 08:30:35 +00:00
qson.go bundle qson lib in util (#1561) 2020-04-23 11:08:09 +03:00
README.md bundle qson lib in util (#1561) 2020-04-23 11:08:09 +03:00

qson

This is copy from https://github.com/joncalhoun/qson As author says he is not acrivelly maintains the repo and not plan to do that.

Usage

You can either turn a URL query param into a JSON byte array, or unmarshal that directly into a Go object.

Transforming the URL query param into a JSON byte array:

import "github.com/joncalhoun/qson"

func main() {
  b, err := qson.ToJSON("bar%5Bone%5D%5Btwo%5D=2&bar[one][red]=112")
  if err != nil {
    panic(err)
  }
  fmt.Println(string(b))
  // Should output: {"bar":{"one":{"red":112,"two":2}}}
}

Or unmarshalling directly into a Go object using JSON struct tags:

import "github.com/joncalhoun/qson"

type unmarshalT struct {
	A string     `json:"a"`
	B unmarshalB `json:"b"`
}
type unmarshalB struct {
	C int `json:"c"`
}

func main() {
  var out unmarshalT
  query := "a=xyz&b[c]=456"
  err := Unmarshal(&out, query)
  if err != nil {
  	t.Error(err)
  }
  // out should equal
  //   unmarshalT{
	// 	  A: "xyz",
	// 	  B: unmarshalB{
	// 	  	C: 456,
	// 	  },
	//   }
}

To get a query string like in the two previous examples you can use the RawQuery field on the net/url.URL type.