/
genesis.go
46 lines (38 loc) · 1.3 KB
/
genesis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package typed
import (
"context"
"fmt"
"strings"
"github.com/tendermint/starport/starport/pkg/placeholder"
"github.com/tendermint/starport/starport/pkg/protoanalysis"
)
// ProtoGenesisStateMessage is the name of the proto message that represents the genesis state
const ProtoGenesisStateMessage = "GenesisState"
// PatchGenesisTypeImport patches types/genesis.go content from the issue:
// https://github.com/tendermint/starport/issues/992
func PatchGenesisTypeImport(replacer placeholder.Replacer, content string) string {
patternToCheck := "import ("
replacement := fmt.Sprintf(`import (
%[1]v
)`, PlaceholderGenesisTypesImport)
if !strings.Contains(content, patternToCheck) {
content = replacer.Replace(content, PlaceholderGenesisTypesImport, replacement)
}
return content
}
// GenesisStateHighestFieldNumber returns the highest field number in the genesis state proto message
// This allows to determine next the field numbers
func GenesisStateHighestFieldNumber(path string) (int, error) {
pkgs, err := protoanalysis.Parse(context.Background(), nil, path)
if err != nil {
return 0, err
}
if len(pkgs) == 0 {
return 0, fmt.Errorf("%s is not a proto file", path)
}
m, err := pkgs[0].MessageByName(ProtoGenesisStateMessage)
if err != nil {
return 0, err
}
return m.HighestFieldNumber, nil
}