Skip to content
Open
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
2 changes: 1 addition & 1 deletion codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (t *CodeTemplate) CreateFile(filePath string, data interface{}) error {
code := t.Buffer.Bytes()

// Enforce code generation standard https://golang.org/s/generatedcode
if !GeneratedCodeRegexp.Match(code) {
if !isGenerated(code) {
return nil, errors.New("output does not follow standard defined at https://golang.org/s/generatedcode")
}

Expand Down
37 changes: 37 additions & 0 deletions generated_ast.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//go:build go1.21
// +build go1.21

/*
Copyright 2025 Olivier Mengué

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package codegen

import (
"go/ast"
"go/parser"
"go/token"
)

func isGenerated(code []byte) bool {
// Code from https://cs.opensource.google/go/go/+/refs/tags/go1.24.5:src/go/ast/issues_test.go;l=127
fset := token.NewFileSet()
f, _ := parser.ParseFile(fset, "", code, parser.PackageClauseOnly|parser.ParseComments)
if f == nil {
return false
}

return ast.IsGenerated(f)
}
24 changes: 24 additions & 0 deletions generated_regexp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//go:build !go1.21
// +build !go1.21

/*
Copyright 2025 Olivier Mengué

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package codegen

func isGenerated(code []byte) bool {
return GeneratedCodeRegexp.Match(code)
}
Loading