Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加字段默认值支持 #971

Open
wants to merge 2 commits into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- name: Check out code into the Go module directory
uses: actions/checkout@v2
with:
path: ${{ github.workspace }}/go/src/gopkg.in/yaml.v3
path: ${{ github.workspace }}/go/src/github.com/github.com/putao520/yaml
- name: Set up Go ${{ matrix.go }}
if: matrix.go != 'tip'
uses: actions/setup-go@v2
Expand Down Expand Up @@ -56,6 +56,6 @@ jobs:
echo "$GOROOT/bin" >> $GITHUB_PATH
- run: go version
- run: go get -t ./...
working-directory: ${{ github.workspace }}/go/src/gopkg.in/yaml.v3
working-directory: ${{ github.workspace }}/go/src/github.com/putao520/yaml
- run: go test .
working-directory: ${{ github.workspace }}/go/src/gopkg.in/yaml.v3
working-directory: ${{ github.workspace }}/go/src/github.com/putao520/yaml
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ supported since they're a poor design and are gone in YAML 1.2.
Installation and usage
----------------------

The import path for the package is *gopkg.in/yaml.v3*.
The import path for the package is *github.com/putao520/yaml*.

To install it, run:

go get gopkg.in/yaml.v3
go get github.com/putao520/yaml

API documentation
-----------------

If opened in a browser, the import path itself leads to the API documentation:

