Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

conduit-docs: conduit_processors_filter_tags.md #10

Merged
merged 2 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
}