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

Add buf generate configs and update other packages to use these configs #2637

Merged
merged 76 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
76 commits
Select commit Hold shift + click to select a range
4f01250
init
oliversun9 Nov 21, 2023
dc09dab
commit
oliversun9 Nov 21, 2023
d80be02
update input config and managed config
oliversun9 Nov 22, 2023
0715e66
ModuleFullNameString -> ModuleFullName
oliversun9 Nov 22, 2023
7b50b79
remove ImageFile interface
oliversun9 Nov 22, 2023
99b3741
move const to the top
oliversun9 Nov 22, 2023
175a45a
remove type switch from plugin config
oliversun9 Nov 22, 2023
4d6b7bb
godoc
oliversun9 Nov 22, 2023
9af86dd
Merge branch 'bufmod' into osun/bufgen-interface
oliversun9 Nov 22, 2023
48c2747
add some concrete types
oliversun9 Nov 22, 2023
e73b9c7
rename
oliversun9 Nov 22, 2023
29f34a5
Merge branch 'osun/bufgen-interface' into osun/bufgen-move-v1
oliversun9 Nov 22, 2023
33698bd
commit
oliversun9 Nov 22, 2023
bac4b04
make java_package_prefix, java_package_suffix and etc. file options a…
oliversun9 Nov 22, 2023
ecf6d25
Merge branch 'osun/bufgen-interface' into osun/bufgen-move-v1
oliversun9 Nov 22, 2023
0d80c90
parse managed v1 config into the new config interface; need testing
oliversun9 Nov 22, 2023
5bea9fe
commit
oliversun9 Nov 22, 2023
1a440e0
type config and plugin config
oliversun9 Nov 22, 2023
930e373
TODO
oliversun9 Nov 22, 2023
193f4ac
rename
oliversun9 Nov 22, 2023
2bd20c2
plugin config tests
oliversun9 Nov 22, 2023
088cd69
add some tests; need more for managed; need to debug the code comment…
oliversun9 Nov 22, 2023
cd3b026
tests for managed mode config v1 -> parsed config
oliversun9 Nov 23, 2023
5f3e185
add logic to bufimagemodify to support the new config; the old stuff …
oliversun9 Nov 23, 2023
2789756
added TODO
oliversun9 Nov 23, 2023
a3fcd61
rename file to a more temporary name
oliversun9 Nov 23, 2023
15f0801
checkpoint; going to move some stuff from bufimagemodify to bufgen
oliversun9 Nov 23, 2023
0e6539f
make New..Override public
oliversun9 Nov 24, 2023
c7a7906
commit
oliversun9 Nov 27, 2023
aec9faa
Merge branch 'bufmod' into osun/bufgen-interface
oliversun9 Nov 27, 2023
e87788a
update generate input config interface
oliversun9 Nov 27, 2023
6cfef52
Merge branch 'osun/bufgen-interface' into osun/bufgen-move-v1
oliversun9 Nov 27, 2023
82787fb
fix import paths
oliversun9 Nov 27, 2023
57b0328
geneate.go; there's still work to do
oliversun9 Nov 27, 2023
c6237c7
update input interface
oliversun9 Nov 27, 2023
149d868
Merge branch 'osun/bufgen-interface' into osun/bufgen-move-v1
oliversun9 Nov 27, 2023
657c761
commit
oliversun9 Nov 28, 2023
678ec08
update placeholder
oliversun9 Nov 28, 2023
bccd900
parse v2
oliversun9 Nov 28, 2023
ad75d08
port modify js type (without sweeper)
oliversun9 Nov 28, 2023
048aff3
mark sweeper that supports sweeping field options
oliversun9 Nov 28, 2023
4449874
commit
bufdev Nov 28, 2023
265ae8e
generate with inputs; with some tests skipped due to proto file ref n…
oliversun9 Nov 29, 2023
0eb678c
migrate
oliversun9 Nov 30, 2023
6b66e0d
update TODO
oliversun9 Nov 30, 2023
77729a4
commit
oliversun9 Nov 30, 2023
c3400bc
migrate, without flags; need testing
oliversun9 Nov 30, 2023
cda47af
dont migrate if v2
oliversun9 Nov 30, 2023
8ecef91
parse v1beta1
oliversun9 Nov 30, 2023
fd43bd7
deleting unused code in bufgen
oliversun9 Nov 30, 2023
e7cb090
migrate input as well
oliversun9 Nov 30, 2023
dbcdaf4
--migrate runs; non-testing is finished; need more testing
oliversun9 Nov 30, 2023
f61bb42
todo
oliversun9 Dec 1, 2023
af9daa9
generate_config.go
oliversun9 Dec 1, 2023
05f4773
buf_gen_yaml_file.go
oliversun9 Dec 1, 2023
cfe73ba
commit
oliversun9 Dec 1, 2023
7827f0a
generate_external_config.go
oliversun9 Dec 1, 2023
9ac1693
generate_managed_config.go
oliversun9 Dec 1, 2023
1acc453
move external config to buf_gen_yaml_file.go
oliversun9 Dec 1, 2023
4005a08
generate_managed_option.go
oliversun9 Dec 1, 2023
f346e96
generate manged option
oliversun9 Dec 1, 2023
27fc46a
commit
oliversun9 Dec 1, 2023
521aec1
add Enabled to managed mode config
oliversun9 Dec 1, 2023
62b07b0
generate_plugin_config.go
oliversun9 Dec 2, 2023
e690875
input_config.go
oliversun9 Dec 2, 2023
ca3cec8
some bufimagemodify
oliversun9 Dec 2, 2023
dd7597f
bufimagemodify partly done
oliversun9 Dec 2, 2023
7b21efa
Merge branch 'bufmod' into osun/bufgen-move-v1
oliversun9 Dec 2, 2023
099c72a
Merge branch 'bufmod' into osun/bufgen-move-v1
bufdev Dec 3, 2023
9848b0d
merge bufmod
oliversun9 Dec 4, 2023
f61c862
build
oliversun9 Dec 4, 2023
235ebed
remove empty line
oliversun9 Dec 4, 2023
42dacab
update todo
oliversun9 Dec 4, 2023
738eb3d
merge
bufdev Dec 11, 2023
4834f7b
updates
bufdev Dec 11, 2023
c0d88c9
move bufconfigtest functions
bufdev Dec 11, 2023
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
7 changes: 7 additions & 0 deletions TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
- document behavior of file searching, config override
- fix tamper-proofing
- go through all todos
- Un-skip relevant tests in generate_test.go and generate_unix_test.go, when
- the behavior of --path and --exclude-path is updated to match what's on main
- Debug the skipped test with proto file ref in generate_test.go
- Use syserror when possible (especially in buf gen related code).
- Find the right place (right way to pass a logger) to print a warning when a buf.gen.yaml with
non-empty managed mode but without `enabled: true`. The key is to decide where to pass the logger.
- Fix the issue where reading a buf.lock with empty digest errors. Run `buf mod update` to reproduce.

