Skip to content

Commit fa7a95f

Browse files
authored
Merge pull request #46 from streamingfast/blo-729-substreams-init-for-ethereum-prettify-written-abi-file
Fix: Prettify ABI JSON files when written to disk
2 parents 2f86802 + 6d8bc70 commit fa7a95f

8 files changed

Lines changed: 36 additions & 11 deletions

File tree

convo.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func (c *Conversation[X]) HandleSubstreamsConsumptionChoice(value string) loop.C
7979

8080
var sinkMessage *MsgWrap
8181
var dockerComposeCmd loop.Cmd
82-
82+
8383
switch value {
8484
case "postgres":
8585
sinkMessage = c.Msg().Message(`Sink to Postgres:
@@ -235,7 +235,7 @@ func (c *Conversation[X]) CmdDownloadFiles(msg ReturnGenerate) loop.Cmd {
235235

236236
func (c *Conversation[X]) generateDockerCompose(dbType string) loop.Cmd {
237237
var dockerComposeContent string
238-
238+
239239
if dbType == "postgres" {
240240
dockerComposeContent = `version: "3"
241241
services:
@@ -284,9 +284,9 @@ services:
284284
- "9000:9000"
285285
- "9005:9005"`
286286
}
287-
287+
288288
downloadCmd := c.Action(InputSourceDownloaded{}).DownloadFiles()
289289
downloadCmd.AddFile("docker-compose.yml", []byte(dockerComposeContent), "text/plain", "Docker Compose configuration for "+dbType+" database")
290-
290+
291291
return downloadCmd.Cmd()
292292
}

evm-events-calls/generate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ func (p *Project) Generate() codegen.ReturnGenerate {
3131
}
3232

3333
for _, contract := range p.Contracts {
34-
res.ProjectFiles[fmt.Sprintf("abi/%s_contract.abi.json", contract.Name)] = []byte(contract.Abi.raw)
34+
res.ProjectFiles[fmt.Sprintf("abi/%s_contract.abi.json", contract.Name)] = codegen.PrettifyJSON([]byte(contract.Abi.raw))
3535
}
3636

3737
for _, dds := range p.DynamicContracts {
38-
res.ProjectFiles[fmt.Sprintf("abi/%s_contract.abi.json", dds.Name)] = []byte(dds.Abi.raw)
38+
res.ProjectFiles[fmt.Sprintf("abi/%s_contract.abi.json", dds.Name)] = codegen.PrettifyJSON([]byte(dds.Abi.raw))
3939
}
4040

4141
return res

evm-hello-world/chain_configs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type ChainConfig struct {
1010
DisplayName string // Public
1111
ExplorerLink string
1212
ApiEndpoint string
13-
ApiBaseURL string // Base URL without query parameters
13+
ApiBaseURL string // Base URL without query parameters
1414
ApiQueryParams url.Values // Query parameters to append (e.g. chainid=747474)
1515
ApiEndpointDirect bool
1616
FirstStreamableBlock uint64

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ module github.com/streamingfast/substreams-codegen
22

33
go 1.24.2
44

5+
toolchain go1.24.9
6+
57
require (
68
connectrpc.com/connect v1.16.2
79
github.com/NethermindEth/juno v0.3.1

server/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package server
22

33
import (
4-
_ "embed"
54
"context"
5+
_ "embed"
66
"net/http"
77
"regexp"
88
"strings"
@@ -58,7 +58,7 @@ func (s *server) Run() {
5858
ctx := context.Background()
5959
interval := 12 * time.Hour
6060
s.logger.Info("scheduling firehose-networks registry updates", zap.Duration("interval", interval))
61-
61+
6262
networks.ScheduleUpdateLatestRegistry(ctx, interval, s.logger)
6363
}()
6464

sol-anchor/idl_fields_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,4 @@ func TestUnmarshalOptionVecDefined(t *testing.T) {
9595

9696
assert.Equal(t, "vecDefined", optionType)
9797
assert.Equal(t, "string", result)
98-
}
98+
}

starknet-events/state.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (p *Project) Generate() codegen.ReturnGenerate {
3838
}
3939

4040
for _, contract := range p.Contracts {
41-
res.ProjectFiles[fmt.Sprintf("abi/%s_contract.abi.json", contract.Name)] = []byte(contract.Abi.raw)
41+
res.ProjectFiles[fmt.Sprintf("abi/%s_contract.abi.json", contract.Name)] = codegen.PrettifyJSON([]byte(contract.Abi.raw))
4242
}
4343

4444
return res

utils.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package codegen
22

33
import (
44
"archive/zip"
5+
"encoding/json"
56
"fmt"
67
"os"
78
"path/filepath"
@@ -75,3 +76,25 @@ func ZipFiles(files map[string][]byte) ([]byte, error) {
7576

7677
return zipFileB, nil
7778
}
79+
80+
// PrettifyJSON takes raw JSON bytes and returns prettified JSON with proper indentation.
81+
// If the input is not valid JSON, it returns the original content unchanged.
82+
func PrettifyJSON(rawJSON []byte) []byte {
83+
if len(rawJSON) == 0 {
84+
return rawJSON
85+
}
86+
87+
var jsonData interface{}
88+
if err := json.Unmarshal(rawJSON, &jsonData); err != nil {
89+
// If it's not valid JSON, return the original content
90+
return rawJSON
91+
}
92+
93+
prettified, err := json.MarshalIndent(jsonData, "", " ")
94+
if err != nil {
95+
// If prettification fails, return the original content
96+
return rawJSON
97+
}
98+
99+
return prettified
100+
}

0 commit comments

Comments
 (0)