Skip to content

Commit

Permalink
Moved the init of scheme to main,
Browse files Browse the repository at this point in the history
Signed-off-by: Shiming Zhang <[email protected]>
  • Loading branch information
wzshiming committed Feb 29, 2024
1 parent 66917f2 commit 87af109
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 21 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ clean:

generate:
go mod vendor
rm ./pkg/encoding/scheme.go
./hack/gen_scheme.sh > pkg/encoding/scheme.go
rm ./pkg/scheme/scheme.go
./hack/gen_scheme.sh > pkg/scheme/scheme.go

.PHONY: build test release release-docker-build clean generate
21 changes: 21 additions & 0 deletions cmd/init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed 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 cmd

import (
_ "github.com/etcd-io/auger/pkg/scheme"
)
2 changes: 1 addition & 1 deletion hack/gen_scheme.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package encoding
package scheme
// Don't edit this file directly. It is generated by scheme.sh.
import (
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"fmt"
"os"

_ "github.com/etcd-io/auger/pkg/scheme"

"github.com/etcd-io/auger/cmd"
)

Expand Down
21 changes: 21 additions & 0 deletions pkg/data/init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed 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 data

import (
_ "github.com/etcd-io/auger/pkg/scheme"
)
8 changes: 4 additions & 4 deletions pkg/encoding/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ func Convert(inMediaType, outMediaType string, in []byte, out io.Writer) (*runti

// DetectAndExtract searches the the start of either json of protobuf data, and, if found, returns the mime type and data.
func DetectAndExtract(in []byte) (string, []byte, error) {
if pb, ok := tryFindProto(in); ok {
if pb, ok := TryFindProto(in); ok {
return StorageBinaryMediaType, pb, nil
}
if rawJs, ok := tryFindJson(in); ok {
if rawJs, ok := TryFindJson(in); ok {
js, err := rawJs.MarshalJSON()
if err != nil {
return "", nil, err
Expand All @@ -134,7 +134,7 @@ func DetectAndExtract(in []byte) (string, []byte, error) {
}

// TryFindProto searches for the 'k8s\0' prefix, and, if found, returns the data starting with the prefix.
func tryFindProto(in []byte) ([]byte, bool) {
func TryFindProto(in []byte) ([]byte, bool) {
i := bytes.Index(in, ProtoEncodingPrefix)
if i >= 0 && i < len(in) {
return in[i:], true
Expand All @@ -145,7 +145,7 @@ func tryFindProto(in []byte) ([]byte, bool) {
const jsonStartChars = "{["

// TryFindJson searches for the start of a valid json substring, and, if found, returns the json.
func tryFindJson(in []byte) (*json.RawMessage, bool) {
func TryFindJson(in []byte) (*json.RawMessage, bool) {
var js json.RawMessage

i := bytes.IndexAny(in, jsonStartChars)
Expand Down
14 changes: 8 additions & 6 deletions pkg/encoding/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,28 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package encoding
package encoding_test

import (
"fmt"
"testing"

"github.com/etcd-io/auger/pkg/encoding"
)

var findProtoTests = []struct {
in string
ok bool
expected string
}{
{string(ProtoEncodingPrefix), true, string(ProtoEncodingPrefix)},
{fmt.Sprintf("xxxxxx%s...end", ProtoEncodingPrefix), true, fmt.Sprintf("%s...end", ProtoEncodingPrefix)},
{string(encoding.ProtoEncodingPrefix), true, string(encoding.ProtoEncodingPrefix)},
{fmt.Sprintf("xxxxxx%s...end", encoding.ProtoEncodingPrefix), true, fmt.Sprintf("%s...end", encoding.ProtoEncodingPrefix)},
{fmt.Sprintf("xxxxxx{}"), false, ""},
}

func TestTryFindProto(t *testing.T) {
for _, test := range findProtoTests {
out, ok := tryFindProto([]byte(test.in))
out, ok := encoding.TryFindProto([]byte(test.in))
if test.ok != ok {
t.Errorf("got ok=%t, want ok=%t for %+v", ok, test.ok, test)
}
Expand All @@ -59,14 +61,14 @@ var findJsonTests = []struct {
{"xxxxxx{}", true, "{}"},
{"xx{x[x{}", true, "{}"},
{"xxxxxx[]", true, "[]"},
{fmt.Sprintf("xxxxxx%s...end", ProtoEncodingPrefix), false, ""},
{fmt.Sprintf("xxxxxx%s...end", encoding.ProtoEncodingPrefix), false, ""},
{"{}xxxxxx", false, ""},
{"{}[", false, ""},
}

func TestTryFindJson(t *testing.T) {
for _, test := range findJsonTests {
out, ok := tryFindJson([]byte(test.in))
out, ok := encoding.TryFindJson([]byte(test.in))
if test.ok != ok {
t.Errorf("got ok=%t, want ok=%t for %+v", ok, test.ok, test)
}
Expand Down
7 changes: 0 additions & 7 deletions pkg/encoding/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,9 @@ limitations under the License.
package encoding

import (
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
runtime "k8s.io/apimachinery/pkg/runtime"
schema "k8s.io/apimachinery/pkg/runtime/schema"
serializer "k8s.io/apimachinery/pkg/runtime/serializer"
)

var Scheme = runtime.NewScheme()
var Codecs = serializer.NewCodecFactory(Scheme)

func init() {
v1.AddToGroupVersion(Scheme, schema.GroupVersion{Version: "v1"})
AddToScheme(Scheme)
}
21 changes: 21 additions & 0 deletions pkg/encoding/init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed 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 encoding_test

import (
_ "github.com/etcd-io/auger/pkg/scheme"
)
28 changes: 28 additions & 0 deletions pkg/scheme/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Copyright The Kubernetes Authors.
Licensed 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 scheme

import (
"github.com/etcd-io/auger/pkg/encoding"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
)

func init() {
v1.AddToGroupVersion(encoding.Scheme, schema.GroupVersion{Version: "v1"})
AddToScheme(encoding.Scheme)
}
2 changes: 1 addition & 1 deletion pkg/encoding/scheme.go → pkg/scheme/scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package encoding
package scheme

// Don't edit this file directly. It is generated by scheme.sh.
import (
Expand Down

0 comments on commit 87af109

Please sign in to comment.