diff --git a/go.mod b/go.mod index 250c1d5a..6027e42b 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/banzaicloud/logrus-runtime-formatter v0.0.0-20190729070250-5ae5475bae5e github.com/btcsuite/btcd v0.22.0-beta // indirect github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect + github.com/covalenthq/lenspath v0.3.2 github.com/covalenthq/lumberjack/v3 v3.0.1 github.com/elodina/go-avro v0.0.0-20160406082632-0c8185d9a3ba github.com/ethereum/go-ethereum v1.11.5 @@ -17,7 +18,7 @@ require ( github.com/kelseyhightower/envconfig v1.4.0 github.com/linkedin/goavro/v2 v2.12.0 github.com/satori/go.uuid v1.2.0 - github.com/sirupsen/logrus v1.9.0 + github.com/sirupsen/logrus v1.9.3 github.com/ubiq/go-ubiq v3.0.1+incompatible golang.org/x/sys v0.8.0 google.golang.org/api v0.122.0 // indirect diff --git a/go.sum b/go.sum index 6fd7bed2..b0e4d921 100644 --- a/go.sum +++ b/go.sum @@ -135,6 +135,8 @@ github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3Ee github.com/coreos/go-systemd v0.0.0-20180511133405-39ca1b05acc7/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/coreos/pkg v0.0.0-20160727233714-3ac0863d7acf/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= +github.com/covalenthq/lenspath v0.3.2 h1:XJgc91mff1sBeB3lvIgM7RCUM+0ZAEeEToaaslKjrAY= +github.com/covalenthq/lenspath v0.3.2/go.mod h1:+9jSB9uDfvH4ga4nxucxE3eU+AqDK/PexXd2BP5xNBo= github.com/covalenthq/lumberjack/v3 v3.0.1 h1:WDfNlLMTcMWiLaMEUcku9Jfcb5d8mOq4n5cIPA5l2Tg= github.com/covalenthq/lumberjack/v3 v3.0.1/go.mod h1:cpmebtW0NtC50USEqIlBtuoTZuxWIZGMT1cXfPKduII= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= @@ -572,8 +574,8 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeV github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= -github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= -github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= diff --git a/internal/utils/lenspath.go b/internal/utils/lenspath.go new file mode 100644 index 00000000..0e207515 --- /dev/null +++ b/internal/utils/lenspath.go @@ -0,0 +1,76 @@ +package utils + +import ( + lensp "github.com/covalenthq/lenspath" + "github.com/linkedin/goavro/v2" + log "github.com/sirupsen/logrus" +) + +var dataLens = createLenspath([]string{"replicaEvent", "*", "data"}) +var transactionsLens = composeLenspath(dataLens, []string{"Transactions", "*"}) +var vLens = composeLenspath(transactionsLens, []string{"v"}) +var rLens = composeLenspath(transactionsLens, []string{"r"}) +var sLens = composeLenspath(transactionsLens, []string{"s"}) +var toLens = composeLenspath(transactionsLens, []string{"to"}) +var fromLens = composeLenspath(transactionsLens, []string{"from"}) + +var headerLens = composeLenspath(dataLens, []string{"Header"}) +var withdrawalsRootLens = composeLenspath(headerLens, []string{"withdrawalsRoot"}) + +var withdrawalsLens = composeLenspath(dataLens, []string{"Withdrawals"}) +var uncleLens = composeLenspath(dataLens, []string{"Uncles"}) + +// utilities for lenspath + +func unwrapType(data map[string]interface{}, lenspath *lensp.Lenspath, nonNilType string) { + lenspathSetter(data, lenspath, func(leafd any) any { + if leafd == nil { + return nil + } + + mp := leafd.(map[string]interface{}) + + return mp[nonNilType] + }) +} + +func wrapType(data map[string]interface{}, lenspath *lensp.Lenspath, nonNilType string) { + lenspathSetter(data, lenspath, func(leafd any) any { + wType := nonNilType + if leafd == nil { + wType = "null" + } + + return goavro.Union(wType, leafd) + }) +} + +func lenspathSetter(data map[string]interface{}, lenspath *lensp.Lenspath, setter func(leafd any) any) { + err := lenspath.Setter(data, setter) + + if err != nil { + log.Fatal(err) + } +} + +func composeLenspath(prevLenspath *lensp.Lenspath, lens []lensp.Lens) *lensp.Lenspath { + lenspath, err := prevLenspath.Compose(lens) + if err != nil { + log.Fatal(err) + + return nil + } + + return lenspath +} + +func createLenspath(lens []lensp.Lens) *lensp.Lenspath { + lenspath, err := lensp.Create(lens) + if err != nil { + log.Fatal(err) + + return nil + } + + return lenspath +} diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 6b5daf9b..7ee8414c 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -20,7 +20,6 @@ import ( "github.com/elodina/go-avro" "github.com/ethereum/go-ethereum/ethclient" "github.com/go-redis/redis/v7" - "github.com/linkedin/goavro/v2" log "github.com/sirupsen/logrus" "golang.org/x/sys/unix" ) @@ -213,166 +212,54 @@ func Writable(path string) bool { return unix.Access(path, unix.W_OK) == nil } -// UnwrapAvroUnion "unwraps" the "to" field from the replica map -// -//nolint:varnamelen +// UnwrapAvroUnion unwraps avro wrapped maps func UnwrapAvroUnion(data map[string]interface{}) map[string]interface{} { - vs := data - for k1 := range data { - if k1 == "replicaEvent" { - m1 := data[k1].([]interface{}) - vsr := m1 - for k2 := range m1 { - m2 := m1[k2].(map[string]interface{}) - vso := m2 - for k3 := range m2 { - if k3 == "data" { - m3 := m2[k3].(map[string]interface{}) - vsd := m3 - for k4 := range m3 { - switch k4 { - case "Transactions": - m4 := m3[k4].([]interface{}) - vst := m4 - for k5 := range m4 { - m5 := m4[k5].(map[string]interface{}) - vsm := make(map[string]interface{}) - for k6, v6 := range m5 { - switch { - case (k6 == "to" || k6 == "from") && v6 != nil: - m6 := v6.(map[string]interface{}) - if v7, ok := m6["string"]; ok { - vsm[k6] = v7 - } - case (k6 == "v" || k6 == "r" || k6 == "s") && v6 != nil: - m6 := v6.(map[string]interface{}) - if v7, ok := m6["bytes"]; ok { - vsm[k6] = v7 - } - default: - vsm[k6] = v6 - } - } - vst[k5] = vsm - } - vsd[k4] = vst - - case "Header": - m4 := m3[k4].(map[string]interface{}) - vst := m4 - for k5, v5 := range m4 { - if k5 == "withdrawalsRoot" && v5 != nil { - m5 := v5.(map[string]interface{}) - if v6, ok := m5["string"]; ok { - vst[k5] = v6 - } - } - } - vsd[k4] = vst - - case "Withdrawals", "Uncles": - m4 := m3[k4].(map[string]interface{}) - if m3[k4] == nil { - vsd[k4] = nil - } else { - vsd[k4] = m4["array"] - } - } - } - vso[k3] = vsd - } - } - vsr[k2] = vso - } - vs[k1] = vsr - } + if data == nil { + return nil } - return vs + // v, r, s + unwrapType(data, vLens, "bytes") + unwrapType(data, rLens, "bytes") + unwrapType(data, sLens, "bytes") + + // to, from + unwrapType(data, toLens, "string") + unwrapType(data, fromLens, "string") + + // withdrawalsRoot + unwrapType(data, withdrawalsRootLens, "string") + + // withdrawals, uncles + unwrapType(data, withdrawalsLens, "array") + unwrapType(data, uncleLens, "array") + + return data } -// MapToAvroUnion converts the "to" field in the replica map to an Avro Union type allowing -// -//nolint:varnamelen +// MapToAvroUnion converts the several field in the replica map to an Avro Union type allowing func MapToAvroUnion(data map[string]interface{}) map[string]interface{} { - vs := data - for k1 := range data { - if k1 == "replicaEvent" { - m1 := data[k1].([]interface{}) - vsr := m1 - for k2 := range m1 { - m2 := m1[k2].(map[string]interface{}) - vso := m2 - for k3 := range m2 { - if k3 == "data" { - m3 := m2[k3].(map[string]interface{}) - vsd := m3 - for k4 := range m3 { - switch k4 { - case "Transactions": - m4 := m3[k4].([]interface{}) - vst := m4 - for k5 := range m4 { - m5 := m4[k5].(map[string]interface{}) - vsm := m5 - for k6, v6 := range m5 { - if k6 == "to" || k6 == "from" { - if v6 == nil { - vsm[k6] = goavro.Union("null", nil) - } else { - vsm[k6] = goavro.Union("string", v6) - } - } else if k6 == "v" || k6 == "r" || k6 == "s" { - if v6 == nil { - vsm[k6] = goavro.Union("null", nil) - } else { - vsm[k6] = goavro.Union("bytes", v6) - } - } - } - vst[k5] = vsm - } - vsd[k4] = vst - - case "Header": - m4 := m3[k4].(map[string]interface{}) - vst := m4 - for k5, v5 := range m4 { - if k5 == "withdrawalsRoot" { - if v5 == nil { - vst[k5] = goavro.Union("null", nil) - } else { - vst[k5] = goavro.Union("string", v5) - } - } - } - vsd[k4] = vst - - case "Withdrawals", "Uncles": - if m3[k4] == nil { - vsd[k4] = goavro.Union("null", nil) - } else { - m4 := m3[k4].([]interface{}) - vsd[k4] = goavro.Union("array", m4) - } - } - } - if vsd["Withdrawals"] == nil { - vsd["Withdrawals"] = goavro.Union("null", nil) - } - if vsd["Uncles"] == nil { - vsd["Uncles"] = goavro.Union("null", nil) - } - vso[k3] = vsd - } - } - vsr[k2] = vso - } - vs[k1] = vsr - } + if data == nil { + return nil } - return vs + // v,r,s + wrapType(data, vLens, "bytes") + wrapType(data, rLens, "bytes") + wrapType(data, sLens, "bytes") + + // to, from + wrapType(data, toLens, "string") + wrapType(data, fromLens, "string") + + // withdrawalsRoot + wrapType(data, withdrawalsRootLens, "string") + + // withdrawals, uncles + wrapType(data, withdrawalsLens, "array") + wrapType(data, uncleLens, "array") + + return data } // Version Provides version info on bsp agent binary