Giter VIP home page Giter VIP logo

yaml's Introduction

yaml beta

yaml is a YAML parser for Go. It is a fork of yaml that adds some features to make it more useful for go-faster purposes, including better error reporting and performance.

Compatibility

The yaml package supports most of YAML 1.2, but preserves some behavior from 1.1 for backwards compatibility.

Specifically, as of v3 of the yaml package:

  • YAML 1.1 bools (yes/no, on/off) are supported as long as they are being decoded into a typed bool value. Otherwise they behave as a string. Booleans in YAML 1.2 are true/false only.
  • Octals encode and decode as 0777 per YAML 1.1, rather than 0o777 as specified in YAML 1.2, because most parsers still use the old format. Octals in the 0o777 format are supported though, so new files work.
  • Does not support base-60 floats. These are gone from YAML 1.2, and were actually never supported by this package as it's clearly a poor choice.

and offers backwards compatibility with YAML 1.1 in some cases. 1.2, including support for anchors, tags, map merging, etc. Multi-document unmarshaling is not yet implemented, and base-60 floats from YAML 1.1 are purposefully not supported since they're a poor design and are gone in YAML 1.2.

Installation and usage

The import path for the package is github.com/go-faster/yaml.

To install it, run:

go get github.com/go-faster/yaml

API stability

Unlike the original library, this module is not guaranteed to be stable.

Example

package main

import (
	"fmt"
	"log"

	"github.com/go-faster/yaml"
)

var data = `
a: Easy!
b:
  c: 2
  d: [3, 4]
`

// Note: struct fields must be public in order for unmarshal to
// correctly populate the data.
type T struct {
	A string
	B struct {
		RenamedC int   `yaml:"c"`
		D        []int `yaml:",flow"`
	}
}

func main() {
	t := T{}

	err := yaml.Unmarshal([]byte(data), &t)
	if err != nil {
		log.Fatalf("error: %v", err)
	}
	fmt.Printf("--- t:\n%v\n\n", t)

	d, err := yaml.Marshal(&t)
	if err != nil {
		log.Fatalf("error: %v", err)
	}
	fmt.Printf("--- t dump:\n%s\n\n", string(d))

	m := make(map[interface{}]interface{})

	err = yaml.Unmarshal([]byte(data), &m)
	if err != nil {
		log.Fatalf("error: %v", err)
	}
	fmt.Printf("--- m:\n%v\n\n", m)

	d, err = yaml.Marshal(&m)
	if err != nil {
		log.Fatalf("error: %v", err)
	}
	fmt.Printf("--- m dump:\n%s\n\n", string(d))
}

This example will generate the following output:

--- t:
{Easy! {2 [3 4]}}

--- t dump:
a: Easy!
b:
  c: 2
  d: [3, 4]


--- m:
map[a:Easy! b:map[c:2 d:[3 4]]]

--- m dump:
a: Easy!
b:
  c: 2
  d:
  - 3
  - 4

yaml's People

Contributors

andrewheberle avatar chenrui333 avatar chillum avatar dependabot[bot] avatar ernado avatar gogolok avatar hiveminded avatar jameinel avatar king-jam avatar ktnyt avatar laverya avatar liggitt avatar mogthesprog avatar mvo5 avatar mwhudson avatar niemeyer avatar pchou avatar prashantv avatar rliebz avatar robx avatar rogpeppe avatar sashamelentyev avatar shawnps avatar songmu avatar sylr avatar tcolgate avatar tdakkota avatar vitreuz avatar wupeka avatar xtreme-sameer-vohra avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

tdakkota

yaml's Issues

FuzzDecodeEncodeDecode: []byte("{0}")

