From 418df5b55c7d456f955694761fa60058901b1533 Mon Sep 17 00:00:00 2001 From: sudeep Date: Tue, 30 May 2023 20:10:40 +0530 Subject: [PATCH] use lenspath library to simplify replica maps wrapping/unwrapping --- go.mod | 1 + go.sum | 2 + internal/utils/lenspath.go | 73 ++++++++++++++ internal/utils/utils.go | 193 ++++++++----------------------------- 4 files changed, 116 insertions(+), 153 deletions(-) create mode 100644 internal/utils/lenspath.go diff --git a/go.mod b/go.mod index e2eb20fa..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 diff --git a/go.sum b/go.sum index 46acb061..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= diff --git a/internal/utils/lenspath.go b/internal/utils/lenspath.go new file mode 100644 index 00000000..b59ff981 --- /dev/null +++ b/internal/utils/lenspath.go @@ -0,0 +1,73 @@ +package utils + +import ( + "github.com/linkedin/goavro/v2" + log "github.com/sirupsen/logrus" + lensp "github.com/covalenthq/lenspath" +) + +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) map[string]interface{} { + return 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) map[string]interface{} { + return 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) map[string]interface{} { + err := lenspath.Setter(data, setter) + + if err != nil { + log.Fatal(err) + } + + return data +} + +func composeLenspath(prevLenspath *lensp.Lenspath, lens []lensp.Lens) *lensp.Lenspath { + if lenspath, err := prevLenspath.Compose(lens); err != nil { + log.Fatal(err) + return nil + } else { + return lenspath + } +} + +func createLenspath(lens []lensp.Lens) *lensp.Lenspath { + if lenspath, err := lensp.Create(lens); err != nil { + log.Fatal(err) + return nil + } else { + 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