- [https://gopkg.in/yaml.v3](https://gopkg.in/yaml.v3)
- [https://github.com/putao520/yaml](https://github.com/putao520/yaml)

API stability
-------------
Expand All @@ -72,7 +72,7 @@ import (
"fmt"
"log"

"gopkg.in/yaml.v3"
"github.com/putao520/yaml"
)

var data = `
Expand Down
54 changes: 54 additions & 0 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -875,12 +875,65 @@ func isStringMap(n *Node) bool {
return true
}

func safeDefaultType(t reflect.Type) bool {
switch t.Kind() {
case reflect.Int:
return true
case reflect.Bool:
return true
case reflect.Float32:
return true
case reflect.Float64:
return true
case reflect.Int8:
return true
case reflect.Int16:
return true
case reflect.Int32:
return true
case reflect.Int64:
return true
case reflect.Uint:
return true
case reflect.Uint8:
return true
case reflect.Uint16:
return true
case reflect.Uint32:
return true
case reflect.Uint64:
return true
case reflect.String:
return true
default:
return false
}
}

func (d *decoder) mappingStruct(n *Node, out reflect.Value) (good bool) {
sinfo, err := getStructInfo(out.Type())
if err != nil {
panic(err)
}

// 构造 n 的key映射
nodeMap := make(map[string]*Node)
l_ := len(n.Content)
for i := 0; i < l_; i += 2 {
nodeMap[n.Content[i].Value] = n.Content[i+1]
}
// 遍历 sinfo.FieldsList,将所有的字段都设置为未处理
for _, info := range sinfo.FieldsList {
if nodeMap[info.Key] == nil {
if safeDefaultType(info.Type) {
// 新增 key
n.Content = append(n.Content, &Node{Kind: ScalarNode, Value: info.Key})
// 新增 value
n.Content = append(n.Content, &Node{Kind: ScalarNode, Value: info.Default, Type: info.Type})
}
}
}

var inlineMap reflect.Value
var elemType reflect.Type
if sinfo.InlineMap != -1 {
Expand Down Expand Up @@ -949,6 +1002,7 @@ func (d *decoder) mappingStruct(n *Node, out reflect.Value) (good bool) {
if mergeNode != nil {
d.merge(n, mergeNode, out)
}

return true
}

Expand Down
14 changes: 7 additions & 7 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
"strings"
"time"

"github.com/putao520/yaml"
. "gopkg.in/check.v1"
"gopkg.in/yaml.v3"
)

var unmarshalIntTest = 123
Expand Down Expand Up @@ -947,7 +947,7 @@ var unmarshalErrorTests = []struct {
{"%TAG !%79! tag:yaml.org,2002:\n---\nv: !%79!int '1'", "yaml: did not find expected whitespace"},
{"a:\n 1:\nb\n 2:", ".*could not find expected ':'"},
{"a: 1\nb: 2\nc 2\nd: 3\n", "^yaml: line 3: could not find expected ':'$"},
{"#\n-\n{", "yaml: line 3: could not find expected ':'"}, // Issue #665
{"#\n-\n{", "yaml: line 3: could not find expected ':'"}, // Issue #665
{"0: [:!00 \xef", "yaml: incomplete UTF-8 octet sequence"}, // Issue #666
{
"a: &a [00,00,00,00,00,00,00,00,00]\n" +
Expand Down Expand Up @@ -1482,7 +1482,7 @@ func (s *S) TestMergeNestedStruct(c *C) {
// 2) A simple implementation might attempt to handle the key skipping
// directly by iterating over the merging map without recursion, but
// there are more complex cases that require recursion.
//
//
// Quick summary of the fields:
//
// - A must come from outer and not overriden
Expand All @@ -1498,7 +1498,7 @@ func (s *S) TestMergeNestedStruct(c *C) {
A, B, C int
}
type Outer struct {
D, E int
D, E int
Inner Inner
Inline map[string]int `yaml:",inline"`
}
Expand All @@ -1516,10 +1516,10 @@ func (s *S) TestMergeNestedStruct(c *C) {
// Repeat test with a map.

var testm map[string]interface{}
var wantm = map[string]interface {} {
"f": 60,
var wantm = map[string]interface{}{
"f": 60,
"inner": map[string]interface{}{
"a": 10,
"a": 10,
},
"d": 40,
"e": 50,
Expand Down
2 changes: 1 addition & 1 deletion encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (
"net"
"os"

"github.com/putao520/yaml"
. "gopkg.in/check.v1"
"gopkg.in/yaml.v3"
)

var marshalIntTest = 123
Expand Down
8 changes: 6 additions & 2 deletions example_embedded_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ import (
"fmt"
"log"

"gopkg.in/yaml.v3"
"github.com/putao520/yaml"
)

// An example showing how to unmarshal embedded
// structs from YAML.

type StructA struct {
A string `yaml:"a"`
A string `yaml:"a"`
C string `yaml:"c" default:"true"`
D bool `yaml:"d" default:"true"`
E int64 `yaml:"e" default:"123"`
F float64 `yaml:"f" default:"123.123"`
}

type StructB struct {
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module "gopkg.in/yaml.v3"
module github.com/putao520/yaml

require (
"gopkg.in/check.v1" v0.0.0-20161208181325-20d25e280405
)
go 1.20

require gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405
2 changes: 1 addition & 1 deletion limit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"strings"
"testing"

"github.com/putao520/yaml"
. "gopkg.in/check.v1"
"gopkg.in/yaml.v3"
)

var limitTests = []struct {
Expand Down
38 changes: 19 additions & 19 deletions node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"fmt"
"os"

"github.com/putao520/yaml"
. "gopkg.in/check.v1"
"gopkg.in/yaml.v3"
"io"
"strings"
)
Expand Down Expand Up @@ -688,17 +688,17 @@ var nodeTests = []struct {
Line: 3,
Column: 4,
}, {
Kind: yaml.ScalarNode,
Tag: "!!str",
Value: "c",
Kind: yaml.ScalarNode,
Tag: "!!str",
Value: "c",
LineComment: "# IC",
Line: 5,
Column: 1,
Line: 5,
Column: 1,
}, {
Kind: yaml.SequenceNode,
Tag: "!!seq",
Line: 6,
Column: 3,
Kind: yaml.SequenceNode,
Tag: "!!seq",
Line: 6,
Column: 3,
Content: []*yaml.Node{{
Kind: yaml.ScalarNode,
Tag: "!!str",
Expand All @@ -707,17 +707,17 @@ var nodeTests = []struct {
Column: 5,
}},
}, {
Kind: yaml.ScalarNode,
Tag: "!!str",
Value: "d",
Kind: yaml.ScalarNode,
Tag: "!!str",
Value: "d",
LineComment: "# ID",
Line: 7,
Column: 1,
Line: 7,
Column: 1,
}, {
Kind: yaml.MappingNode,
Tag: "!!map",
Line: 8,
Column: 3,
Kind: yaml.MappingNode,
Tag: "!!map",
Line: 8,
Column: 3,
Content: []*yaml.Node{{
Kind: yaml.ScalarNode,
Tag: "!!str",
Expand Down
1 change: 0 additions & 1 deletion resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ func resolve(tag string, in string) (rtag string, out interface{}) {
if !resolvableTag(tag) {
return tag, in
}

defer func() {
switch tag {
case "", rtag, strTag, binaryTag:
Expand Down