Skip to content

Commit

Permalink
initial code with mbornder qntfy#93
Browse files Browse the repository at this point in the history
  • Loading branch information
kwangsuk committed May 9, 2022
1 parent f202606 commit 13cdfed
Show file tree
Hide file tree
Showing 9 changed files with 899 additions and 17 deletions.
15 changes: 13 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
module github.com/qntfy/kazaam/v4

go 1.12
replace github.com/qntfy/kazaam/v4 => /home/kkim/next-gen/kwan-kazaam/kazaam

replace github.com/qntfy/kazaam/v4/transform => /home/kkim/next-gen/kwan-kazaam/kazaam/transform

replace github.com/qntfy/kazaam/v4/converter => /home/kkim/next-gen/kwan-kazaam/kazaam/converter

replace github.com/qntfy/kazaam/v4/registry => /home/kkim/next-gen/kwan-kazaam/kazaam/registry

go 1.17

require (
github.com/gofrs/uuid v3.2.0+incompatible
github.com/gofrs/uuid v4.2.0+incompatible
github.com/pkg/errors v0.9.1
github.com/qntfy/jsonparser v1.0.2
)

require github.com/kwangsuk/kazaam v3.4.9+incompatible // indirect
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
github.com/gofrs/uuid v3.2.0+incompatible h1:y12jRkkFxsd7GpqdSZ+/KCs/fJbqpEXSGd4+jfEaewE=
github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gofrs/uuid v4.2.0+incompatible h1:yyYWMnhkhrKwwr8gAOcOCYxOOscHgDS9yZgBrnJfGa0=
github.com/gofrs/uuid v4.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/kwangsuk/kazaam v3.4.9+incompatible h1:6kNMRHzz/b8z5b5u09c2Pcp+iDsq2fORfewcK0bbgMo=
github.com/kwangsuk/kazaam v3.4.9+incompatible/go.mod h1:kMKQGVuauUGV/3qm0c00kPaXl6jCG196X9JVnNLfK8w=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/qntfy/jsonparser v1.0.2 h1:hko+J4L7HSaYoB2yuzinWc9MkO93zWKUmzPHJwB53OM=
github.com/qntfy/jsonparser v1.0.2/go.mod h1:F+LCdwPnFBsubQ+ugnBczIP9RWv5wSCqnUmLHPUx4ZU=
52 changes: 49 additions & 3 deletions kazaam.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"strings"

"github.com/qntfy/jsonparser"
"github.com/qntfy/kazaam/v4/converter"
"github.com/qntfy/kazaam/v4/registry"
"github.com/qntfy/kazaam/v4/transform"
)

