Skip to content
Permalink
Browse files
feat: add diff method and some tests for mem object (#13)
  • Loading branch information
gxthrj committed Aug 18, 2020
1 parent 4e456d8 commit 8d2ac23392fa2abe91b9b50ade7e6f3f39b534f2
Showing 13 changed files with 523 additions and 3 deletions.
@@ -219,7 +219,7 @@ http:
name:
exact: "json"
age:
Greater: 18
greater: 18
- uris:
- prefix: "/"
3 go.mod
@@ -7,6 +7,9 @@ require (
github.com/hashicorp/go-memdb v1.2.1
github.com/onsi/ginkgo v1.14.0
github.com/onsi/gomega v1.10.1
github.com/sergi/go-diff v1.1.0 // indirect
github.com/xeipuuv/gojsonschema v1.2.0
github.com/yudai/gojsondiff v1.0.0
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect
gopkg.in/yaml.v2 v2.3.0
)
13 go.sum
@@ -1,4 +1,5 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
@@ -24,6 +25,9 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
@@ -34,14 +38,21 @@ github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7J
github.com/onsi/gomega v1.10.1 h1:o0+MgICZLuZ7xjH7Vx6zS/zcu93/BEp1VwkIW1mEXCE=
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
github.com/yudai/gojsondiff v1.0.0 h1:27cbfqXLVEJ1o8I6v3y9lg8Ydm53EKqHXAOMxEGlCOA=
github.com/yudai/gojsondiff v1.0.0/go.mod h1:AY32+k2cwILAkW1fbgxQ5mUmMiZFgLIV+FBNExI05xg=
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 h1:BHyfKlQyqbsFN5p3IfnEUduWvb9is428/nNb5L3U01M=
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82/go.mod h1:lgjkn3NuSvDfVJdfcVVdX+jpBxNmX4rDAzaS45IcYoM=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7 h1:AeiKBIuRw3UomYXSbLy0Mc2dDLfdtbT/IVn4keq83P0=
@@ -69,9 +80,11 @@ google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzi
google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM=
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
@@ -0,0 +1,61 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package mem

import (
"encoding/json"
"fmt"
"github.com/yudai/gojsondiff"
)

var (
differ = gojsondiff.New()
)

func HasDiff(a, b interface{}) (bool, error) {
aJSON, err := json.Marshal(a)
if err != nil {
return false, err
}
bJSON, err := json.Marshal(b)
if err != nil {
return false, err
}
if d, err := differ.Compare(aJSON, bJSON); err != nil {
return false, err
} else {
fmt.Println(d.Deltas())
return d.Modified(), nil
}
}

func Diff(a, b interface{}) (gojsondiff.Diff, error) {
aJSON, err := json.Marshal(a)
if err != nil {
return nil, err
}
bJSON, err := json.Marshal(b)
if err != nil {
return nil, err
}
if d, err := differ.Compare(aJSON, bJSON); err != nil {
return nil, err
} else {
return d, nil
}
}
@@ -0,0 +1,162 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package mem_test

import (
"encoding/json"
"fmt"
"github.com/apache/apisix-control-plane/pkg/mem"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("Diff", func() {
Describe("compare with mems", func() {
Context("gateway compare", func() {
It("keep the same", func() {
a := []byte(`{
"kind": "Gateway",
"name": "g1",
"full_name": "Gateway:g1",
"servers": [
{
"hosts": ["www.foo.com", "foo.com"],
"port": {
"number": 8080,
"name": "http",
"protocol": "HTTP"
}
},
{
"hosts": ["www.foo2.com", "foo2.com"],
"port": {
"number": 8082,
"name": "http2",
"protocol": "HTTP"
}
}
]
}`)
b := []byte(`{
"kind": "Gateway",
"name": "g1",
"full_name": "Gateway:g1",
"servers": [
{
"hosts": ["www.foo.com", "foo.com"],
"port": {
"number": 8080,
"name": "http",
"protocol": "HTTP"
}
},
{
"hosts": ["www.foo2.com", "foo2.com"],
"port": {
"number": 8082,
"name": "http2",
"protocol": "HTTP"
}
}
]
}`)
var ga *mem.Gateway
if err := json.Unmarshal(a, &ga); err != nil {
fmt.Println(err)
}
var gb *mem.Gateway
if err := json.Unmarshal(b, &gb); err != nil {
fmt.Println(err)
}

if result, err := mem.HasDiff(*ga, *gb); err != nil {
fmt.Println(err)
} else {
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(false))
}
})
})

Context("gateway compare", func() {
It("different because of the sort of servers", func() {
a := []byte(`{
"kind": "Gateway",
"name": "g1",
"full_name": "Gateway:g1",
"servers": [
{
"hosts": ["www.foo.com", "foo.com"],
"port": {
"number": 8080,
"name": "http",
"protocol": "HTTP"
}
},
{
"hosts": ["www.foo2.com", "foo2.com"],
"port": {
"number": 8082,
"name": "http2",
"protocol": "HTTP"
}
}
]
}`)
b := []byte(`{
"kind": "Gateway",
"name": "g1",
"full_name": "Gateway:g1",
"servers": [
{
"hosts": ["www.foo2.com", "foo2.com"],
"port": {
"number": 8082,
"name": "http2",
"protocol": "HTTP"
}
},
{
"hosts": ["www.foo.com", "foo.com"],
"port": {
"number": 8080,
"name": "http",
"protocol": "HTTP"
}
}
]
}`)
var ga *mem.Gateway
if err := json.Unmarshal(a, &ga); err != nil {
fmt.Println(err)
}
var gb *mem.Gateway
if err := json.Unmarshal(b, &gb); err != nil {
fmt.Println(err)
}

if result, err := mem.HasDiff(*ga, *gb); err != nil {
fmt.Println(err)
} else {
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(true))
}
})
})
})
})
@@ -17,12 +17,17 @@

package mem

import (
"fmt"
)

type GatewayDB struct {
Gateways []*Gateway
}

// insert Gateway to memDB
func (db *GatewayDB) Insert() error {
fmt.Println(DB)
txn := DB.Txn(true)
defer txn.Abort()
for _, r := range db.Gateways {
@@ -34,6 +39,20 @@ func (db *GatewayDB) Insert() error {
return nil
}

func (g *Gateway) FindByFullName() (*Gateway, error) {
txn := DB.Txn(false)
defer txn.Abort()
if raw, err := txn.First(GatewayTable, "id", *g.FullName); err != nil {
return nil, err
} else {
if raw != nil {
currentGateway := raw.(*Gateway)
return currentGateway, nil
}
return nil, fmt.Errorf("NOT FOUND")
}
}

func (g *Gateway) Diff(t MemModel) bool {
return true
}
@@ -0,0 +1,30 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package mem_test

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestMem(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Mem Suite")
}

0 comments on commit 8d2ac23

Please sign in to comment.