--- FAIL: FuzzDecodeEncodeDecode (1.43s)
    --- FAIL: FuzzDecodeEncodeDecode (0.00s)
        fuzz_test.go:106:
                Error Trace:    /root/yamlx/fuzz_test.go:106
                                                        /root/yamlx/fuzz_test.go:110
                                                        /root/yamlx/fuzz_test.go:110
                                                        /root/yamlx/fuzz_test.go:113
                                                        /root/yamlx/value.go:556
                                                        /root/yamlx/value.go:339
                                                        /root/yamlx/fuzz.go:337
                Error:          Not equal:
                                expected: "!!null"
                                actual  : "!!str"

                                Diff:
                                --- Expected
                                +++ Actual
                                @@ -1 +1 @@
                                -!!null
                                +!!str
                Test:           FuzzDecodeEncodeDecode
        fuzz_test.go:79: Input: "{0}"
        fuzz_test.go:81: Data: "{0: ''}\n"

    Failing input written to testdata/fuzz/FuzzDecodeEncodeDecode/90513016cb18725627689d6b25adb2c440d71f64d72e23ef201ed3ac51443001
    To re-run:
    go test -run=FuzzDecodeEncodeDecode/90513016cb18725627689d6b25adb2c440d71f64d72e23ef201ed3ac51443001
FAIL
exit status 1
FAIL    github.com/go-faster/yamlx      7.794s
root@hosting635102:~/yamlx# cat testdata/fuzz/FuzzDecodeEncodeDecode/90513016cb18725627689d6b25adb2c440d71f64d72e23ef201ed3ac51443001
go test fuzz v1
[]byte("{0}")

FuzzUnmarshal: panic

fuzz: elapsed: 7m40s, execs: 240689066 (0/sec), new interesting: 1496 (total: 2692)
--- FAIL: FuzzUnmarshal (460.10s)
    --- FAIL: FuzzUnmarshal (0.00s)
        testing.go:1349: panic: runtime error: hash of unhashable type map[string]interface {}
            goroutine 259780 [running]:
            runtime/debug.Stack()
                /usr/lib/go-1.18/src/runtime/debug/stack.go:24 +0x90
            testing.tRunner.func1()
                /usr/lib/go-1.18/src/testing/testing.go:1349 +0x1f2
            panic({0x74df80, 0xc0008f65f0})
                /usr/lib/go-1.18/src/runtime/panic.go:838 +0x207
            github.com/go-faster/yamlx.handleErr(0xc0016a56e8)
                /root/yamlx/errors.go:152 +0xc5
            panic({0x74df80, 0xc0008f65f0})
                /usr/lib/go-1.18/src/runtime/panic.go:838 +0x207
            github.com/go-faster/yamlx.(*decoder).mapping(0xc000895180, 0xc0008f9180, {0x74b880?, 0xc0008b1e60?, 0xc0016a50c8?})
                /root/yamlx/decode.go:841 +0xd37
            github.com/go-faster/yamlx.(*decoder).unmarshal(0xc000895180, 0xc0008f9180, {0x74b880?, 0xc0008b1e60?, 0xc0008f6590?})
                /root/yamlx/decode.go:524 +0x876
            github.com/go-faster/yamlx.(*decoder).merge(0xc000895180, 0xc0008f8dc0?, 0xc0008f9180, {0x74b880?, 0xc0008b1e60?, 0x194?})
                /root/yamlx/decode.go:983 +0x2b1
            github.com/go-faster/yamlx.(*decoder).mapping(0xc000895180, 0xc0008f8dc0, {0x746d40?, 0xc0008f6530?, 0x0?})
                /root/yamlx/decode.go:863 +0x11bc
            github.com/go-faster/yamlx.(*decoder).unmarshal(0xc000895180, 0xc0008f8dc0, {0x746d40?, 0xc0008f6530?, 0x0?})
                /root/yamlx/decode.go:524 +0x876
            github.com/go-faster/yamlx.(*decoder).document(0xc000895180, 0xc0008f8d20, {0x746d40?, 0xc0008f6530?, 0x0?})
                /root/yamlx/decode.go:541 +0xca
            github.com/go-faster/yamlx.(*decoder).unmarshal(0xc000895180, 0xc0008f8d20, {0x746d40?, 0xc0008f6530?, 0x2?})
                /root/yamlx/decode.go:512 +0x97e
            github.com/go-faster/yamlx.unmarshal({0xc00aa18400, 0x3b, 0x100}, {0x735ec0?, 0xc0008f6530})
                /root/yamlx/unmarshal.go:136 +0x48e
            github.com/go-faster/yamlx.Unmarshal(...)
                /root/yamlx/unmarshal.go:54
            github.com/go-faster/yamlx_test.FuzzUnmarshal.func1(0x0?, {0xc00aa18400, 0x3b, 0x100})
                /root/yamlx/unmarshal_fuzz_test.go:28 +0x52
            reflect.Value.call({0x7429a0?, 0x836938?, 0x13?}, {0x791197, 0x4}, {0xc0008b1da0, 0x2, 0x2?})
                /usr/lib/go-1.18/src/reflect/value.go:556 +0x845
            reflect.Value.Call({0x7429a0?, 0x836938?, 0x514?}, {0xc0008b1da0, 0x2, 0x2})
                /usr/lib/go-1.18/src/reflect/value.go:339 +0xbf
            testing.(*F).Fuzz.func1.1(0x2c7d617b2c7d617b?)
                /usr/lib/go-1.18/src/testing/fuzz.go:337 +0x231
            testing.tRunner(0xc00aa26680, 0xc0008af7a0)
