-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move part of dubbogo common module to gost (#100)
- Loading branch information
Showing
11 changed files
with
563 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
intTest: 11 | ||
booleanTest: false | ||
strTest: "strTest" | ||
|
||
child: | ||
strTest: "childStrTest" |
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,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 yaml | ||
|
||
import ( | ||
"io/ioutil" | ||
"path" | ||
) | ||
|
||
import ( | ||
perrors "github.com/pkg/errors" | ||
|
||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
// LoadYMLConfig Load yml config byte from file | ||
func LoadYMLConfig(confProFile string) ([]byte, error) { | ||
if len(confProFile) == 0 { | ||
return nil, perrors.Errorf("application configure(provider) file name is nil") | ||
} | ||
|
||
if path.Ext(confProFile) != ".yml" { | ||
return nil, perrors.Errorf("application configure file name{%v} suffix must be .yml", confProFile) | ||
} | ||
|
||
return ioutil.ReadFile(confProFile) | ||
} | ||
|
||
// UnmarshalYMLConfig Load yml config byte from file, then unmarshal to object | ||
func UnmarshalYMLConfig(confProFile string, out interface{}) ([]byte, error) { | ||
confFileStream, err := LoadYMLConfig(confProFile) | ||
if err != nil { | ||
return confFileStream, perrors.Errorf("ioutil.ReadFile(file:%s) = error:%v", confProFile, perrors.WithStack(err)) | ||
} | ||
return confFileStream, yaml.Unmarshal(confFileStream, out) | ||
} | ||
|
||
// UnmarshalYML unmarshals decodes the first document found within the in byte slice and assigns decoded values into the out value. | ||
func UnmarshalYML(data []byte, out interface{}) error { | ||
return yaml.Unmarshal(data, out) | ||
} | ||
|
||
// MarshalYML serializes the value provided into a YAML document. | ||
func MarshalYML(in interface{}) ([]byte, error) { | ||
return yaml.Marshal(in) | ||
} |
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,70 @@ | ||
/* | ||
* 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 yaml | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestUnmarshalYMLConfig(t *testing.T) { | ||
conPath, err := filepath.Abs("./testdata/config.yml") | ||
assert.NoError(t, err) | ||
c := &Config{} | ||
_, err = UnmarshalYMLConfig(conPath, c) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "strTest", c.StrTest) | ||
assert.Equal(t, 11, c.IntTest) | ||
assert.Equal(t, false, c.BooleanTest) | ||
assert.Equal(t, "childStrTest", c.ChildConfig.StrTest) | ||
} | ||
|
||
func TestUnmarshalYMLConfigError(t *testing.T) { | ||
c := &Config{} | ||
_, err := UnmarshalYMLConfig("./testdata/config", c) | ||
assert.Error(t, err) | ||
_, err = UnmarshalYMLConfig("", c) | ||
assert.Error(t, err) | ||
} | ||
|
||
func TestUnmarshalYML(t *testing.T) { | ||
c := &Config{} | ||
b, err := LoadYMLConfig("./testdata/config.yml") | ||
assert.NoError(t, err) | ||
err = UnmarshalYML(b, c) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "strTest", c.StrTest) | ||
assert.Equal(t, 11, c.IntTest) | ||
assert.Equal(t, false, c.BooleanTest) | ||
assert.Equal(t, "childStrTest", c.ChildConfig.StrTest) | ||
} | ||
|
||
type Config struct { | ||
StrTest string `yaml:"strTest" default:"default" json:"strTest,omitempty" property:"strTest"` | ||
IntTest int `default:"109" yaml:"intTest" json:"intTest,omitempty" property:"intTest"` | ||
BooleanTest bool `yaml:"booleanTest" default:"true" json:"booleanTest,omitempty"` | ||
ChildConfig ChildConfig `yaml:"child" json:"child,omitempty"` | ||
} | ||
|
||
type ChildConfig struct { | ||
StrTest string `default:"default" yaml:"strTest" json:"strTest,omitempty"` | ||
} |
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
Oops, something went wrong.