NOTE: We are not allowing cross-workspace finding for include_package_files=true

Expand Down
2 changes: 2 additions & 0 deletions make/buf/all.mk
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ testbufnew: installbuf
./private/buf/bufcurl/... \
./private/buf/buffetch/... \
./private/buf/bufformat/... \
./private/buf/bufgen/... \
./private/buf/bufprint/... \
./private/buf/bufworkspace/... \
./private/buf/cmd/buf/command/alpha/package/... \
Expand All @@ -74,6 +75,7 @@ testbufnew: installbuf
./private/buf/cmd/buf/command/build/... \
./private/buf/cmd/buf/command/breaking/... \
./private/buf/cmd/buf/command/convert/... \
./private/buf/cmd/buf/command/generate/... \
./private/buf/cmd/buf/command/lint/... \
./private/buf/cmd/buf/command/lsfiles/... \
./private/buf/cmd/buf/command/mod/... \
Expand Down
37 changes: 37 additions & 0 deletions private/buf/bufctl/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ type Controller interface {
input string,
options ...FunctionOption,
) (bufimage.Image, error)
GetImageForInputConfig(
ctx context.Context,
inputConfig bufconfig.InputConfig,
options ...FunctionOption,
) (bufimage.Image, error)
GetImageForWorkspace(
ctx context.Context,
workspace bufworkspace.Workspace,
Expand Down Expand Up @@ -275,6 +280,18 @@ func (c *controller) GetImage(
return c.getImage(ctx, input, functionOptions)
}

func (c *controller) GetImageForInputConfig(
ctx context.Context,
inputConfig bufconfig.InputConfig,
options ...FunctionOption,
) (bufimage.Image, error) {
functionOptions := newFunctionOptions()
for _, option := range options {
option(functionOptions)
}
return c.getImageForInputConfig(ctx, inputConfig, functionOptions)
}

func (c *controller) GetImageForWorkspace(
ctx context.Context,
workspace bufworkspace.Workspace,
Expand Down Expand Up @@ -630,6 +647,26 @@ func (c *controller) getImage(
if err != nil {
return nil, err
}
return c.getImageForRef(ctx, ref, functionOptions)
}

func (c *controller) getImageForInputConfig(
ctx context.Context,
inputConfig bufconfig.InputConfig,
functionOptions *functionOptions,
) (bufimage.Image, error) {
ref, err := c.buffetchRefParser.GetRefForInputConfig(ctx, inputConfig)
if err != nil {
return nil, err
}
return c.getImageForRef(ctx, ref, functionOptions)
}

func (c *controller) getImageForRef(
ctx context.Context,
ref buffetch.Ref,
functionOptions *functionOptions,
) (bufimage.Image, error) {
switch t := ref.(type) {
case buffetch.ProtoFileRef:
workspace, err := c.getWorkspaceForProtoFileRef(ctx, t, functionOptions)
Expand Down
61 changes: 61 additions & 0 deletions private/buf/buffetch/buffetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ package buffetch

import (
"context"
"fmt"
"io"
"net/http"

"github.com/bufbuild/buf/private/buf/buffetch/internal"
"github.com/bufbuild/buf/private/bufpkg/bufconfig"
"github.com/bufbuild/buf/private/bufpkg/bufmodule"
"github.com/bufbuild/buf/private/pkg/app"
"github.com/bufbuild/buf/private/pkg/git"
Expand Down Expand Up @@ -132,18 +134,33 @@ type ProtoFileRef interface {
type MessageRefParser interface {
// GetMessageRef gets the reference for the message file.
GetMessageRef(ctx context.Context, value string) (MessageRef, error)
// GetMessageRefForInputConfig gets the reference for the message file.
GetMessageRefForInputConfig(
ctx context.Context,
inputConfig bufconfig.InputConfig,
) (MessageRef, error)
}

// SourceRefParser is a source ref parser for Buf.
type SourceRefParser interface {
// GetSourceRef gets the reference for the source file.
GetSourceRef(ctx context.Context, value string) (SourceRef, error)
// GetSourceRef gets the reference for the source file.
GetSourceRefForInputConfig(
ctx context.Context,
inputConfig bufconfig.InputConfig,
) (SourceRef, error)
}

// DirRefParser is a dif ref parser for Buf.
type DirRefParser interface {
// GetDirRef gets the reference for the source file.
GetDirRef(ctx context.Context, value string) (DirRef, error)
// GetDirRefForInputConfig gets the reference for the source file.
GetDirRefForInputConfig(
ctx context.Context,
inputConfig bufconfig.InputConfig,
) (DirRef, error)
}

// ModuleRefParser is a source ref parser for Buf.
Expand All @@ -161,6 +178,11 @@ type SourceOrModuleRefParser interface {

// GetSourceOrModuleRef gets the reference for the message file or source bucket.
GetSourceOrModuleRef(ctx context.Context, value string) (SourceOrModuleRef, error)
// GetSourceOrModuleRefForInputConfig gets the reference for the message file or source bucket.
GetSourceOrModuleRefForInputConfig(
ctx context.Context,
inputConfig bufconfig.InputConfig,
) (SourceOrModuleRef, error)
}

// RefParser is a ref parser for Buf.
Expand All @@ -170,8 +192,11 @@ type RefParser interface {
DirRefParser
SourceOrModuleRefParser

// TODO: should this be renamed to GetRefForString?
// GetRef gets the reference for the message file, source bucket, or module.
GetRef(ctx context.Context, value string) (Ref, error)
// GetRefForInputConfig gets the reference for the message file, source bucket, or module.
GetRefForInputConfig(ctx context.Context, inputConfig bufconfig.InputConfig) (Ref, error)
}

// NewRefParser returns a new RefParser.
Expand Down Expand Up @@ -394,6 +419,42 @@ func NewWriter(
)
}

// GetInputConfigForString returns the input config for the input string.
func GetInputConfigForString(
ctx context.Context,
refParser RefParser,
value string,
) (bufconfig.InputConfig, error) {
ref, err := refParser.GetRef(ctx, value)
if err != nil {
return nil, err
}
switch t := ref.(type) {
case MessageRef:
switch t.MessageEncoding() {
case MessageEncodingBinpb:
return bufconfig.NewBinaryImageInputConfig(
t.Path(),
t.internalSingleRef().CompressionType().String(),
)
case MessageEncodingJSON:
return bufconfig.NewJSONImageInputConfig(
t.Path(),
t.internalSingleRef().CompressionType().String(),
)
case MessageEncodingTxtpb:
return bufconfig.NewBinaryImageInputConfig(
t.Path(),
t.internalSingleRef().CompressionType().String(),
)
default:
// TODO: handle refs with YAML type
return nil, fmt.Errorf("unknown encoding: %v", t.MessageEncoding())
}
}
return internal.GetInputConfigForRef(ref.internalRef(), value)
}

type getBucketOptions struct {
noSearch bool
}
Expand Down
82 changes: 82 additions & 0 deletions private/buf/buffetch/internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ package internal

import (
"context"
"fmt"
"io"
"net/http"
"strconv"

"github.com/bufbuild/buf/private/bufpkg/bufconfig"
"github.com/bufbuild/buf/private/bufpkg/bufmodule"
"github.com/bufbuild/buf/private/pkg/app"
"github.com/bufbuild/buf/private/pkg/git"
Expand Down Expand Up @@ -83,6 +86,20 @@ type ArchiveType int
// CompressionType is a compression type.
type CompressionType int

// String implements fmt.Stringer
func (c CompressionType) String() string {
switch c {
case CompressionTypeNone:
return "none"
case CompressionTypeGzip:
return "gzip"
case CompressionTypeZstd:
return "zstd"
default:
return strconv.Itoa(int(c))
}
}

// Ref is a reference.
type Ref interface {
ref()
Expand Down Expand Up @@ -360,6 +377,12 @@ type RefParser interface {
//
// The options should be used to validate that you are getting one of the correct formats.
GetParsedRef(ctx context.Context, value string, options ...GetParsedRefOption) (ParsedRef, error)
// GetParsedRefForInputConfig gets the ParsedRef for the input config.
//
// The returned ParsedRef will be either a ParsedSingleRef, ParsedArchiveRef, ParsedDirRef, ParsedGitRef, or ParsedModuleRef.
//
// The options should be used to validate that you are getting one of the correct formats.
GetParsedRefForInputConfig(ctx context.Context, inputConfig bufconfig.InputConfig, options ...GetParsedRefOption) (ParsedRef, error)
}

// NewRefParser returns a new RefParser.
Expand Down Expand Up @@ -809,3 +832,62 @@ func WithPutFileNoFileCompression() PutFileOption {

// GetModuleOption is a GetModule option.
type GetModuleOption func(*getModuleOptions)

// GetInputConfigForRef returns the input config for the ref. A string is also
// passed because if the ref is a git ref, it would only have a git.Name, instead
// of a git branch, a git ref and a git tag. Therefore the original string is passed.
func GetInputConfigForRef(ref Ref, value string) (bufconfig.InputConfig, error) {
_, options, err := getRawPathAndOptions(value)
if err != nil {
return nil, err
}
switch t := ref.(type) {
case ArchiveRef:
switch t.ArchiveType() {
case ArchiveTypeZip:
return bufconfig.NewZipArchiveInputConfig(
t.Path(),
t.SubDirPath(),
t.StripComponents(),
)
case ArchiveTypeTar:
return bufconfig.NewTarballInputConfig(
t.Path(),
t.SubDirPath(),
t.CompressionType().String(),
t.StripComponents(),
)
default:
return nil, fmt.Errorf("invalid archive type: %v", t.ArchiveType())
}
case DirRef:
return bufconfig.NewDirectoryInputConfig(
t.Path(),
)
case ModuleRef:
return bufconfig.NewModuleInputConfig(
t.ModuleRef().String(),
)
case ProtoFileRef:
return bufconfig.NewProtoFileInputConfig(
t.Path(),
t.IncludePackageFiles(),
)
case GitRef:
return bufconfig.NewGitRepoInputConfig(
t.Path(),
t.SubDirPath(),
options["branch"],
options["tag"],
options["ref"],
toPointer(t.Depth()),
t.RecurseSubmodules(),
)
default:
return nil, fmt.Errorf("unexpected Ref of type %T", ref)
}
}

func toPointer[T any](value T) *T {
return &value
}
Loading
Loading