From 3c92877960bbc00f26070a3076a653e3c5da46f7 Mon Sep 17 00:00:00 2001 From: shiqizng <80276844+shiqizng@users.noreply.github.com> Date: Tue, 14 Mar 2023 15:14:54 -0400 Subject: [PATCH] conduit-docs: conduit_processors_filter_tags.md (#10) --- .../filterprocessor/fields/searcher.go | 2 +- .../filterprocessor/gen/generate.go | 45 ++++++++++++++++++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/conduit/plugins/processors/filterprocessor/fields/searcher.go b/conduit/plugins/processors/filterprocessor/fields/searcher.go index 8d764776..1b17fd39 100644 --- a/conduit/plugins/processors/filterprocessor/fields/searcher.go +++ b/conduit/plugins/processors/filterprocessor/fields/searcher.go @@ -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" diff --git a/conduit/plugins/processors/filterprocessor/gen/generate.go b/conduit/plugins/processors/filterprocessor/gen/generate.go index 0f3eafe5..4b4506f2 100644 --- a/conduit/plugins/processors/filterprocessor/gen/generate.go +++ b/conduit/plugins/processors/filterprocessor/gen/generate.go @@ -5,6 +5,7 @@ import ( "io" "os" "reflect" + "sort" "strings" "text/template" @@ -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 }} @@ -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 == "" { @@ -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) + } + } }