mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-14 02:10:00 +08:00
feat(goctl/rpc): support external proto imports with cross-package ty… (#5472)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
89
tools/goctl/rpc/test/proto/06_wellknown/compare.sh
Executable file
89
tools/goctl/rpc/test/proto/06_wellknown/compare.sh
Executable file
@@ -0,0 +1,89 @@
|
||||
#!/usr/bin/env bash
|
||||
# Scenario 06: compare old vs new goctl output — well-known type imports
|
||||
# Usage: bash compare.sh
|
||||
# Requires: go install github.com/zeromicro/go-zero/tools/goctl@latest (auto-installed)
|
||||
set -e
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
GOCTL_ROOT="$(cd "$SCRIPT_DIR/../../../.." && pwd)"
|
||||
NEW_GOCTL="$GOCTL_ROOT/bin/goctl"
|
||||
OLD_GOCTL="$(go env GOPATH)/bin/goctl"
|
||||
OUT_OLD="$SCRIPT_DIR/output_old"
|
||||
OUT_NEW="$SCRIPT_DIR/output_new"
|
||||
|
||||
verify_build() {
|
||||
local dir="$1" label="$2"
|
||||
echo "Verifying $label ..."
|
||||
cd "$dir"
|
||||
go mod tidy
|
||||
if go build ./...; then
|
||||
echo " ✅ $label: build passed"
|
||||
else
|
||||
echo " ❌ $label: build failed"
|
||||
exit 1
|
||||
fi
|
||||
cd "$SCRIPT_DIR"
|
||||
}
|
||||
|
||||
# Find well-known types include path
|
||||
PROTOC_INCLUDE=""
|
||||
PROTOC_BIN="$(which protoc 2>/dev/null || true)"
|
||||
if [ -n "$PROTOC_BIN" ]; then
|
||||
CANDIDATE="$(cd "$(dirname "$PROTOC_BIN")/.." && pwd)/include"
|
||||
[ -f "$CANDIDATE/google/protobuf/timestamp.proto" ] && PROTOC_INCLUDE="$CANDIDATE"
|
||||
fi
|
||||
if [ -z "$PROTOC_INCLUDE" ]; then
|
||||
for d in /opt/homebrew/include /usr/local/include; do
|
||||
[ -f "$d/google/protobuf/timestamp.proto" ] && PROTOC_INCLUDE="$d" && break
|
||||
done
|
||||
fi
|
||||
if [ -z "$PROTOC_INCLUDE" ]; then
|
||||
echo "Error: cannot find google/protobuf/timestamp.proto. Install protobuf (brew install protobuf)."
|
||||
exit 1
|
||||
fi
|
||||
echo "Well-known types: $PROTOC_INCLUDE"
|
||||
|
||||
# Install released goctl and build local goctl
|
||||
echo ">>> Installing goctl@latest ..."
|
||||
go install github.com/zeromicro/go-zero/tools/goctl@latest
|
||||
echo ">>> Building local goctl ..."
|
||||
go build -o "$NEW_GOCTL" "$GOCTL_ROOT"
|
||||
|
||||
# Generate with old goctl
|
||||
echo ">>> Generating with old goctl ..."
|
||||
rm -rf "$OUT_OLD" && mkdir -p "$OUT_OLD/pb"
|
||||
(cd "$OUT_OLD" && go mod init example.com/demo/s06_wellknown > /dev/null 2>&1)
|
||||
cd "$SCRIPT_DIR"
|
||||
"$OLD_GOCTL" rpc protoc events.proto \
|
||||
--go_out="$OUT_OLD/pb" \
|
||||
--go-grpc_out="$OUT_OLD/pb" \
|
||||
--zrpc_out="$OUT_OLD/rpc" \
|
||||
--go_opt=paths=source_relative \
|
||||
--go-grpc_opt=paths=source_relative \
|
||||
--proto_path=. \
|
||||
--proto_path="$PROTOC_INCLUDE"
|
||||
verify_build "$OUT_OLD" "old"
|
||||
|
||||
# Generate with new goctl
|
||||
echo ">>> Generating with new goctl ..."
|
||||
rm -rf "$OUT_NEW" && mkdir -p "$OUT_NEW/pb"
|
||||
(cd "$OUT_NEW" && go mod init example.com/demo/s06_wellknown > /dev/null 2>&1)
|
||||
cd "$SCRIPT_DIR"
|
||||
"$NEW_GOCTL" rpc protoc events.proto \
|
||||
--go_out="$OUT_NEW/pb" \
|
||||
--go-grpc_out="$OUT_NEW/pb" \
|
||||
--zrpc_out="$OUT_NEW/rpc" \
|
||||
--go_opt=paths=source_relative \
|
||||
--go-grpc_opt=paths=source_relative \
|
||||
--proto_path=. \
|
||||
--proto_path="$PROTOC_INCLUDE"
|
||||
verify_build "$OUT_NEW" "new"
|
||||
|
||||
# Diff old vs new (exclude go.mod / go.sum)
|
||||
echo ""
|
||||
echo ">>> Diff (old vs new):"
|
||||
if diff -rq --exclude="go.mod" --exclude="go.sum" "$OUT_OLD" "$OUT_NEW" > /dev/null 2>&1; then
|
||||
echo " [identical] no differences between old and new output"
|
||||
else
|
||||
diff -r --exclude="go.mod" --exclude="go.sum" "$OUT_OLD" "$OUT_NEW" || true
|
||||
fi
|
||||
37
tools/goctl/rpc/test/proto/06_wellknown/events.proto
Normal file
37
tools/goctl/rpc/test/proto/06_wellknown/events.proto
Normal file
@@ -0,0 +1,37 @@
|
||||
syntax = "proto3";
|
||||
|
||||
// 场景06:import google/protobuf/timestamp.proto (well-known type)
|
||||
// 预期:goctl 正常运行,well-known type 的 pb.go 不会被重复生成(跳过)
|
||||
package eventsvc;
|
||||
|
||||
option go_package = "example.com/demo/eventsvc";
|
||||
|
||||
import "google/protobuf/timestamp.proto";
|
||||
|
||||
message Event {
|
||||
string id = 1;
|
||||
string name = 2;
|
||||
google.protobuf.Timestamp created_at = 3;
|
||||
}
|
||||
|
||||
message CreateEventReq {
|
||||
string name = 1;
|
||||
}
|
||||
|
||||
message CreateEventReply {
|
||||
Event event = 1;
|
||||
}
|
||||
|
||||
message ListEventsReq {
|
||||
int32 page = 1;
|
||||
int32 size = 2;
|
||||
}
|
||||
|
||||
message ListEventsReply {
|
||||
repeated Event events = 1;
|
||||
}
|
||||
|
||||
service EventService {
|
||||
rpc CreateEvent(CreateEventReq) returns (CreateEventReply);
|
||||
rpc ListEvents(ListEventsReq) returns (ListEventsReply);
|
||||
}
|
||||
58
tools/goctl/rpc/test/proto/06_wellknown/gen.sh
Executable file
58
tools/goctl/rpc/test/proto/06_wellknown/gen.sh
Executable file
@@ -0,0 +1,58 @@
|
||||
#!/usr/bin/env bash
|
||||
# Scenario 06: well-known type imports
|
||||
# Usage: bash gen.sh
|
||||
set -e
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
GOCTL_ROOT="$(cd "$SCRIPT_DIR/../../../.." && pwd)"
|
||||
GOCTL="$GOCTL_ROOT/bin/goctl"
|
||||
OUT="$SCRIPT_DIR/output"
|
||||
|
||||
# Find well-known types include path
|
||||
PROTOC_INCLUDE=""
|
||||
PROTOC_BIN="$(which protoc 2>/dev/null || true)"
|
||||
if [ -n "$PROTOC_BIN" ]; then
|
||||
CANDIDATE="$(cd "$(dirname "$PROTOC_BIN")/.." && pwd)/include"
|
||||
[ -f "$CANDIDATE/google/protobuf/timestamp.proto" ] && PROTOC_INCLUDE="$CANDIDATE"
|
||||
fi
|
||||
if [ -z "$PROTOC_INCLUDE" ]; then
|
||||
for d in /opt/homebrew/include /usr/local/include; do
|
||||
[ -f "$d/google/protobuf/timestamp.proto" ] && PROTOC_INCLUDE="$d" && break
|
||||
done
|
||||
fi
|
||||
if [ -z "$PROTOC_INCLUDE" ]; then
|
||||
echo "Error: cannot find google/protobuf/timestamp.proto. Install protobuf (brew install protobuf)."
|
||||
exit 1
|
||||
fi
|
||||
echo "Well-known types: $PROTOC_INCLUDE"
|
||||
|
||||
# Build goctl from source
|
||||
go build -o "$GOCTL" "$GOCTL_ROOT"
|
||||
|
||||
# Clean and initialize output directory
|
||||
rm -rf "$OUT" && mkdir -p "$OUT/pb"
|
||||
(cd "$OUT" && go mod init example.com/demo/s06_wellknown > /dev/null 2>&1)
|
||||
|
||||
# Generate code
|
||||
cd "$SCRIPT_DIR"
|
||||
"$GOCTL" rpc protoc events.proto \
|
||||
--go_out="$OUT/pb" \
|
||||
--go-grpc_out="$OUT/pb" \
|
||||
--zrpc_out="$OUT/rpc" \
|
||||
--go_opt=paths=source_relative \
|
||||
--go-grpc_opt=paths=source_relative \
|
||||
--proto_path=. \
|
||||
--proto_path="$PROTOC_INCLUDE"
|
||||
|
||||
# Verify build
|
||||
echo "Running go mod tidy..."
|
||||
cd "$OUT" && go mod tidy
|
||||
echo "Checking build..."
|
||||
if go build ./...; then
|
||||
echo "✅ Build passed"
|
||||
else
|
||||
echo "❌ Build failed"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Done. Output: $OUT"
|
||||
Reference in New Issue
Block a user