Skip to content

Commit 233c0d3

Browse files
authored
feat: add yaml tag support and bump to golang 1.22 (#4)
1 parent d3b50d1 commit 233c0d3

File tree

13 files changed

+168
-71
lines changed

13 files changed

+168
-71
lines changed

.github/workflows/ci.yaml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,32 @@ on:
44
push:
55
branches:
66
- '**'
7-
# - $default-branch
87
pull_request:
98
branches:
109
- '**'
11-
# - $default-branch
1210

1311
jobs:
1412
build:
13+
runs-on: ubuntu-latest
1514
env:
1615
COVER: true
17-
runs-on: ubuntu-20.04
16+
1817
steps:
1918

2019
- name: Check out code
21-
uses: actions/checkout@v2
20+
uses: actions/checkout@v4
2221

23-
- name: Set up Go 1.17
24-
uses: actions/setup-go@v2
22+
- name: Set up Go
23+
uses: actions/setup-go@v5
2524
with:
26-
go-version: 1.17.x
25+
go-version-file: reference-gen/go.mod
2726
id: go
2827

2928
- name: Get dependencies
29+
env:
30+
GOLANGCI_LINT_VERSION: v1.63.4
3031
run: |
31-
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.36.0
32+
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin ${GOLANGCI_LINT_VERSION}
3233
curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
3334
chmod +x ./cc-test-reporter
3435

.github/workflows/codeql.yml

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,22 @@ jobs:
1515
strategy:
1616
fail-fast: false
1717

18-
# CodeQL runs on ubuntu-latest and windows-latest
1918
runs-on: ubuntu-latest
2019

2120
steps:
2221
- name: Checkout repository
23-
uses: actions/checkout@v2
24-
with:
25-
# We must fetch at least the immediate parents so that if this is
26-
# a pull request then we can checkout the head.
27-
fetch-depth: 2
28-
29-
# If this run was triggered by a pull request event, then checkout
30-
# the head of the pull request instead of the merge commit.
31-
- run: git checkout HEAD^2
32-
if: ${{ github.event_name == 'pull_request' }}
22+
uses: actions/checkout@v4
3323

3424
# Initializes the CodeQL tools for scanning.
3525
- name: Initialize CodeQL
36-
uses: github/codeql-action/init@v1
26+
uses: github/codeql-action/init@v3
3727
with:
3828
languages: go
3929

4030
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
4131
# If this step fails, then you should remove it and run the build manually (see below)
4232
- name: Autobuild
43-
uses: github/codeql-action/autobuild@v1
33+
uses: github/codeql-action/autobuild@v3
4434

4535
# ℹ️ Command-line programs to run using the OS shell.
4636
# 📚 https://git.io/JvXDl
@@ -54,4 +44,4 @@ jobs:
5444
# make release
5545

5646
- name: Perform CodeQL Analysis
57-
uses: github/codeql-action/analyze@v1
47+
uses: github/codeql-action/analyze@v3

.github/workflows/stale.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
runs-on: ubuntu-latest
1111

1212
steps:
13-
- uses: actions/stale@v1
13+
- uses: actions/stale@v9
1414
with:
1515
repo-token: ${{ secrets.GITHUB_TOKEN }}
1616
stale-issue-message: 'This issue has been inactive for 60 days. If the issue is still relevant please comment to re-activate the issue. If no action is taken within 7 days, the issue will be marked closed.'

reference-gen/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
module github.com/oauth2-proxy/tools/reference-gen
22

3-
go 1.17
3+
go 1.22
44

55
require (
66
github.com/go-git/go-git/v5 v5.4.2
77
github.com/onsi/ginkgo v1.14.1
88
github.com/onsi/gomega v1.10.2
9+
github.com/sergi/go-diff v1.1.0
910
github.com/spf13/pflag v1.0.5
1011
k8s.io/gengo v0.0.0-20201113003025-83324d819ded
1112
k8s.io/klog/v2 v2.4.0
@@ -15,7 +16,6 @@ require (
1516
github.com/fsnotify/fsnotify v1.4.9 // indirect
1617
github.com/go-logr/logr v0.2.0 // indirect
1718
github.com/nxadm/tail v1.4.4 // indirect
18-
github.com/sergi/go-diff v1.1.0 // indirect
1919
golang.org/x/net v0.0.0-20210326060303-6b1517762897 // indirect
2020
golang.org/x/sys v0.0.0-20210502180810-71e4cd670f79 // indirect
2121
golang.org/x/text v0.3.3 // indirect

reference-gen/pkg/generator/generator.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"errors"
66
"fmt"
7-
"io/ioutil"
87
"os"
98
"path/filepath"
109
"text/template"
@@ -68,7 +67,7 @@ func loadHeaderText(fileName string) ([]byte, error) {
6867
return []byte{}, nil
6968
}
7069

71-
headerText, err := ioutil.ReadFile(fileName)
70+
headerText, err := os.ReadFile(fileName)
7271
if err != nil {
7372
return nil, fmt.Errorf("error reading file: %v", err)
7473
}
@@ -184,7 +183,7 @@ func (g *generator) writeOutput(content []byte) error {
184183
return nil
185184
}
186185

187-
if err := ioutil.WriteFile(g.outputFileName, content, 0600); err != nil {
186+
if err := os.WriteFile(g.outputFileName, content, 0600); err != nil {
188187
return fmt.Errorf("could not write file %q: %v", g.outputFileName, err)
189188
}
190189
klog.Infof("Rendered output written to %q", g.outputFileName)

reference-gen/pkg/generator/generator_test.go

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"embed"
66
"fmt"
7-
"io/ioutil"
87
"os"
98
"strings"
109

@@ -16,7 +15,7 @@ import (
1615
)
1716

1817
const (
19-
testDataPackage = "github.com/oauth2-proxy/tools/reference-gen/pkg/generator/testdata"
18+
testDataPackage = "github.com/oauth2-proxy/tools/reference-gen/pkg/generator/testdata/"
2019
)
2120

2221
//go:embed testdata/*.md
@@ -29,35 +28,37 @@ var _ = Describe("Generator", func() {
2928
expectedOutputFileName string
3029
}
3130

32-
DescribeTable("should generate the expected output", func(in generatorTableInput) {
33-
By("Creating an output file")
34-
outputFile, err := ioutil.TempFile("", "oauth2-proxy-reference-generator-suite-")
35-
Expect(err).ToNot(HaveOccurred())
36-
37-
outputFileName := outputFile.Name()
38-
Expect(outputFile.Close()).To(Succeed())
39-
40-
By("Constructing the generator")
41-
gen, err := NewGenerator(testDataPackage, in.requestedTypes, in.headerFileName, outputFileName, "")
42-
Expect(err).ToNot(HaveOccurred())
43-
44-
By("Running the generator")
45-
Expect(gen.Run()).To(Succeed())
46-
47-
By("Loading the output")
48-
output, err := os.ReadFile(outputFileName)
49-
Expect(err).ToNot(HaveOccurred())
50-
51-
By("Loading the expected output")
52-
expectedOutput, err := testOutputs.ReadFile(in.expectedOutputFileName)
53-
Expect(err).ToNot(HaveOccurred())
54-
55-
By("Comparing the outputs")
56-
diffs := diff.Do(string(expectedOutput), string(output))
57-
if len(diffs) > 1 {
58-
// A single diff means the two files are equal, only fail if there is more than one diff.
59-
fmt.Printf("\nUnexpected diff:\n\n%s\n", prettyPrintDiff(diffs))
60-
Fail("Unexpected diff in generated output")
31+
DescribeTable("should generate the expected output for json & yaml tags", func(in generatorTableInput) {
32+
for _, pkg := range []string{"json", "yaml"} {
33+
By(pkg + ": Creating an output file")
34+
outputFile, err := os.CreateTemp("", pkg+"-oauth2-proxy-reference-generator-suite-")
35+
Expect(err).ToNot(HaveOccurred())
36+
37+
outputFileName := outputFile.Name()
38+
Expect(outputFile.Close()).To(Succeed())
39+
40+
By(pkg + ": Constructing the generator")
41+
gen, err := NewGenerator(testDataPackage+pkg, in.requestedTypes, in.headerFileName, outputFileName, "")
42+
Expect(err).ToNot(HaveOccurred())
43+
44+
By(pkg + ": Running the generator")
45+
Expect(gen.Run()).To(Succeed())
46+
47+
By(pkg + ": Loading the output")
48+
output, err := os.ReadFile(outputFileName)
49+
Expect(err).ToNot(HaveOccurred())
50+
51+
By(pkg + ": Loading the expected output")
52+
expectedOutput, err := testOutputs.ReadFile(in.expectedOutputFileName)
53+
Expect(err).ToNot(HaveOccurred())
54+
55+
By(pkg + ": Comparing the outputs")
56+
diffs := diff.Do(string(expectedOutput), string(output))
57+
if len(diffs) > 1 {
58+
// A single diff means the two files are equal, only fail if there is more than one diff.
59+
fmt.Printf("\n%s: Unexpected diff:\n\n%s\n", pkg, prettyPrintDiff(diffs))
60+
Fail(pkg + ": Unexpected diff in generated output")
61+
}
6162
}
6263
},
6364
Entry("With the full test structure, pulls in references for all substructs", generatorTableInput{

reference-gen/pkg/generator/testdata/fullMyTestStruct.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ members table as the origin struct.
99

1010
| Field | Type | Description |
1111
| ----- | ---- | ----------- |
12-
| `NonTaggedField` | _bool_ | NonTaggedField doesn't have a json tag, so the name will be capitalised. |
12+
| `NonTaggedField` | _bool_ | NonTaggedField doesn't have a tag, so the name will be capitalised. |
1313

1414
### AliasedExternalMap
15-
#### (`map[string]interface{}` alias)
15+
#### (`map[string]any` alias)
1616

1717
(**Appears on:** [MyTestStruct](#myteststruct))
1818

@@ -77,4 +77,4 @@ SomeSubStruct is a struct to go within another struct.
7777

7878
| Field | Type | Description |
7979
| ----- | ---- | ----------- |
80-
| `NonTaggedField` | _bool_ | NonTaggedField doesn't have a json tag, so the name will be capitalised. |
80+
| `NonTaggedField` | _bool_ | NonTaggedField doesn't have a tag, so the name will be capitalised. |

reference-gen/pkg/generator/testdata/types.go renamed to reference-gen/pkg/generator/testdata/json/types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package testdata
1+
package json
22

33
import (
44
"text/template"
@@ -63,7 +63,7 @@ type MyTestStruct struct {
6363

6464
// SomeSubStruct is a struct to go within another struct.
6565
type SomeSubStruct struct {
66-
// NonTaggedField doesn't have a json tag, so the name will be capitalised.
66+
// NonTaggedField doesn't have a tag, so the name will be capitalised.
6767
NonTaggedField bool
6868

6969
// privateStruct should not be included in the docs.

reference-gen/pkg/generator/testdata/someSubStructOnly.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ SomeSubStruct is a struct to go within another struct.
66

77
| Field | Type | Description |
88
| ----- | ---- | ----------- |
9-
| `NonTaggedField` | _bool_ | NonTaggedField doesn't have a json tag, so the name will be capitalised. |
9+
| `NonTaggedField` | _bool_ | NonTaggedField doesn't have a tag, so the name will be capitalised. |

reference-gen/pkg/generator/testdata/someSubStructWithHeader.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ SomeSubStruct is a struct to go within another struct.
1010

1111
| Field | Type | Description |
1212
| ----- | ---- | ----------- |
13-
| `NonTaggedField` | _bool_ | NonTaggedField doesn't have a json tag, so the name will be capitalised. |
13+
| `NonTaggedField` | _bool_ | NonTaggedField doesn't have a tag, so the name will be capitalised. |

0 commit comments

Comments
 (0)