Skip to content

Commit

Permalink
Merge pull request #235 from goccy/feature/rename-context-api
Browse files Browse the repository at this point in the history
Rename MarshalWithContext and UnmarshalWithContext
  • Loading branch information
goccy authored Jul 19, 2021
2 parents 2c8e5ef + c14dac7 commit 894a764
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
10 changes: 5 additions & 5 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1687,11 +1687,11 @@ func TestDecoder_UseJSONUnmarshaler(t *testing.T) {
}
}

type unmarshalWithContext struct {
type unmarshalContext struct {
v int
}

func (c *unmarshalWithContext) UnmarshalYAML(ctx context.Context, b []byte) error {
func (c *unmarshalContext) UnmarshalYAML(ctx context.Context, b []byte) error {
v, ok := ctx.Value("k").(int)
if !ok {
return fmt.Errorf("cannot get valid context")
Expand All @@ -1706,10 +1706,10 @@ func (c *unmarshalWithContext) UnmarshalYAML(ctx context.Context, b []byte) erro
return nil
}

func Test_UnmarshalerWithContext(t *testing.T) {
func Test_UnmarshalerContext(t *testing.T) {
ctx := context.WithValue(context.Background(), "k", 1)
var v unmarshalWithContext
if err := yaml.UnmarshalWithContext(ctx, []byte(`1`), &v); err != nil {
var v unmarshalContext
if err := yaml.UnmarshalContext(ctx, []byte(`1`), &v); err != nil {
t.Fatalf("%+v", err)
}
if v.v != 1 {
Expand Down
8 changes: 4 additions & 4 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1173,9 +1173,9 @@ func Test_Marshaler(t *testing.T) {
t.Logf("%s", buf)
}

type marshalWithContext struct{}
type marshalContext struct{}

func (c *marshalWithContext) MarshalYAML(ctx context.Context) ([]byte, error) {
func (c *marshalContext) MarshalYAML(ctx context.Context) ([]byte, error) {
v, ok := ctx.Value("k").(int)
if !ok {
return nil, fmt.Errorf("cannot get valid context")
Expand All @@ -1186,9 +1186,9 @@ func (c *marshalWithContext) MarshalYAML(ctx context.Context) ([]byte, error) {
return []byte("1"), nil
}

func Test_MarshalerWithContext(t *testing.T) {
func Test_MarshalerContext(t *testing.T) {
ctx := context.WithValue(context.Background(), "k", 1)
bytes, err := yaml.MarshalWithContext(ctx, &marshalWithContext{})
bytes, err := yaml.MarshalContext(ctx, &marshalContext{})
if err != nil {
t.Fatalf("%+v", err)
}
Expand Down
12 changes: 6 additions & 6 deletions yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ func Marshal(v interface{}) ([]byte, error) {

// MarshalWithOptions serializes the value provided into a YAML document with EncodeOptions.
func MarshalWithOptions(v interface{}, opts ...EncodeOption) ([]byte, error) {
return MarshalWithContext(context.Background(), v, opts...)
return MarshalContext(context.Background(), v, opts...)
}

// MarshalWithContext serializes the value provided into a YAML document with context.Context and EncodeOptions.
func MarshalWithContext(ctx context.Context, v interface{}, opts ...EncodeOption) ([]byte, error) {
// MarshalContext serializes the value provided into a YAML document with context.Context and EncodeOptions.
func MarshalContext(ctx context.Context, v interface{}, opts ...EncodeOption) ([]byte, error) {
var buf bytes.Buffer
if err := NewEncoder(&buf, opts...).EncodeContext(ctx, v); err != nil {
return nil, errors.Wrapf(err, "failed to marshal")
Expand Down Expand Up @@ -182,11 +182,11 @@ func Unmarshal(data []byte, v interface{}) error {
// UnmarshalWithOptions decodes with DecodeOptions the first document found within the in byte slice
// and assigns decoded values into the out value.
func UnmarshalWithOptions(data []byte, v interface{}, opts ...DecodeOption) error {
return UnmarshalWithContext(context.Background(), data, v, opts...)
return UnmarshalContext(context.Background(), data, v, opts...)
}

// UnmarshalWithContext decodes with context.Context and DecodeOptions.
func UnmarshalWithContext(ctx context.Context, data []byte, v interface{}, opts ...DecodeOption) error {
// UnmarshalContext decodes with context.Context and DecodeOptions.
func UnmarshalContext(ctx context.Context, data []byte, v interface{}, opts ...DecodeOption) error {
dec := NewDecoder(bytes.NewBuffer(data), opts...)
if err := dec.DecodeContext(ctx, v); err != nil {
if err == io.EOF {
Expand Down

0 comments on commit 894a764

Please sign in to comment.