Debian Codegraph @

pkg/util/proto/testing/openapi_v3.go

raw symbols SCIP
1 /*
2 Copyright 2022 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package testing
18
19 import (
20 "os"
21 "path/filepath"
22 "sync"
23
24 openapi_v3 "github.com/google/gnostic-models/openapiv3"
25 )
26
27 type FakeV3 struct {
28 Path string
29
30 lock sync.Mutex
31 documents map[string]*openapi_v3.Document
32 errors map[string]error
33 }
34
35 func (f *FakeV3) OpenAPIV3Schema(groupVersion string) (*openapi_v3.Document, error) {
36 f.lock.Lock()
37 defer f.lock.Unlock()
38
39 if existing, ok := f.documents[groupVersion]; ok {
40 return existing, nil
41 } else if existingError, ok := f.errors[groupVersion]; ok {
42 return nil, existingError
43 }
44
45 _, err := os.Stat(f.Path)
46 if err != nil {
47 return nil, err
48 }
49 spec, err := os.ReadFile(filepath.Join(f.Path, groupVersion+".json"))
50 if err != nil {
51 return nil, err
52 }
53
54 if f.documents == nil {
55 f.documents = make(map[string]*openapi_v3.Document)
56 }
57
58 if f.errors == nil {
59 f.errors = make(map[string]error)
60 }
61
62 result, err := openapi_v3.ParseDocument(spec)
63 if err != nil {
64 f.errors[groupVersion] = err
65 return nil, err
66 }
67
68 f.documents[groupVersion] = result
69 return result, nil
70 }