-
Notifications
You must be signed in to change notification settings - Fork 29
/
doc_test.go
63 lines (50 loc) · 1.33 KB
/
doc_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package loads_test
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"github.com/go-openapi/loads"
"github.com/go-openapi/swag"
)
func ExampleSpec() {
// Example with default loaders defined at the package level
path := "fixtures/yaml/swagger/spec.yml"
doc, err := loads.Spec(path)
if err != nil {
fmt.Println("Could not load this spec")
return
}
fmt.Printf("Spec loaded: %q\n", doc.Host())
// Output: Spec loaded: "api.example.com"
}
func ExampleLoaderOption() {
// Example with custom loaders passed as options
path := "fixtures/yaml/swagger/spec.yml"
// a simpler version of loads.JSONDoc
jsonLoader := loads.NewDocLoaderWithMatch(
func(pth string) (json.RawMessage, error) {
buf, err := os.ReadFile(pth)
return json.RawMessage(buf), err
},
func(pth string) bool {
return filepath.Ext(pth) == ".json"
},
)
// equivalent to the default loader at the package level, which does:
//
// loads.AddLoader(swag.YAMLMatcher, swag.YAMLDoc)
yamlLoader := loads.NewDocLoaderWithMatch(
swag.YAMLDoc,
func(pth string) bool {
return filepath.Ext(pth) == ".yml"
},
)
doc, err := loads.Spec(path, loads.WithDocLoaderMatches(jsonLoader, yamlLoader))
if err != nil {
fmt.Println("Could not load this spec")
return
}
fmt.Printf("Spec loaded: %q\n", doc.Host())
// Output: Spec loaded: "api.example.com"
}