Skip to content

Commit

Permalink
conduit-docs: conduit_processors_filter_tags.md (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
shiqizng committed Mar 14, 2023
1 parent 79b8292 commit 3c92877
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package fields

//go:generate go run ../gen/generate.go fields ./generated_signed_txn_map.go
//go:generate go run ../gen/generate.go fields ./generated_signed_txn_map.go ../../../../../conduit-docs/conduit_processors_filter_tags.md

import (
"fmt"
Expand Down
45 changes: 44 additions & 1 deletion conduit/plugins/processors/filterprocessor/gen/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"os"
"reflect"
"sort"
"strings"
"text/template"

Expand Down Expand Up @@ -216,6 +217,35 @@ func recursiveTagFields(theStruct interface{}, ignoreTags map[string]bool, outpu
return errors
}

func writeFieldsToFile(filepath string, fields map[string]internal.StructField) error {
fout, err := os.Create(filepath)
if err != nil {
return err
}
defer fout.Close()
// sort the tags
tags := make([]string, 0, len(fields))
for k := range fields {
tags = append(tags, k)
}
sort.Strings(tags)
_, err = fout.WriteString(fmt.Sprintf("|%s|%s|\n", "filter tag", "transaction field"))
if err != nil {
return err
}
_, err = fout.WriteString("| -------- | ------- |\n")
if err != nil {
return err
}
for _, tag := range tags {
_, err = fout.WriteString(fmt.Sprintf("|%s|%s|\n", tag, fields[tag].FieldPath))
if err != nil {
return err
}
}
return err
}

const templateStr = `// Code generated via go generate. DO NOT EDIT.
package {{ .PackageName }}
Expand All @@ -241,14 +271,19 @@ func LookupFieldByTag(tag string, input *sdk.SignedTxnWithAD) (interface{}, erro
`

// usage:
// go run generate.go packagename outputfile
// go run generate.go packagename outputfile tagsfile
func main() {
var packageName string
var outputFilepath string
var tagsFilepath string

if len(os.Args) == 3 {
packageName = os.Args[1]
outputFilepath = os.Args[2]
} else if len(os.Args) == 4 {
packageName = os.Args[1]
outputFilepath = os.Args[2]
tagsFilepath = os.Args[3]
}

if packageName == "" {
Expand Down Expand Up @@ -306,4 +341,12 @@ func main() {
fmt.Fprintf(os.Stderr, "Template execute failure: %s", err)
os.Exit(1)
}

// write filter tags to a file
if tagsFilepath != "" {
err = writeFieldsToFile(tagsFilepath, fields)
if err != nil {
fmt.Fprintf(os.Stderr, "error while creating file %s, err: %v\n", tagsFilepath, err)
}
}
}

0 comments on commit 3c92877

Please sign in to comment.