-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
can configure data source priorities, allow to translate URL form dat…
…a committee, elderberry-validium not implemented
- Loading branch information
1 parent
eebe6eb
commit 90bb862
Showing
8 changed files
with
109 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package translator | ||
|
||
type ConfigRuleFullMatch struct { | ||
ContextName string `mapstructure:"ContextName"` | ||
Old string `mapstructure:"Old"` | ||
New string `mapstructure:"New"` | ||
} | ||
|
||
type Config struct { | ||
FullMatchRules []ConfigRuleFullMatch `mapstructure:"UniversalFullRules"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package translator | ||
|
||
type Translator interface { | ||
Translate(contextName string, data string) string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package translator | ||
|
||
import "github.com/0xPolygonHermez/zkevm-synchronizer-l1/log" | ||
|
||
type TranslatorFullMatchRule struct { | ||
// If null match any context | ||
ContextName *string | ||
// If null match any data | ||
FullMatchString string | ||
NewString string | ||
} | ||
|
||
func (t *TranslatorFullMatchRule) Match(contextName string, data string) bool { | ||
if t.ContextName != nil && *t.ContextName != contextName { | ||
return false | ||
} | ||
if t.FullMatchString != data { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func (t *TranslatorFullMatchRule) Translate(contextName string, data string) string { | ||
return t.NewString | ||
} | ||
|
||
func NewTranslatorFullMatchRule(contextName *string, fullMatchString string, newString string) *TranslatorFullMatchRule { | ||
return &TranslatorFullMatchRule{ | ||
ContextName: contextName, | ||
FullMatchString: fullMatchString, | ||
NewString: newString, | ||
} | ||
} | ||
|
||
type TranslatorImpl struct { | ||
FullMatchRules []TranslatorFullMatchRule | ||
} | ||
|
||
func NewTranslatorImpl() *TranslatorImpl { | ||
return &TranslatorImpl{ | ||
FullMatchRules: []TranslatorFullMatchRule{}, | ||
} | ||
} | ||
|
||
func (t *TranslatorImpl) Translate(contextName string, data string) string { | ||
for _, rule := range t.FullMatchRules { | ||
if rule.Match(contextName, data) { | ||
translated := rule.Translate(contextName, data) | ||
log.Debugf("Translated (ctxName=%s) %s to %s", contextName, data, translated) | ||
return translated | ||
} | ||
} | ||
return data | ||
} | ||
|
||
func (t *TranslatorImpl) AddRule(rule TranslatorFullMatchRule) { | ||
t.FullMatchRules = append(t.FullMatchRules, rule) | ||
} | ||
|
||
func (t *TranslatorImpl) AddConfigRules(cfg Config) { | ||
for _, v := range cfg.FullMatchRules { | ||
var contextName *string | ||
if v.ContextName != "" { | ||
contextName = &v.ContextName | ||
} | ||
rule := NewTranslatorFullMatchRule(contextName, v.Old, v.New) | ||
t.AddRule(*rule) | ||
} | ||
} |