Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: split large compute file up #2546

Merged
merged 7 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127,584 changes: 0 additions & 127,584 deletions compute/v0.alpha/compute-gen.go

Large diffs are not rendered by default.

68,204 changes: 68,204 additions & 0 deletions compute/v0.alpha/compute2-gen.go

Large diffs are not rendered by default.

59,414 changes: 59,414 additions & 0 deletions compute/v0.alpha/compute3-gen.go

Large diffs are not rendered by default.

119,556 changes: 0 additions & 119,556 deletions compute/v0.beta/compute-gen.go

Large diffs are not rendered by default.

63,725 changes: 63,725 additions & 0 deletions compute/v0.beta/compute2-gen.go

Large diffs are not rendered by default.

55,865 changes: 55,865 additions & 0 deletions compute/v0.beta/compute3-gen.go

Large diffs are not rendered by default.

108,251 changes: 0 additions & 108,251 deletions compute/v1/compute-gen.go

Large diffs are not rendered by default.

58,393 changes: 58,393 additions & 0 deletions compute/v1/compute2-gen.go

Large diffs are not rendered by default.

49,892 changes: 49,892 additions & 0 deletions compute/v1/compute3-gen.go

Large diffs are not rendered by default.

120 changes: 112 additions & 8 deletions google-api-go-generator/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ const (
googleDiscoveryURL = "https://www.googleapis.com/discovery/v1/apis"
googleDefaultUniverse = "googleapis.com"
universeDomainPlaceholder = "UNIVERSE_DOMAIN"

splitFileSeperator = `// =*=*"`
// Size the buffer so there is room for the header and contents when
// splitting up large files.
splitFileHeaderSize = 500
)

var (
Expand Down Expand Up @@ -75,6 +80,10 @@ var skipAPIGeneration = map[string]bool{
"datalineage:v1": true,
}

var apisToSplit = map[string]bool{
"compute": true,
}

// API represents an API to generate, as well as its state while it's
// generating.
type API struct {
Expand Down Expand Up @@ -226,7 +235,7 @@ func getAPIs() []*API {
} else {
bytes = slurpURL(*apisURL)
if *publicOnly {
if err := writeFile(apiListFile, bytes); err != nil {
if err := writeFile(apiListFile, "", bytes); err != nil {
log.Fatal(err)
}
}
Expand Down Expand Up @@ -300,7 +309,7 @@ func apiFromFile(file string) (*API, error) {
func checkAndUpdateSpecFile(file string, contents []byte) error {
// check if file exists
if _, err := os.Stat(file); os.IsNotExist(err) {
return writeFile(file, contents)
return writeFile(file, "", contents)
}
existing, err := os.ReadFile(file)
if err != nil {
Expand All @@ -309,7 +318,7 @@ func checkAndUpdateSpecFile(file string, contents []byte) error {
if err := isNewerRevision(existing, contents); err != nil {
return err
}
return writeFile(file, contents)
return writeFile(file, "", contents)
}

// isNewerRevision returns nil if the contents of new has a newer revision than
Expand All @@ -331,7 +340,7 @@ func isNewerRevision(old []byte, new []byte) error {
return nil
}

func writeFile(file string, contents []byte) error {
func writeFile(file, pkg string, contents []byte) error {
// Don't write it if the contents are identical.
existing, err := os.ReadFile(file)
if err == nil && (bytes.Equal(existing, contents) || basicallyEqual(existing, contents)) {
Expand All @@ -341,7 +350,49 @@ func writeFile(file string, contents []byte) error {
if err = os.MkdirAll(outdir, 0755); err != nil {
return fmt.Errorf("failed to Mkdir %s: %v", outdir, err)
}
return os.WriteFile(file, contents, 0644)
// Don't try to split spec files, json or non-allowlisted packages
if pkg == "" || !apisToSplit[pkg] {
return os.WriteFile(file, contents, 0644)
}

// Split generated file out into multiple
bs := bytes.Split(contents, []byte(splitFileSeperator))
for i, b := range bs {
var name string
var newB []byte
if i == 0 {
// For the base case, use the provided inputs as is
name = file
var err error
newB, err = format.Source(b)
if err != nil {
return err
}
} else {
// determine the new file name
base := filepath.Dir(file)
fileNum := i + 1
name = filepath.Join(base, fmt.Sprintf("%s%d-gen.go", pkg, fileNum))

// prepend file header, package, and imports
var buf bytes.Buffer
// Size the buffer so there is room for the header and contents
buf.Grow(len(b) + splitFileHeaderSize)
splitFileHeading(&buf, pkg)
_, err := buf.Write(b)
if err != nil {
return err
}
newB, err = format.Source(buf.Bytes())
if err != nil {
return err
}
}
if err := os.WriteFile(name, newB, 0644); err != nil {
return err
}
}
return nil
}

var ignoreLines = regexp.MustCompile(`(?m)^\s+"(?:etag|revision)": ".+\n`)
Expand Down Expand Up @@ -589,7 +640,7 @@ func (a *API) WriteGeneratedCode() error {
}

code, err := a.GenerateCode()
errw := writeFile(genfilename, code)
errw := writeFile(genfilename, a.Package(), code)
if err == nil {
err = errw
}
Expand Down Expand Up @@ -862,8 +913,12 @@ func (a *API) GenerateCode() ([]byte, error) {
for _, meth := range a.APIMethods() {
meth.generateCode()
}

for _, res := range a.doc.Resources {
a.insertSplitFileComment()
rCnt := len(a.doc.Resources) / 2
for i, res := range a.doc.Resources {
if i == rCnt {
a.insertSplitFileComment()
}
a.generateResourceMethods(res)
}

Expand All @@ -874,6 +929,55 @@ func (a *API) GenerateCode() ([]byte, error) {
return clean, nil
}

func (a *API) insertSplitFileComment() {
if apisToSplit[a.Package()] {
a.pn("")
a.pn(splitFileSeperator)
a.pn("")
}
}

// splitFileHeading writes the file preamble used when generating a split file
// client like compute.
func splitFileHeading(w io.Writer, pkg string) {
pn := func(format string, args ...interface{}) {
_, err := fmt.Fprintf(w, format+"\n", args...)
if err != nil {
panic(err)
}
}

pn("// Copyright %s Google LLC.", *copyrightYear)
pn("// Use of this source code is governed by a BSD-style")
pn("// license that can be found in the LICENSE file.")
pn("")
pn("// Code generated file. DO NOT EDIT.")
pn("")
pn("package %s", pkg)
pn("")
pn("import (")
for _, imp := range []string{
"context",
"fmt",
"io",
"net/http",
} {
pn(" %q", imp)
}
pn("")
for _, imp := range []struct {
pkg string
lname string
}{
{*gensupportPkg, "gensupport"},
{*googleapiPkg, "googleapi"},
} {
pn(" %s %q", imp.lname, imp.pkg)
}
pn(")")
pn("")
}

func (a *API) generateScopeConstants() {
scopes := a.doc.Auth.OAuth2Scopes
if len(scopes) == 0 {
Expand Down