From fd2045ff0515cd8260bd0e7ef1ed4b1c8dd18d69 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Fri, 16 Apr 2021 17:32:06 +0300 Subject: [PATCH] initial import Signed-off-by: Vasiliy Tolstov --- .github/ISSUE_TEMPLATE/bug_report.md | 24 ++++ .../feature-request---enhancement.md | 17 +++ .github/ISSUE_TEMPLATE/question.md | 14 +++ .github/PULL_REQUEST_TEMPLATE.md | 9 ++ .github/renovate.json | 19 ++++ .github/stale.sh | 13 +++ .github/workflows/build.yml | 46 ++++++++ .github/workflows/pr.yml | 46 ++++++++ codec_test.go | 18 +++ go.mod | 5 + go.sum | 16 +++ urlencode.go | 104 ++++++++++++++++++ 12 files changed, 331 insertions(+) create mode 100644 .github/ISSUE_TEMPLATE/bug_report.md create mode 100644 .github/ISSUE_TEMPLATE/feature-request---enhancement.md create mode 100644 .github/ISSUE_TEMPLATE/question.md create mode 100644 .github/PULL_REQUEST_TEMPLATE.md create mode 100644 .github/renovate.json create mode 100755 .github/stale.sh create mode 100644 .github/workflows/build.yml create mode 100644 .github/workflows/pr.yml create mode 100644 codec_test.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 urlencode.go diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 0000000..1899438 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,24 @@ +--- +name: Bug report +about: For reporting bugs in go-micro +title: "[BUG]" +labels: '' +assignees: '' + +--- + +**Describe the bug** + +1. What are you trying to do? +2. What did you expect to happen? +3. What happens instead? + +**How to reproduce the bug:** + +If possible, please include a minimal code snippet here. + +**Environment:** +Go Version: please paste `go version` output here +``` +please paste `go env` output here +``` diff --git a/.github/ISSUE_TEMPLATE/feature-request---enhancement.md b/.github/ISSUE_TEMPLATE/feature-request---enhancement.md new file mode 100644 index 0000000..459817f --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature-request---enhancement.md @@ -0,0 +1,17 @@ +--- +name: Feature request / Enhancement +about: If you have a need not served by go-micro +title: "[FEATURE]" +labels: '' +assignees: '' + +--- + +**Is your feature request related to a problem? Please describe.** +A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] + +**Describe the solution you'd like** +A clear and concise description of what you want to happen. + +**Additional context** +Add any other context or screenshots about the feature request here. diff --git a/.github/ISSUE_TEMPLATE/question.md b/.github/ISSUE_TEMPLATE/question.md new file mode 100644 index 0000000..1daf48b --- /dev/null +++ b/.github/ISSUE_TEMPLATE/question.md @@ -0,0 +1,14 @@ +--- +name: Question +about: Ask a question about go-micro +title: '' +labels: '' +assignees: '' + +--- + +Before asking, please check if your question has already been answered: + +1. Check the documentation - https://micro.mu/docs/ +2. Check the examples and plugins - https://github.com/micro/examples & https://github.com/micro/go-plugins +3. Search existing issues diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..cba3cbc --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,9 @@ +## Pull Request template +Please, go through these steps before clicking submit on this PR. + +1. Give a descriptive title to your PR. +2. Provide a description of your changes. +3. Make sure you have some relevant tests. +4. Put `closes #XXXX` in your comment to auto-close the issue that your PR fixes (if applicable). + +**PLEASE REMOVE THIS TEMPLATE BEFORE SUBMITTING** diff --git a/.github/renovate.json b/.github/renovate.json new file mode 100644 index 0000000..52d2918 --- /dev/null +++ b/.github/renovate.json @@ -0,0 +1,19 @@ +{ + "extends": [ + "config:base" + ], + "packageRules": [ + { + "matchUpdateTypes": ["minor", "patch", "pin", "digest"], + "automerge": true + }, + { + "groupName": "all deps", + "separateMajorMinor": true, + "groupSlug": "all", + "packagePatterns": [ + "*" + ] + } + ] +} diff --git a/.github/stale.sh b/.github/stale.sh new file mode 100755 index 0000000..8a345c4 --- /dev/null +++ b/.github/stale.sh @@ -0,0 +1,13 @@ +#!/bin/bash -ex + +export PATH=$PATH:$(pwd)/bin +export GO111MODULE=on +export GOBIN=$(pwd)/bin + +#go get github.com/rvflash/goup@v0.4.1 + +#goup -v ./... +#go get github.com/psampaz/go-mod-outdated@v0.6.0 +go list -u -m -mod=mod -json all | go-mod-outdated -update -direct -ci || true + +#go list -u -m -json all | go-mod-outdated -update diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..eb19b68 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,46 @@ +name: build +on: + push: + branches: + - master +jobs: + test: + name: test + runs-on: ubuntu-latest + steps: + - name: setup + uses: actions/setup-go@v2 + with: + go-version: 1.16 + - name: checkout + uses: actions/checkout@v2 + - name: cache + uses: actions/cache@v2 + with: + path: ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: ${{ runner.os }}-go- + - name: deps + run: go get -v -t -d ./... + - name: test + env: + INTEGRATION_TESTS: yes + run: go test -mod readonly -v ./... + lint: + name: lint + runs-on: ubuntu-latest + steps: + - name: checkout + uses: actions/checkout@v2 + - name: lint + uses: golangci/golangci-lint-action@v2 + continue-on-error: true + with: + # Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version. + version: v1.30 + # Optional: working directory, useful for monorepos + # working-directory: somedir + # Optional: golangci-lint command line arguments. + # args: --issues-exit-code=0 + # Optional: show only new issues if it's a pull request. The default value is `false`. + # only-new-issues: true diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml new file mode 100644 index 0000000..545baf2 --- /dev/null +++ b/.github/workflows/pr.yml @@ -0,0 +1,46 @@ +name: prbuild +on: + pull_request: + branches: + - master +jobs: + test: + name: test + runs-on: ubuntu-latest + steps: + - name: setup + uses: actions/setup-go@v2 + with: + go-version: 1.16 + - name: checkout + uses: actions/checkout@v2 + - name: cache + uses: actions/cache@v2 + with: + path: ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: ${{ runner.os }}-go- + - name: deps + run: go get -v -t -d ./... + - name: test + env: + INTEGRATION_TESTS: yes + run: go test -mod readonly -v ./... + lint: + name: lint + runs-on: ubuntu-latest + steps: + - name: checkout + uses: actions/checkout@v2 + - name: lint + uses: golangci/golangci-lint-action@v2 + continue-on-error: true + with: + # Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version. + version: v1.30 + # Optional: working directory, useful for monorepos + # working-directory: somedir + # Optional: golangci-lint command line arguments. + # args: --issues-exit-code=0 + # Optional: show only new issues if it's a pull request. The default value is `false`. + # only-new-issues: true diff --git a/codec_test.go b/codec_test.go new file mode 100644 index 0000000..341ced2 --- /dev/null +++ b/codec_test.go @@ -0,0 +1,18 @@ +package urlencode + +import ( + "bytes" + "testing" +) + +func TestReadBody(t *testing.T) { + s := &struct { + Name string + }{} + c := NewCodec() + b := bytes.NewReader(nil) + err := c.ReadBody(b, s) + if err != nil { + t.Fatal(err) + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..9c658c4 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/unistack-org/micro-codec-urlencode/v3 + +go 1.15 + +require github.com/unistack-org/micro/v3 v3.3.13 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..3e07cd4 --- /dev/null +++ b/go.sum @@ -0,0 +1,16 @@ +github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= +github.com/ef-ds/deque v1.0.4/go.mod h1:gXDnTC3yqvBcHbq2lcExjtAcVrOnJCbMcZXmuj8Z4tg= +github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= +github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= +github.com/silas/dag v0.0.0-20210121180416-41cf55125c34/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I= +github.com/unistack-org/micro/v3 v3.3.13 h1:y4bDDkbwnjgOckrhFkC6D/o42tr75X33UbrB+Ko0M68= +github.com/unistack-org/micro/v3 v3.3.13/go.mod h1:98hNcMXp/WyWJwLwCuwrhN1Jm7aCWaRNsMfRjK8Fq+Y= +golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/urlencode.go b/urlencode.go new file mode 100644 index 0000000..7a943a2 --- /dev/null +++ b/urlencode.go @@ -0,0 +1,104 @@ +// Package urlencode provides a urlencode codec +package urlencode + +import ( + "io" + "io/ioutil" + + "github.com/unistack-org/micro/v3/codec" + rutil "github.com/unistack-org/micro/v3/util/reflect" +) + +type urlencodeCodec struct{} + +func (c *urlencodeCodec) Marshal(b interface{}) ([]byte, error) { + switch m := b.(type) { + case nil: + return nil, nil + case *codec.Frame: + return m.Data, nil + } + + v, err := rutil.StructURLValues(b, "", []string{"protobuf", "json"}) + if err != nil { + return nil, err + } + + return []byte(v.Encode()), nil +} + +func (c *urlencodeCodec) Unmarshal(b []byte, v interface{}) error { + if len(b) == 0 { + return nil + } + switch m := v.(type) { + case nil: + return nil + case *codec.Frame: + m.Data = b + return nil + } + + mp, err := rutil.URLMap(string(b)) + if err != nil { + return err + } + + return rutil.Merge(v, rutil.FlattenMap(mp), rutil.Tags([]string{"protobuf", "json"}), rutil.SliceAppend(true)) +} + +func (c *urlencodeCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.MessageType) error { + return nil +} + +func (c *urlencodeCodec) ReadBody(conn io.Reader, b interface{}) error { + switch m := b.(type) { + case nil: + return nil + case *codec.Frame: + buf, err := ioutil.ReadAll(conn) + if err != nil { + return err + } else if len(buf) == 0 { + return nil + } + m.Data = buf + return nil + } + + buf, err := ioutil.ReadAll(conn) + if err != nil { + return err + } else if len(buf) == 0 { + return nil + } + + return c.Unmarshal(buf, b) +} + +func (c *urlencodeCodec) Write(conn io.Writer, m *codec.Message, b interface{}) error { + switch m := b.(type) { + case nil: + return nil + case *codec.Frame: + _, err := conn.Write(m.Data) + return err + } + + buf, err := c.Marshal(b) + if err != nil { + return err + } + + _, err = conn.Write(buf) + + return err +} + +func (c *urlencodeCodec) String() string { + return "json" +} + +func NewCodec() codec.Codec { + return &urlencodeCodec{} +}