Skip to content

Commit c3422bb

Browse files
committed
doc: added examples to document simple use cases
Also: fix(ci): scheduled vuln scan once a week is enough, no need to run it every day Signed-off-by: Frédéric BIDON <[email protected]>
1 parent 061f7b4 commit c3422bb

File tree

4 files changed

+105
-5
lines changed

4 files changed

+105
-5
lines changed

.github/workflows/scanner.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ on:
1515
push:
1616
branches: [ "master" ]
1717
schedule:
18-
- cron: '18 4 * * *'
18+
- cron: '18 4 * * 3'
1919

2020
jobs:
2121
analysis:

README.md

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,40 @@ An implementation of JSON Pointer for golang, which supports go `struct`.
2020

2121
API is stable.
2222

23+
## Import this library in your project
24+
25+
```cmd
26+
go get github.com/go-openapi/jsonpointer
27+
```
28+
29+
## Basic usage
30+
31+
See [examples](./examples_test.go)
32+
33+
```go
34+
import (
35+
"github.com/go-openapi/jsonpointer"
36+
)
37+
38+
...
39+
40+
pointer, err := jsonpointer.New("/foo/1")
41+
if err != nil {
42+
... // error: e.g. invalid JSON pointer specification
43+
}
44+
45+
value, kind, err := pointer.Get(doc)
46+
if err != nil {
47+
... // error: e.g. key not found, index out of bounds, etc.
48+
}
49+
50+
...
51+
```
52+
53+
## Change log
54+
55+
See <https://github.com/go-openapi/jsonpointer/releases>
56+
2357
## References
2458

2559
<https://tools.ietf.org/html/draft-ietf-appsawg-json-pointer-07>
@@ -62,8 +96,8 @@ using the special trailing character "-" is not implemented.
6296
<!-- Badges: documentation & support -->
6397
[doc-badge]: https://img.shields.io/badge/doc-site-blue?link=https%3A%2F%2Fgoswagger.io%2Fgo-openapi%2F
6498
[doc-url]: https://goswagger.io/go-openapi
65-
[godoc-badge]: https://godoc.org/github.com/go-openapi/jsonpointer?status.svg
66-
[godoc-url]: http://godoc.org/github.com/go-openapi/jsonpointer
99+
[godoc-badge]: https://pkg.go.dev/github.com/go-openapi/jsonpointer?status.svg
100+
[godoc-url]: http://pkg.go.dev/github.com/go-openapi/jsonpointer
67101
[slack-badge]: https://slackin.goswagger.io/badge.svg
68102
[slack-url]: https://slackin.goswagger.io
69103
<!-- Badges: license & compliance -->

examples_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// SPDX-FileCopyrightText: Copyright (c) 2015-2025 go-swagger maintainers
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package jsonpointer
5+
6+
import (
7+
"encoding/json"
8+
"fmt"
9+
)
10+
11+
type exampleDocument struct {
12+
Foo []string `json:"foo"`
13+
}
14+
15+
func ExamplePointer_Get() {
16+
var doc exampleDocument
17+
18+
if err := json.Unmarshal(testDocumentJSONBytes, &doc); err != nil { // populates doc
19+
panic(err)
20+
}
21+
22+
pointer, err := New("/foo/1")
23+
if err != nil {
24+
panic(err)
25+
}
26+
27+
value, kind, err := pointer.Get(doc)
28+
if err != nil {
29+
panic(err)
30+
}
31+
32+
fmt.Printf(
33+
"value: %q\nkind: %v\n",
34+
value, kind,
35+
)
36+
37+
// Output:
38+
// value: "baz"
39+
// kind: string
40+
}
41+
42+
func ExamplePointer_Set() {
43+
var doc exampleDocument
44+
45+
if err := json.Unmarshal(testDocumentJSONBytes, &doc); err != nil { // populates doc
46+
panic(err)
47+
}
48+
49+
pointer, err := New("/foo/1")
50+
if err != nil {
51+
panic(err)
52+
}
53+
54+
result, err := pointer.Set(&doc, "hey my")
55+
if err != nil {
56+
panic(err)
57+
}
58+
59+
fmt.Printf("result: %#v\n", result)
60+
fmt.Printf("doc: %#v\n", doc)
61+
62+
// Output:
63+
// result: &jsonpointer.exampleDocument{Foo:[]string{"bar", "hey my"}}
64+
// doc: jsonpointer.exampleDocument{Foo:[]string{"bar", "hey my"}}
65+
}

pointer.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ func (p *Pointer) DecodedTokens() []string {
6464
return result
6565
}
6666

67-
// IsEmpty returns true if this is an empty json pointer
68-
// this indicates that it points to the root document
67+
// IsEmpty returns true if this is an empty json pointer.
68+
//
69+
// This indicates that it points to the root document.
6970
func (p *Pointer) IsEmpty() bool {
7071
return len(p.referenceTokens) == 0
7172
}

0 commit comments

Comments
 (0)