Skip to content

Commit 8d2de68

Browse files
authored
bug fix: ignore private variables when marshaling (#24)
* bug fix: ignore private variables when marshaling * include coverage in github actions
1 parent cd0752d commit 8d2de68

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

.github/workflows/test.yml

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ jobs:
44
test:
55
strategy:
66
matrix:
7-
go-version: [1.13.x, 1.14.x, 1.15.x, 1.16.x]
7+
go-version: [1.13.x, 1.14.x, 1.15.x, 1.16.x, 1.17.x]
88
os: [ubuntu-latest]
99
runs-on: ${{ matrix.os }}
1010
steps:
@@ -15,4 +15,18 @@ jobs:
1515
- name: Checkout code
1616
uses: actions/checkout@v2
1717
- name: Test
18-
run: go test ./...
18+
run: go test ./...
19+
coverage:
20+
runs-on: ubuntu-latest
21+
steps:
22+
- name: Install Go
23+
uses: actions/setup-go@v2
24+
with:
25+
go-version: 1.18.x
26+
- name: Checkout code
27+
uses: actions/checkout@v2
28+
- name: Test
29+
run: |
30+
go test -v -race -coverprofile=coverage.txt -covermode=atomic
31+
- name: Coverage
32+
uses: codecov/codecov-action@v2

marshal.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ func MarshalUnescaped(v interface{}) string {
6666
func parseStruct(u *url.URL, uVal *url.Values, vStruct reflect.Value) {
6767
for i := 0; i < vStruct.NumField(); i++ {
6868
field := vStruct.Field(i)
69-
69+
if !field.CanInterface() {
70+
continue // skip unexported variables
71+
}
7072
// check for embedded struct and handle recursively
7173
if field.Kind() == reflect.Struct {
7274
ptr := reflect.New(field.Type())

marshal_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,13 @@ func TestMarshal(t *testing.T) {
315315
}{String: "Fuji"},
316316
Expected: "?String=Fuji",
317317
},
318+
"private": {
319+
Input: struct {
320+
Int int
321+
name string
322+
}{Int: 7, name: "hello"},
323+
Expected: "?Int=7",
324+
},
318325
}
319326
trial.New(fn, cases).SubTest(t)
320327
}

0 commit comments

Comments
 (0)