Skip to content

Commit

Permalink
a bit of refactor and wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduard-Voiculescu committed Jul 22, 2024
1 parent e3e4dca commit 6ec334a
Show file tree
Hide file tree
Showing 11 changed files with 85,711 additions and 82,457 deletions.
51 changes: 51 additions & 0 deletions generator/generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package generator

import (
"bytes"
"fmt"
"os"
"text/template"

"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/streamingfast/firehose-gear/protobuf"
)

type Generator struct {
templatePath string

Messages []*protobuf.Message
Metadata *types.Metadata
}

func NewGenerator(templatePath string, messages []*protobuf.Message, metadata *types.Metadata) *Generator {
return &Generator{
templatePath: templatePath,
Messages: messages,
Metadata: metadata,
}
}

func (g *Generator) Generate() error {
b, err := os.ReadFile(g.templatePath)
if err != nil {
return fmt.Errorf("failed to read file: %w", err)
}

templ, err := template.New("").Parse(string(b))
if err != nil {
return fmt.Errorf("failed to parse template: %w", err)
}

buffer := &bytes.Buffer{}
if err := templ.Execute(buffer, g); err != nil {
return fmt.Errorf("failed to execute template: %w", err)
}

content := buffer.Bytes()
err = os.WriteFile("../templates/gen_types.go", content, 0644)
if err != nil {
return fmt.Errorf("failed to write file: %w", err)
}

return nil
}
26 changes: 26 additions & 0 deletions generator/generator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package generator

import (
"testing"

firecoreRPC "github.com/streamingfast/firehose-core/rpc"
"github.com/streamingfast/firehose-gear/rpc"
"github.com/stretchr/testify/require"
)

func Test_Generator(t *testing.T) {
gearClients := firecoreRPC.NewClients[*rpc.Client]()
gearClient := rpc.NewClient("https://vara-mainnet.public.blastapi.io")
gearClients.Add(gearClient)

mc := rpc.NewMetadataConverter(gearClients, nil, nil)
_, err := mc.Convert("")
require.NoError(t, err)

messages := mc.FetchMessages()
metadata := mc.FetchMetadata()

gen := NewGenerator("/Users/eduardvoiculescu/git/streamingFast/firehose-gear/templates/gen_types.go.gotmpl", messages, metadata)
err = gen.Generate()
require.NoError(t, err)
}
Loading

0 comments on commit 6ec334a

Please sign in to comment.