go test fuzz v1
[]byte("0:\n00:\n 00000000000000000000000000000000000000000\n<<:\n  {}:")

FuzzDecodeEncodeDecode: did not find expected ',' or '}'

--- FAIL: FuzzDecodeEncodeDecode (154.81s)
    --- FAIL: FuzzDecodeEncodeDecode (0.00s)
        fuzz_test.go:115: 
                Error Trace:    /src/faster/yamlx/fuzz_test.go:115
                                                        /usr/local/go/src/reflect/value.go:586
                                                        /usr/local/go/src/reflect/value.go:370
                                                        /usr/local/go/src/testing/fuzz.go:335
                Error:          Received unexpected error:
                                yaml: line 1: did not find expected ',' or '}'
                Test:           FuzzDecodeEncodeDecode
        fuzz_test.go:91: Input: "{{}#\n}0"
        fuzz_test.go:93: Data: "{{} #\n: ''}\n"
    
    Failing input written to testdata/fuzz/FuzzDecodeEncodeDecode/f21f52dada1442fc
    To re-run:
    go test -run=FuzzDecodeEncodeDecode/f21f52dada1442fc
go test fuzz v1
[]byte("{{}#\n}0")

FuzzDecodeEncodeDecode failed

fuzz: elapsed: 5s, execs: 43811 (11005/sec), new interesting: 62 (total: 300)
--- FAIL: FuzzDecodeEncodeDecode (4.66s)
    --- FAIL: FuzzDecodeEncodeDecode (0.00s)
        fuzz_test.go:86:
                Error Trace:    /root/yamlx/fuzz_test.go:86
                                                        /root/yamlx/value.go:556
                                                        /root/yamlx/value.go:339
                                                        /root/yamlx/fuzz.go:337
                Error:          Received unexpected error:
                                yaml: line 2: did not find expected '-' indicator
                Test:           FuzzDecodeEncodeDecode
        fuzz_test.go:89: v.IsZero() != v2.IsZero(), false != true
        fuzz_test.go:90: Zero value, data: "0:\n    #00\n    - |4-\n       00\n"
        fuzz_test.go:71: Input: "0:\n    #00\n    - |1 \n      00"

    Failing input written to testdata/fuzz/FuzzDecodeEncodeDecode/b249c93d07c0f84c4c499fce19374c2c6839014568fbdaa31cd5ef0ae83a897e
    To re-run:
    go test -run=FuzzDecodeEncodeDecode/b249c93d07c0f84c4c499fce19374c2c6839014568fbdaa31cd5ef0ae83a897e

FuzzDecodeEncodeDecode: did not find expected ',' or '}'

fuzz: elapsed: 2m56s, minimizing
--- FAIL: FuzzDecodeEncodeDecode (175.66s)
    --- FAIL: FuzzDecodeEncodeDecode (0.00s)
        fuzz_test.go:115: 
                Error Trace:    /src/faster/yamlx/fuzz_test.go:115
                                                        /usr/local/go/src/reflect/value.go:586
                                                        /usr/local/go/src/reflect/value.go:370
                                                        /usr/local/go/src/testing/fuzz.go:335
                Error:          Received unexpected error:
                                yaml: line 1: did not find expected ',' or '}'
                Test:           FuzzDecodeEncodeDecode
        fuzz_test.go:91: Input: "{{} #\n}"
        fuzz_test.go:93: Data: "{{} #\n: ''}\n"
    
    Failing input written to testdata/fuzz/FuzzDecodeEncodeDecode/f91ab6905aa88579
    To re-run:
    go test -run=FuzzDecodeEncodeDecode/f91ab6905aa88579
