Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
feat: add gateway yaml struct transform && refactor pkg yaml -> yml (#7)
* feat: add gateway yaml struct transform && refactor pkg yaml -> yml * fix: Gateway yaml struct do not need ID field
- Loading branch information
Showing
11 changed files
with
235 additions
and
19 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
@@ -7,4 +7,5 @@ require ( | ||
github.com/onsi/ginkgo v1.14.0 | ||
github.com/onsi/gomega v1.10.1 | ||
github.com/xeipuuv/gojsonschema v1.2.0 | ||
gopkg.in/yaml.v2 v2.3.0 | ||
) |
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
@@ -0,0 +1,66 @@ | ||
/* | ||
* 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 | ||
|
||
type Route struct { | ||
ID *string `json:"id,omitempty" yml:"id,omitempty"` | ||
Group *string `json:"group,omitempty" yml:"group,omitempty"` | ||
FullName *string `json:"full_name,omitempty" yml:"full_name,omitempty"` | ||
ResourceVersion *string `json:"resource_version,omitempty" yml:"resource_version,omitempty"` | ||
Host *string `json:"host,omitempty" yml:"host,omitempty"` | ||
Path *string `json:"path,omitempty" yml:"path,omitempty"` | ||
Name *string `json:"name,omitempty" yml:"name,omitempty"` | ||
Methods []*string `json:"methods,omitempty" yml:"methods,omitempty"` | ||
ServiceId *string `json:"service_id,omitempty" yml:"service_id,omitempty"` | ||
ServiceName *string `json:"service_name,omitempty" yml:"service_name,omitempty"` | ||
UpstreamId *string `json:"upstream_id,omitempty" yml:"upstream_id,omitempty"` | ||
UpstreamName *string `json:"upstream_name,omitempty" yml:"upstream_name,omitempty"` | ||
Plugins []*Plugin `json:"plugins,omitempty" yml:"plugins,omitempty"` | ||
} | ||
|
||
type Upstream struct { | ||
ID *string `json:"id,omitempty" yml:"id,omitempty"` | ||
FullName *string `json:"full_name,omitempty" yml:"full_name,omitempty"` | ||
Group *string `json:"group,omitempty" yml:"group,omitempty"` | ||
ResourceVersion *string `json:"resource_version,omitempty" yml:"resource_version,omitempty"` | ||
Name *string `json:"name,omitempty" yml:"name,omitempty"` | ||
Type *string `json:"type,omitempty" yml:"type,omitempty"` | ||
HashOn *string `json:"hash_on,omitemtpy" yml:"hash_on,omitempty"` | ||
Key *string `json:"key,omitempty" yml:"key,omitempty"` | ||
Nodes []*Node `json:"nodes,omitempty" yml:"nodes,omitempty"` | ||
FromKind *string `json:"from_kind,omitempty" yml:"from_kind,omitempty"` | ||
} | ||
|
||
type Node struct { | ||
IP *string `json:"ip,omitempty" yml:"ip,omitempty"` | ||
Port *int `json:"port,omitempty" yml:"port,omitempty"` | ||
Weight *int `json:"weight,omitempty" yml:"weight,omitempty"` | ||
} | ||
|
||
type Plugin struct { | ||
ID *string `json:"id,omitempty"` | ||
Selector Selector `json:"selector"` | ||
Sort []PluginSchema `json:"sort"` | ||
} | ||
|
||
type Selector map[string]string | ||
|
||
type PluginSchema struct { | ||
Name string `json:"name"` | ||
Conf interface{} `json:"conf"` | ||
} |
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
@@ -0,0 +1,48 @@ | ||
/* | ||
* 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 yml | ||
|
||
type YmlModel interface { | ||
ToMem() string | ||
Type() string | ||
} | ||
|
||
type Gateway struct { | ||
Kind *string `json:"kind"` | ||
Name *string `json:"name"` | ||
Servers []Server `json:"servers"` | ||
} | ||
|
||
type Server struct { | ||
Port *Port `json:"port,omitempty"` | ||
Hosts []string `json:"host,omitempty"` | ||
} | ||
|
||
type Port struct { | ||
Number int `json:"number"` | ||
Name string `json:"name"` | ||
Protocol string `json:"protocol"` | ||
} | ||
|
||
func (g *Gateway) ToMem() string { | ||
return "gateway" | ||
} | ||
|
||
func (g *Gateway) Type() string { | ||
return "Gateway" | ||
} |
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
@@ -14,7 +14,7 @@ | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package yaml | ||
package yml | ||
|
||
import ( | ||
"fmt" | ||
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
@@ -14,7 +14,7 @@ | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package yaml | ||
package yml | ||
|
||
import "github.com/ghodss/yaml" | ||
|
||
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
@@ -0,0 +1,59 @@ | ||
/* | ||
* 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 yml | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
func Trans(b []byte, y []byte) YmlModel { | ||
// 1.trans with kind | ||
var yMap map[string]interface{} | ||
if err := json.Unmarshal(b, &yMap); err != nil { | ||
fmt.Println("trans to map error") | ||
return nil | ||
} else { | ||
kind := yMap["kind"] | ||
switch kind { | ||
case "Gateway": | ||
// trans to Gateway | ||
if g, err := ToGateway(y); err != nil { | ||
fmt.Println("trans to Gateway error ", err) | ||
return nil | ||
} else { | ||
fmt.Println(g) | ||
return g | ||
} | ||
default: | ||
fmt.Println("nil") | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
func ToGateway(y []byte) (*Gateway, error) { | ||
var g *Gateway | ||
if err := yaml.Unmarshal(y, &g); err != nil { | ||
fmt.Println(err) | ||
return nil, err | ||
} else { | ||
return g, nil | ||
} | ||
} |
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
@@ -0,0 +1,42 @@ | ||
package yml_test | ||
|
||
import ( | ||
"fmt" | ||
"github.com/apache/apisix-control-plane/pkg/yml" | ||
"github.com/ghodss/yaml" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("Trans", func() { | ||
Describe("trans to model", func() { | ||
var b []byte | ||
BeforeEach(func() { | ||
b = []byte(` | ||
kind: Gateway | ||
name: foo-gw | ||
servers: | ||
- port: | ||
number: 80 | ||
name: http | ||
protocol: HTTP | ||
hosts: | ||
- "a.foo.com" | ||
- "b.foo.com" | ||
`) | ||
fmt.Println("BeforeEach executed") | ||
}) | ||
Context("trans", func() { | ||
It("trans to gateway no error", func() { | ||
y, err := yaml.YAMLToJSON(b) | ||
fmt.Println(string(y)) | ||
ym := yml.Trans(y, b) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(ym.Type()).To(Equal("Gateway")) | ||
g, ok := ym.(*yml.Gateway) | ||
Expect(ok).To(Equal(true)) | ||
Expect(len(g.Servers[0].Hosts)).To(Equal(2)) | ||
}) | ||
}) | ||
}) | ||
}) |
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
@@ -14,7 +14,7 @@ | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package yaml_test | ||
package yml_test | ||
|
||
import ( | ||
"testing" | ||
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