Skip to content

Commit 035906c

Browse files
feat: add market-data-unwrap example and fix generators for cross-package types
- Add complete market-data-unwrap example demonstrating the unwrap annotation for map values that should serialize as arrays in JSON - Fix mock generator to properly handle map fields - Fix unwrap generator to use qualified type names for imported messages - Fix client generator to conditionally import "bytes" (only when needed) - Fix unwrap detection to check imported messages directly The example shows a real-world use case based on the Alpaca Market Data API, demonstrating how map<string, BarList> serializes as {"SYMBOL": [...]} instead of {"SYMBOL": {"bars": [...]}}. Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent f7f4ba1 commit 035906c

25 files changed

+3606
-18
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Generated code
2+
api/
3+
docs/
4+
5+
# Go build artifacts
6+
*.exe
7+
*.exe~
8+
*.dll
9+
*.so
10+
*.dylib
11+
*.test
12+
*.out
13+
14+
# IDE
15+
.idea/
16+
.vscode/
17+
*.swp
18+
*.swo
19+
20+
# OS
21+
.DS_Store
22+
Thumbs.db
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
.PHONY: demo install generate run client clean test test-unwrap
2+
3+
# Complete demo workflow - installs tools, generates code, runs server
4+
demo:
5+
@rm -rf api docs
6+
@echo "🚀 Running market data unwrap demo..."
7+
@$(MAKE) generate
8+
@echo ""
9+
@echo "✅ Demo ready! Starting server..."
10+
@echo ""
11+
@$(MAKE) run
12+
13+
# Install required tools
14+
install:
15+
@echo "Installing sebuf plugins..."
16+
@go install github.com/bufbuild/buf/cmd/buf@latest
17+
@go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
18+
@GOPROXY=direct go install github.com/SebastienMelki/sebuf/cmd/protoc-gen-go-http@latest
19+
@GOPROXY=direct go install github.com/SebastienMelki/sebuf/cmd/protoc-gen-go-client@latest
20+
@GOPROXY=direct go install github.com/SebastienMelki/sebuf/cmd/protoc-gen-openapiv3@latest
21+
@echo "✅ Tools installed"
22+
23+
# Generate code from proto files
24+
generate:
25+
@echo "Fetching dependencies..."
26+
@buf dep update
27+
@echo "Generating code..."
28+
@buf generate
29+
@echo "Updating Go modules..."
30+
@go mod tidy
31+
@echo "✅ Code generated and dependencies updated"
32+
33+
# Run the server
34+
run: generate
35+
@go run main.go
36+
37+
# Run the client example (requires server running)
38+
client:
39+
@go run client_example.go
40+
41+
# Clean generated files
42+
clean:
43+
@rm -rf api docs
44+
@echo "✅ Cleaned generated files"
45+
46+
# Test the API endpoints with valid unwrapped response
47+
test:
48+
@echo "Testing API endpoints..."
49+
@echo ""
50+
@echo "1. Get option bars (demonstrates unwrap serialization):"
51+
@curl -s -X GET "http://localhost:8080/v2/options/bars?symbols=TSLA260123C00335000&timeframe=1Day" \
52+
-H "APCA-API-KEY-ID: test-key" \
53+
-H "APCA-API-SECRET-KEY: test-secret" \
54+
| python3 -m json.tool
55+
@echo ""
56+
@echo "2. Get latest option bars:"
57+
@curl -s -X GET "http://localhost:8080/v2/options/bars/latest?symbols=TSLA260123C00335000" \
58+
-H "APCA-API-KEY-ID: test-key" \
59+
-H "APCA-API-SECRET-KEY: test-secret" \
60+
| python3 -m json.tool
61+
62+
# Test to show the unwrap behavior - compare JSON structure
63+
test-unwrap:
64+
@echo "Demonstrating unwrap behavior..."
65+
@echo ""
66+
@echo "With unwrap annotation, the response looks like:"
67+
@echo '{"bars": {"TSLA260123C00335000": [{"c": 143.08, ...}, {"c": 145.34, ...}]}}'
68+
@echo ""
69+
@echo "Without unwrap, it would look like:"
70+
@echo '{"bars": {"TSLA260123C00335000": {"bars": [{"c": 143.08, ...}, {"c": 145.34, ...}]}}}'
71+
@echo ""
72+
@echo "Making actual request to see the unwrapped response:"
73+
@curl -s -X GET "http://localhost:8080/v2/options/bars?symbols=TSLA260123C00335000&timeframe=1Day" \
74+
-H "APCA-API-KEY-ID: test-key" \
75+
-H "APCA-API-SECRET-KEY: test-secret" \
76+
| python3 -m json.tool
77+
78+
# Test validation errors
79+
test-validation:
80+
@echo "Testing validation errors..."
81+
@echo ""
82+
@echo "1. Missing required symbol parameter (should return 400):"
83+
@curl -s -X GET "http://localhost:8080/v2/options/bars?timeframe=1Day" \
84+
-H "APCA-API-KEY-ID: test-key" \
85+
-H "APCA-API-SECRET-KEY: test-secret" \
86+
| python3 -m json.tool
87+
@echo ""
88+
@echo "2. Invalid sort value (should return 400):"
89+
@curl -s -X GET "http://localhost:8080/v2/options/bars?symbols=TSLA&timeframe=1Day&sort=invalid" \
90+
-H "APCA-API-KEY-ID: test-key" \
91+
-H "APCA-API-SECRET-KEY: test-secret" \
92+
| python3 -m json.tool
93+
@echo ""
94+
@echo "3. Missing required header (should return 400):"
95+
@curl -s -X GET "http://localhost:8080/v2/options/bars?symbols=TSLA&timeframe=1Day" \
96+
| python3 -m json.tool

0 commit comments

Comments
 (0)