Skip to content

Commit a768d65

Browse files
odsodnoahdietzquirogas
committed
feat: backport googleapis v2 migration
Heavy lifting for migration to protobuf v2 APIs already done by @noahdietz and @quirogas - reusing the code as-is. Kudos. Co-authored-by: Noah Dietz <noahdietz@users.noreply.github.com> Co-authored-by: Santiago Quiroga <quirogas@users.noreply.github.com>
1 parent e4050d2 commit a768d65

67 files changed

Lines changed: 9847 additions & 5 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ require (
66
bitbucket.org/creachadair/stringset v0.0.14
77
buf.build/gen/go/aep/api/protocolbuffers/go v1.36.10-20251109183837-26a011a354ee.1
88
buf.build/go/bufplugin v0.9.0
9+
cloud.google.com/go/iam v1.5.3
910
cloud.google.com/go/longrunning v0.7.0
1011
github.com/bmatcuk/doublestar/v4 v4.9.1
12+
github.com/bufbuild/protocompile v0.14.1
1113
github.com/gertd/go-pluralize v0.2.1
1214
github.com/google/go-cmp v0.7.0
1315
github.com/jhump/protoreflect v1.17.0
@@ -19,6 +21,7 @@ require (
1921
google.golang.org/genproto/googleapis/api v0.0.0-20251014184007-4626949a642f
2022
google.golang.org/protobuf v1.36.10
2123
gopkg.in/yaml.v2 v2.4.0
24+
gopkg.in/yaml.v3 v3.0.1
2225
)
2326

2427
require (
@@ -29,7 +32,6 @@ require (
2932
buf.build/go/spdx v0.2.0 // indirect
3033
cel.dev/expr v0.24.0 // indirect
3134
github.com/antlr4-go/antlr/v4 v4.13.1 // indirect
32-
github.com/bufbuild/protocompile v0.14.1 // indirect
3335
github.com/clipperhouse/uax29/v2 v2.2.0 // indirect
3436
github.com/golang/protobuf v1.5.4 // indirect
3537
github.com/google/cel-go v0.26.1 // indirect

go.sum

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
bitbucket.org/creachadair/stringset v0.0.14 h1:t1ejQyf8utS4GZV/4fM+1gvYucggZkfhb+tMobDxYOE=
22
bitbucket.org/creachadair/stringset v0.0.14/go.mod h1:Ej8fsr6rQvmeMDf6CCWMWGb14H9mz8kmDgPPTdiVT0w=
3-
buf.build/gen/go/aep/api/protocolbuffers/go v1.36.10-20251016045117-f9844266f27f.1 h1:jK64CGldQTGX2ESi+v41uERp+z28OFqGyAn/T/Qs8Q0=
4-
buf.build/gen/go/aep/api/protocolbuffers/go v1.36.10-20251016045117-f9844266f27f.1/go.mod h1:JOZpZ+zS3G2OKO02hKlEazAGR5X9uVpFyeCamXBXg5c=
5-
buf.build/gen/go/aep/api/protocolbuffers/go v1.36.10-20251102152130-5f3e69139afa.1 h1:SEKgDODwWdsVL9nnOPkFBlZ9wAuO7kRQNobSZb5JR7I=
6-
buf.build/gen/go/aep/api/protocolbuffers/go v1.36.10-20251102152130-5f3e69139afa.1/go.mod h1:JOZpZ+zS3G2OKO02hKlEazAGR5X9uVpFyeCamXBXg5c=
73
buf.build/gen/go/aep/api/protocolbuffers/go v1.36.10-20251109183837-26a011a354ee.1 h1:hvHgJH3scOHSRm9nC9JOdesPF9yc7Fl281TNQe60SL0=
84
buf.build/gen/go/aep/api/protocolbuffers/go v1.36.10-20251109183837-26a011a354ee.1/go.mod h1:JOZpZ+zS3G2OKO02hKlEazAGR5X9uVpFyeCamXBXg5c=
95
buf.build/gen/go/bufbuild/bufplugin/protocolbuffers/go v1.36.10-20250718181942-e35f9b667443.1 h1:FzJGrb8r7vir+P3zJ5Ebey8p54LYTYtQsrM/U35YO9Q=
@@ -20,6 +16,8 @@ buf.build/go/spdx v0.2.0 h1:IItqM0/cMxvFJJumcBuP8NrsIzMs/UYjp/6WSpq8LTw=
2016
buf.build/go/spdx v0.2.0/go.mod h1:bXdwQFem9Si3nsbNy8aJKGPoaPi5DKwdeEp5/ArZ6w8=
2117
cel.dev/expr v0.24.0 h1:56OvJKSH3hDGL0ml5uSxZmz3/3Pq4tJ+fb1unVLAFcY=
2218
cel.dev/expr v0.24.0/go.mod h1:hLPLo1W4QUmuYdA72RBX06QTs6MXw941piREPl3Yfiw=
19+
cloud.google.com/go/iam v1.5.3 h1:+vMINPiDF2ognBJ97ABAYYwRgsaqxPbQDlMnbHMjolc=
20+
cloud.google.com/go/iam v1.5.3/go.mod h1:MR3v9oLkZCTlaqljW6Eb2d3HGDGK5/bDv93jhfISFvU=
2321
cloud.google.com/go/longrunning v0.7.0 h1:FV0+SYF1RIj59gyoWDRi45GiYUMM3K1qO51qoboQT1E=
2422
cloud.google.com/go/longrunning v0.7.0/go.mod h1:ySn2yXmjbK9Ba0zsQqunhDkYi0+9rlXIwnoAf+h+TPY=
2523
github.com/antlr4-go/antlr/v4 v4.13.1 h1:SqQKkuVZ+zWkMMNkjy5FZe5mr5WURWnlpmOuzYWrPrQ=

internal/v2/version.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
package internal
15+
16+
// Version is the current tagged release of the library.
17+
const Version = "2.2.0"

lint/v2/config.go

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package lint
16+
17+
import (
18+
"encoding/json"
19+
"fmt"
20+
"io"
21+
"os"
22+
"path/filepath"
23+
"strings"
24+
25+
"github.com/bmatcuk/doublestar/v4"
26+
"gopkg.in/yaml.v3"
27+
)
28+
29+
// Configs determine if a rule is enabled or not on a file path.
30+
type Configs []Config
31+
32+
// Config stores rule configurations for certain files
33+
// that the file path must match any of the included paths
34+
// but none of the excluded ones.
35+
type Config struct {
36+
// Explicitly specify the input file paths in scope of this config.
37+
// If omitted, it applies to all input file paths.
38+
IncludedPaths []string `json:"included_paths" yaml:"included_paths"`
39+
40+
// Explicitly specify the input files paths to exclude from using this
41+
// config. If omitted, none of the input file paths are excluded.
42+
ExcludedPaths []string `json:"excluded_paths" yaml:"excluded_paths"`
43+
44+
// The fully-qualifed rule name of a rule to enable as part of this config.
45+
// Can be one of the following formats:
46+
//
47+
// - an individual rule: `core::0203::field-behavior-required`
48+
// - an entire AIP rule group: `core::0203`
49+
// - an entire AIP category: `core`
50+
// - all rules: `all`
51+
EnabledRules []string `json:"enabled_rules" yaml:"enabled_rules"`
52+
53+
// The fully-qualifed rule name of a rule to disable as part of this config.
54+
// Can be one of the following formats:
55+
//
56+
// - an individual rule: `core::0203::field-behavior-required`
57+
// - an entire AIP rule group: `core::0203`
58+
// - an entire AIP category: `core`
59+
// - all rules: `all`
60+
DisabledRules []string `json:"disabled_rules" yaml:"disabled_rules"`
61+
}
62+
63+
// ReadConfigsFromFile reads Configs from a file.
64+
// It supports JSON(.json) and YAML(.yaml or .yml) files.
65+
func ReadConfigsFromFile(path string) (Configs, error) {
66+
var parse func(io.Reader) (Configs, error)
67+
switch filepath.Ext(path) {
68+
case ".json":
69+
parse = ReadConfigsJSON
70+
case ".yaml", ".yml":
71+
parse = ReadConfigsYAML
72+
}
73+
if parse == nil {
74+
return nil, fmt.Errorf("reading Configs: unsupported format `%q` with file path `%q`", filepath.Ext(path), path)
75+
}
76+
77+
f, err := os.Open(path)
78+
if err != nil {
79+
return nil, fmt.Errorf("readConfig: %s", err.Error())
80+
}
81+
defer f.Close()
82+
83+
return parse(f)
84+
}
85+
86+
// ReadConfigsJSON reads Configs from a JSON file.
87+
func ReadConfigsJSON(f io.Reader) (Configs, error) {
88+
b, err := io.ReadAll(f)
89+
if err != nil {
90+
return nil, err
91+
}
92+
var c Configs
93+
if err := json.Unmarshal(b, &c); err != nil {
94+
return nil, err
95+
}
96+
return c, nil
97+
}
98+
99+
// ReadConfigsYAML reads Configs from a YAML(.yml or .yaml) file.
100+
func ReadConfigsYAML(f io.Reader) (Configs, error) {
101+
b, err := io.ReadAll(f)
102+
if err != nil {
103+
return nil, err
104+
}
105+
var c Configs
106+
if err := yaml.Unmarshal(b, &c); err != nil {
107+
return nil, err
108+
}
109+
return c, nil
110+
}
111+
112+
// IsRuleEnabled returns true if a rule is enabled by the configs.
113+
func (configs Configs) IsRuleEnabled(rule string, path string) bool {
114+
// Enabled by default if the rule does not belong to one of the default
115+
// disabled groups. Otherwise, needs to be explicitly enabled.
116+
enabled := !matchRule(rule, defaultDisabledRules...)
117+
for _, c := range configs {
118+
if c.matchPath(path) {
119+
if matchRule(rule, c.DisabledRules...) {
120+
enabled = false
121+
}
122+
if matchRule(rule, c.EnabledRules...) {
123+
enabled = true
124+
}
125+
}
126+
}
127+
128+
return enabled
129+
}
130+
131+
func (c Config) matchPath(path string) bool {
132+
if matchPath(path, c.ExcludedPaths...) {
133+
return false
134+
}
135+
return len(c.IncludedPaths) == 0 || matchPath(path, c.IncludedPaths...)
136+
}
137+
138+
func matchPath(path string, pathPatterns ...string) bool {
139+
path = filepath.ToSlash(path)
140+
for _, pattern := range pathPatterns {
141+
pattern = filepath.ToSlash(pattern)
142+
if matched, _ := doublestar.Match(pattern, path); matched {
143+
return true
144+
}
145+
}
146+
return false
147+
}
148+
149+
func matchRule(rule string, rulePrefixes ...string) bool {
150+
rule = strings.ToLower(rule)
151+
for _, prefix := range rulePrefixes {
152+
prefix = strings.ToLower(prefix)
153+
prefix = strings.TrimSuffix(prefix, nameSeparator) // "core::" -> "core"
154+
prefix = strings.TrimPrefix(prefix, nameSeparator) // "::http-body" -> "http-body"
155+
if prefix == "all" ||
156+
prefix == rule ||
157+
strings.HasPrefix(rule, prefix+nameSeparator) || // e.g., "core" matches "core::http-body", but not "core-rules::http-body"
158+
strings.HasSuffix(rule, nameSeparator+prefix) || // e.g., "http-body" matches "core::http-body", but not "core::google-http-body"
159+
strings.Contains(rule, nameSeparator+prefix+nameSeparator) { // e.g., "http-body" matches "core::http-body::post", but not "core::google-http-body::post"
160+
return true
161+
}
162+
}
163+
return false
164+
}

0 commit comments

Comments
 (0)