• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pace / bricks / 13717956986

06 Mar 2025 02:30PM UTC coverage: 51.823% (-4.8%) from 56.612%
13717956986

push

github

web-flow
Merge pull request #406 from pace/gomod-update

This updates all dependencies to the latest version, excluding

github.com/bsm/redislock
github.com/dave/jennifer

as newer versions lead to unwanted behavior.

54 of 82 new or added lines in 9 files covered. (65.85%)

453 existing lines in 15 files now uncovered.

4889 of 9434 relevant lines covered (51.82%)

20.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

51.52
/http/jsonapi/generator/generate.go
1
// Copyright © 2018 by PACE Telematics GmbH. All rights reserved.
2

3
package generator
4

5
import (
6
        "fmt"
7
        "io"
8
        "net/http"
9
        "net/url"
10
        "os"
11
        "strings"
12

13
        "github.com/dave/jennifer/jen"
14
        "github.com/getkin/kin-openapi/openapi3"
15
)
16

17
type buildFunc func(schema *openapi3.T) error
18

19
// Generator for go types, requests handler and simple validators
20
// for the given OpenAPIv3. The OpenAPIv3 schema is expected to comply
21
// with the json-api specification.
22
// Everything that doesn't comply to the json-api specification will
23
// be ignored during generation.
24
// The Generator doesn't validate necessarily.
25
type Generator struct {
26
        goSource            *jen.File
27
        serviceName         string
28
        generatedTypes      map[string]bool
29
        generatedArrayTypes map[string]bool
30
}
31

NEW
32
func loadSwaggerFromURI(loader *openapi3.Loader, url *url.URL) (*openapi3.T, error) { // nolint: interfacer
×
NEW
33
        var schema *openapi3.T
×
34

×
35
        resp, err := http.Get(url.String())
×
36
        if err != nil {
×
37
                return nil, err
×
38
        }
×
39
        defer resp.Body.Close() // nolint: errcheck
×
40

×
41
        body, err := io.ReadAll(resp.Body)
×
42
        if err != nil {
×
43
                return nil, err
×
44
        }
×
45

NEW
46
        schema, err = loader.LoadFromData(body)
×
47
        if err != nil {
×
48
                return nil, err
×
49
        }
×
50

51
        return schema, nil
×
52
}
53

54
// BuildSource generates the go code in the specified path with specified package name
55
// based on the passed schema source (url or file path)
56
func (g *Generator) BuildSource(source, packagePath, packageName string) (string, error) {
5✔
57
        loader := openapi3.NewLoader()
5✔
58
        var schema *openapi3.T
5✔
59

5✔
60
        if strings.HasPrefix(source, "http://") || strings.HasPrefix(source, "https://") {
5✔
61
                loc, err := url.Parse(source)
×
62
                if err != nil {
×
63
                        return "", err
×
64
                }
×
65

66
                schema, err = loadSwaggerFromURI(loader, loc)
×
67
                if err != nil {
×
68
                        return "", err
×
69
                }
×
70
        } else {
5✔
71
                // read spec
5✔
72
                data, err := os.ReadFile(source) // nolint: gosec
5✔
73
                if err != nil {
5✔
74
                        return "", err
×
75
                }
×
76

77
                // parse spec
78
                schema, err = loader.LoadFromData(data)
5✔
79
                if err != nil {
5✔
80
                        return "", err
×
81
                }
×
82
        }
83

84
        return g.BuildSchema(schema, packagePath, packageName)
5✔
85
}
86

87
// BuildSchema generates the go code in the specified path with specified package name
88
// based on the passed schema
89
func (g *Generator) BuildSchema(schema *openapi3.T, packagePath, packageName string) (string, error) {
5✔
90
        g.generatedTypes = make(map[string]bool)
5✔
91
        g.generatedArrayTypes = make(map[string]bool)
5✔
92

5✔
93
        g.goSource = jen.NewFilePathName(packagePath, packageName)
5✔
94
        g.goSource.PackageComment("// Code generated by github.com/pace/bricks DO NOT EDIT.")
5✔
95
        g.goSource.ImportAlias(pkgJSONAPIMetrics, "metrics")
5✔
96
        g.goSource.ImportAlias(pkgSentry, "sentry")
5✔
97

5✔
98
        g.serviceName = packageName
5✔
99

5✔
100
        buildFuncs := []buildFunc{
5✔
101
                g.BuildTypes,
5✔
102
                g.buildSecurityBackendInterface,
5✔
103
                g.buildSecurityConfigs,
5✔
104
                g.BuildHandler,
5✔
105
        }
5✔
106

5✔
107
        for _, bf := range buildFuncs {
25✔
108
                err := bf(schema)
20✔
109
                if err != nil {
20✔
110
                        return "", err
×
111
                }
×
112
        }
113

114
        return fmt.Sprintf("%#v", g.goSource), nil
5✔
115
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc