Skip to content

Commit

Permalink
Add raw behaviour for the jq expression for the simple fields(like st…
Browse files Browse the repository at this point in the history
…ring, int, etc...)
  • Loading branch information
mgerasimchuk committed Oct 31, 2023
1 parent 8cc4e5e commit 252b8da
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/test-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ jobs:
- name: Test Integration
run: make test-integration
- name: Coverage report
uses: ncruces/go-coverage-report@57ac6f0f19874f7afbab596105154f08004f482e
uses: mgerasimchuk/go-coverage-report@chart-diff-only
with:
badge-title: Coverage (integration)
coverage-file: assets/coverage/integration/coverage.out
report: true
output-dir: assets/coverage/integration
chart: true
chart-show-diff-only: true
3 changes: 2 additions & 1 deletion .github/workflows/test-unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ jobs:
- name: Test Unit
run: make test-unit
- name: Coverage report
uses: ncruces/go-coverage-report@57ac6f0f19874f7afbab596105154f08004f482e
uses: mgerasimchuk/go-coverage-report@chart-diff-only
with:
badge-title: Coverage (unit)
coverage-file: assets/coverage/unit/coverage.out
report: true
output-dir: assets/coverage/unit
chart: true
chart-show-diff-only: true
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ These capabilities make Protty a useful tool for a variety of purposes, such as
The following command will start a proxy on port 8080, and after starting, all traffic from port 8080 will be redirected to a remote host located at https://example.com

```shell
docker run -p8080:80 -e REMOTE_URI=https://example.com:443 mgerasimchuk/protty:v0.4.3
docker run -p8080:80 -e REMOTE_URI=https://example.com:443 mgerasimchuk/protty:v0.4.4
```

## Running options and runtime configuration

```
» ~ docker run -p8080:80 -it mgerasimchuk/protty:v0.4.3 /bin/sh -c 'protty start --help'
» ~ docker run -p8080:80 -it mgerasimchuk/protty:v0.4.4 /bin/sh -c 'protty start --help'
Start the proxy
Usage:
Expand Down
5 changes: 5 additions & 0 deletions internal/infrastructure/app/protty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ func TestStartCommand(t *testing.T) {
args{targetPath: "/", targetResponseBody: "ok", prottyFlags: append(prottyStart, "--transform-response-body-sed", "s|ok|changed|g")},
want{responseBody: "changed"},
},
{
"Flags configuration JQ expression",
args{targetPath: "/", targetResponseBody: `{"code": 100, "message": "message body"}`, prottyFlags: append(prottyStart, "--transform-response-body-jq", ".message")},
want{responseBody: "message body"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
12 changes: 9 additions & 3 deletions pkg/util/jq.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"github.com/itchyny/gojq"
"reflect"
)

// JQ transform the input by jq expression
Expand Down Expand Up @@ -36,9 +37,14 @@ func JQ(jqExpr string, input []byte) ([]byte, []byte, error) {
}
transformed = v
}
transformedJSON, err := json.Marshal(transformed)
if err != nil {
return input, input, fmt.Errorf("%s: %w", GetFuncName(json.Marshal), err)
var transformedJSON []byte
if reflect.TypeOf(transformed).Kind() == reflect.Map {
transformedJSON, err = json.Marshal(transformed)
if err != nil {
return input, input, fmt.Errorf("%s: %w", GetFuncName(json.Marshal), err)
}
} else {
transformedJSON = []byte(fmt.Sprintf("%v", transformed))
}

return transformedJSON, input, nil
Expand Down

0 comments on commit 252b8da

Please sign in to comment.