Expand All @@ -23,10 +25,11 @@ import (
// Transforms should strive to fail gracefully whenever possible.
type TransformFunc func(spec *transform.Config, data []byte) ([]byte, error)

var validSpecTypes map[string]TransformFunc
var defaultSpecTypes map[string]TransformFunc
var defaultConverters map[string]registry.Converter

func init() {
validSpecTypes = map[string]TransformFunc{
defaultSpecTypes = map[string]TransformFunc{
"pass": transform.Pass,
"shift": transform.Shift,
"extract": transform.Extract,
Expand All @@ -36,6 +39,36 @@ func init() {
"coalesce": transform.Coalesce,
"timestamp": transform.Timestamp,
"uuid": transform.UUID,
"steps": transform.Steps,
"merge": transform.Merge,
}

defaultConverters = map[string]registry.Converter{
"ston": &converter.Ston{},
"ntos": &converter.Ntos{},
"regex": &converter.Regex{},
"mapped": &converter.Mapped{},
"upper": &converter.Upper{},
"lower": &converter.Lower{},
"trim": &converter.Trim{},
"substr": &converter.Substr{},
"add": &converter.Add{},
"mul": &converter.Mul{},
"round": &converter.Round{},
"ceil": &converter.Ceil{},
"floor": &converter.Floor{},
"format": &converter.Format{},
"div": &converter.Div{},
"len": &converter.Len{},
"splitn": &converter.Splitn{},
"eqs": &converter.Eqs{},
"not": &converter.Not{},
"split": &converter.Split{},
"join": &converter.Join{},
}

for name, conv := range defaultConverters {
registry.RegisterConverter(name, conv)
}
}

Expand Down Expand Up @@ -80,7 +113,7 @@ type Config struct {
func NewDefaultConfig() Config {
// make a copy, otherwise if new transforms are registered, they'll affect the whole package
specTypes := make(map[string]TransformFunc)
for k, v := range validSpecTypes {
for k, v := range defaultSpecTypes {
specTypes[k] = v
}
return Config{transforms: specTypes}
Expand Down Expand Up @@ -178,6 +211,16 @@ func (k *Kazaam) Transform(data []byte) ([]byte, error) {
return d, err
}

func initConverters(config *map[string]interface{}) {
for name, converterConfig := range *config {
c := registry.GetConverter(name)
if c != nil {
bytes, _ := json.Marshal(converterConfig)
c.Init(bytes)
}
}
}

// TransformInPlace takes the byte slice `data`, transforms it according
// to the loaded spec, and modifies the byte slice in place.
//
Expand All @@ -194,6 +237,9 @@ func (k *Kazaam) TransformInPlace(data []byte) ([]byte, error) {

var err error
for _, specObj := range k.specJSON {
if specObj.ConvertersConfig != nil {
initConverters(specObj.ConvertersConfig)
}
if specObj.Config != nil && specObj.Over != nil {
var transformedDataList [][]byte
var overKeys []string
Expand Down
2 changes: 1 addition & 1 deletion kazaam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestReregisterKazaamTransform(t *testing.T) {
}

func TestDefaultTransformsSetCardinarily(t *testing.T) {
if len(validSpecTypes) != 9 {
if len(defaultSpecTypes) != 11 {
t.Error("Unexpected number of default transforms. Missing tests?")
}
}
Expand Down
5 changes: 3 additions & 2 deletions spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (
// describes the configuration of the transform.
type spec struct {
*transform.Config
Operation *string `json:"operation"`
Over *string `json:"over,omitempty"`
Operation *string `json:"operation"`
Over *string `json:"over,omitempty"`
ConvertersConfig *map[string]interface{} `json:"converters"`
}

type specInt spec
Expand Down
3 changes: 3 additions & 0 deletions transform/shift.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ func Shift(spec *Config, data []byte) ([]byte, error) {
} else {
dataForV, err = getJSONRaw(data, v, spec.Require, spec.KeySeparator)
if err != nil {
if _, ok := err.(CPathSkipError); ok { // was a conditional path,
continue
}
return nil, err
}
}
Expand Down
62 changes: 62 additions & 0 deletions transform/shift_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,65 @@ func TestShiftWithEndArrayAccess(t *testing.T) {
t.FailNow()
}
}

func TestSkipConditionalPath(t *testing.T) {
jsonOut := `{"Rating":3,"example":{"old":{"value":3}}}`
spec := `{"Rating": "rating.primary.value","example.old": "rating.example","conditional.skip":"path.not.found?"}`

cfg := getConfig(spec, false)
kazaamOut, _ := getTransformTestWrapper(Shift, cfg, testJSONInput)
areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut))

if !areEqual {
t.Error("Transformed data does not match expectation.")
t.Log("Expected: ", jsonOut)
t.Log("Actual: ", kazaamOut)
t.FailNow()
}
}

func TestOc2SonicConditionalPath(t *testing.T) {
jsonOut := `{
"sonic-portchannel:sonic-portchannel": {
"PORTCHANNEL": {
"PORTCHANNEL_LIST": [
{
"name": "PortChannel2",
"static": "true"
}
]
}
}
}`
spec := `{"sonic-portchannel:sonic-portchannel.PORTCHANNEL.PORTCHANNEL_LIST.name": "openconfig-interfaces:interfaces.interface[0].name | substr 0 10 == \"PortChannel\":",
"sonic-portchannel:sonic-portchannel.PORTCHANNEL.PORTCHANNEL_LIST.static": "openconfig-interfaces:interfaces.interface[0].openconfig-if-aggregate:aggregation.config.lag-type == \"STATIC\":" }`
jsonInput := `{
"openconfig-interfaces:interfaces": {
"interface": [
{
"name": "PortChannel2",
"config": {
"name": "PortChannel2",
"type": "iana-if-type:ieee8023adLag"
},
"openconfig-if-aggregate:aggregation": {
"config": {
"lag-type": "STATIC"
}
}
}
]
}
}`

cfg := getConfig(spec, false)
kazaamOut, _ := getTransformTestWrapper(Shift, cfg, jsonInput)
areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut))

if !areEqual {
t.Error("Transformed data does not match expectation.")
t.Log("Expected: ", jsonOut)
t.Log("Actual: ", kazaamOut)
t.FailNow()
}
}
Loading

0 comments on commit 13cdfed

Please sign in to comment.