FAIL
go test fuzz v1
[]byte("{{} #\n}")

FuzzDecodeEncodeDecode: yaml: line 3: could not find expected ':'

--- FAIL: FuzzDecodeEncodeDecode (136.23s)
    --- FAIL: FuzzDecodeEncodeDecode (0.00s)
        fuzz_test.go:115:
                Error Trace:    /root/yaml/fuzz_test.go:115
                                                        /usr/local/go/src/reflect/value.go:586
                                                        /usr/local/go/src/reflect/value.go:370
                                                        /usr/local/go/src/testing/fuzz.go:335
                Error:          Received unexpected error:
                                yaml: line 3: could not find expected ':'
                Test:           FuzzDecodeEncodeDecode
        fuzz_test.go:91: Input: "- ? #000000\n- [0]\n#000000000000000"
        fuzz_test.go:93: Data: "-   '':\n- #000000\n[0]\n#000000000000000\n"

    Failing input written to testdata/fuzz/FuzzDecodeEncodeDecode/924dba5dd5f9c2c1
    To re-run:
    go test -run=FuzzDecodeEncodeDecode/924dba5dd5f9c2c1
FAIL
go test fuzz v1
[]byte("- ? #000000\n- [0]\n#000000000000000")

FuzzDecodeEncodeDecode: yaml: invalid UTF-8 in tag "\xc0\x80"

--- FAIL: FuzzDecodeEncodeDecode (17.48s)
    --- FAIL: FuzzDecodeEncodeDecode (0.00s)
        fuzz_test.go:112: 
                Error Trace:    /src/faster/yamlx/fuzz_test.go:112
                                                        /usr/local/go/src/reflect/value.go:586
                                                        /usr/local/go/src/reflect/value.go:370
                                                        /usr/local/go/src/testing/fuzz.go:335
                Error:          Received unexpected error:
                                yaml: invalid UTF-8 in tag "\xc0\x80"
                Test:           FuzzDecodeEncodeDecode
        fuzz_test.go:91: Input: "!%C0%80"
    
    Failing input written to testdata/fuzz/FuzzDecodeEncodeDecode/f62dc59e4c32e1d6
    To re-run:
    go test -run=FuzzDecodeEncodeDecode/f62dc59e4c32e1d6
go test fuzz v1
[]byte("!%C0%80")

FuzzDecodeEncodeDecode: could not find expected ':'

fuzz_test.go:115: 
    Error Trace:    /src/faster/yamlx/fuzz_test.go:115
                                            /usr/local/go/src/reflect/value.go:586
                                            /usr/local/go/src/reflect/value.go:370
                                            /usr/local/go/src/testing/fuzz.go:335
    Error:          Received unexpected error:
                    yaml: line 4: could not find expected ':'
    Test:           FuzzDecodeEncodeDecode
fuzz_test.go:91: Input: "0:\n  0000: 00000\n000: #0000\n  !0\n 00:"
fuzz_test.go:93: Data: "0:\n    0000: 00000\n000: #0000\n!0\n    00:\n"
go test fuzz v1
[]byte("0:\n  0000: 00000\n000: #0000\n  !0\n 00:")

FuzzDecodeEncodeDecode: did not find expected whitespace or line break

go test -fuzz FuzzDecodeEncodeDecode
--- FAIL: FuzzDecodeEncodeDecode (137.27s)
    --- FAIL: FuzzDecodeEncodeDecode (0.00s)
        fuzz_test.go:115: 
                Error Trace:    /src/faster/yamlx/fuzz_test.go:115
                                                        /usr/local/go/src/reflect/value.go:586
                                                        /usr/local/go/src/reflect/value.go:370
                                                        /usr/local/go/src/testing/fuzz.go:335
                Error:          Received unexpected error:
                                yaml: offset 1: did not find expected whitespace or line break
                Test:           FuzzDecodeEncodeDecode
        fuzz_test.go:91: Input: "!%5B"
        fuzz_test.go:93: Data: "![\n"
    
    Failing input written to testdata/fuzz/FuzzDecodeEncodeDecode/f79f09ab701cbe11
    To re-run:
    go test -run=FuzzDecodeEncodeDecode/f79f09ab701cbe11

f79f09ab701cbe11:

go test fuzz v1
[]byte("!%5B")

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.