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

Task/ut 4516 invalid param #1060

Open
wants to merge 3 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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/iamshivamnanda/yaml/v3
- 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/iamshivamnanda/yaml/v3
- run: go test .
working-directory: ${{ github.workspace }}/go/src/gopkg.in/yaml.v3
working-directory: ${{ github.workspace }}/go/src/github.com/iamshivamnanda/yaml/v3
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/iamshivamnanda/yaml/v3*.

To install it, run:

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

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/iamshivamnanda/yaml/v3](https://github.com/iamshivamnanda/yaml/v3)

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

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

var data = `
Expand Down
87 changes: 74 additions & 13 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"math"
"reflect"
"strconv"
"strings"
"time"
)

Expand All @@ -38,6 +39,21 @@ type parser struct {
textless bool
}

// YAMLValue is a generic struct that holds both the typed value and the YAML Node
type YAMLValue[T any] struct {
Value T
Node *Node
}

// Helper function to check if a type is YAMLValue or YAMLValue[T]
func isYAMLValueType(t reflect.Type) bool {
if t.Kind() != reflect.Struct {
return false
}

return strings.Contains(t.String(), "YAMLValue")
}

func newParser(b []byte) *parser {
p := parser{}
if !yaml_parser_initialize(&p.parser) {
Expand Down Expand Up @@ -481,43 +497,88 @@ func allowedAliasRatio(decodeCount int) float64 {
}
}

func (d *decoder) unmarshal(n *Node, out reflect.Value) (good bool) {
func (d *decoder) unmarshal(node *Node, out reflect.Value) (good bool) {
d.decodeCount++
if d.aliasDepth > 0 {
d.aliasCount++
}
if d.aliasCount > 100 && d.decodeCount > 1000 && float64(d.aliasCount)/float64(d.decodeCount) > allowedAliasRatio(d.decodeCount) {
failf("document contains excessive aliasing")
}
if out.Type() == nodeType {
out.Set(reflect.ValueOf(n).Elem())

// Check if the out value is of YAMLValue type
if isYAMLValueType(out.Type()) {
valueField := out.FieldByName("Value")
nodeField := out.FieldByName("Node")

if !valueField.IsValid() || !nodeField.IsValid() {
failf("YAMLValue struct is missing Value or Node field")
}

// Create a new value to unmarshal into
innerValue := reflect.New(valueField.Type()).Elem()
good = d.unmarshal(node, innerValue)
if good {
// Set the Value and Node fields of YAMLValue
valueField.Set(innerValue)
nodeField.Set(reflect.ValueOf(node))
// Validate the inner value
errors := ValidateStruct(innerValue, node)
if len(errors) > 0 {
for _, e := range errors {
d.terrors = append(d.terrors, e.Error())
}
return false
}
}
return good
}

// Check if out type is *yaml.Node
if out.Type() == reflect.TypeOf(&Node{}) {
out.Set(reflect.ValueOf(node))
return true
}
switch n.Kind {

switch node.Kind {
case DocumentNode:
return d.document(n, out)
return d.document(node, out)
case AliasNode:
return d.alias(n, out)
return d.alias(node, out)
}
out, unmarshaled, good := d.prepare(n, out)

out, unmarshaled, good := d.prepare(node, out)
if unmarshaled {
return good
}
switch n.Kind {

switch node.Kind {
case ScalarNode:
good = d.scalar(n, out)
good = d.scalar(node, out)
case MappingNode:
good = d.mapping(n, out)
good = d.mapping(node, out)
case SequenceNode:
good = d.sequence(n, out)
good = d.sequence(node, out)
case 0:
if n.IsZero() {
if node.IsZero() {
return d.null(out)
}
fallthrough
default:
failf("cannot decode node with unknown kind %d", n.Kind)
failf("cannot decode node with unknown kind %d", node.Kind)
}

// Perform custom validation based on struct tags
if good {
errors := ValidateStruct(out, node)
if len(errors) > 0 {
for _, e := range errors {
d.terrors = append(d.terrors, e.Error())
}
return false
}
}

return good
}

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

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

var unmarshalIntTest = 123
Expand Down Expand Up @@ -602,14 +602,14 @@ var unmarshalTests = []struct {

// issue #295 (allow scalars with colons in flow mappings and sequences)
{
"a: {b: https://github.com/go-yaml/yaml}",
"a: {b: https://github.com/iamshivamnanda/yaml}",
map[string]interface{}{"a": map[string]interface{}{
"b": "https://github.com/go-yaml/yaml",
"b": "https://github.com/iamshivamnanda/yaml",
}},
},
{
"a: [https://github.com/go-yaml/yaml]",
map[string]interface{}{"a": []interface{}{"https://github.com/go-yaml/yaml"}},
"a: [https://github.com/iamshivamnanda/yaml]",
map[string]interface{}{"a": []interface{}{"https://github.com/iamshivamnanda/yaml"}},
},

// Duration
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"

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

var marshalIntTest = 123
Expand Down
2 changes: 1 addition & 1 deletion example_embedded_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"fmt"
"log"

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

// An example showing how to unmarshal embedded
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/iamshivamnanda/yaml/v3

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: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
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"

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

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

. "gopkg.in/check.v1"
"gopkg.in/yaml.v3"
"io"
"strings"

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

var nodeTests = []struct {
Expand Down Expand Up @@ -688,17 +689,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 +708,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
Loading