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

Copyedit documentation #9

Merged
merged 1 commit into from
Sep 29, 2023
Merged
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
22 changes: 9 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,17 @@
[![Report Card](https://goreportcard.com/badge/github.com/bufbuild/protoyaml-go)](https://goreportcard.com/report/github.com/bufbuild/protoyaml-go)
[![GoDoc](https://pkg.go.dev/badge/github.com/bufbuild/protoyaml-go.svg)](https://pkg.go.dev/github.com/bufbuild/protoyaml-go)

Marshal and unmarshal Protocol Buffers as YAML.
Marshal and unmarshal Protocol Buffers as YAML. Provides fine-grained error details with file, line, column and snippet information.

Fully compatible with [protojson](https://github.com/protocolbuffers/protobuf-go/tree/master/encoding/protojson).

Provides fine-grained error details, with file, line, column and snippet information.

## Usage

```go
package main

import (
"log",

"github.com/bufbuild/protoyaml-go",
)

Expand All @@ -43,10 +40,9 @@ func main() {
}
```

Either `nil`, or an error with a detailed message will be returned. The error message includes the
file name (if `Path` is set on `UnmarshalOptions`), line number, column number and snippet of the
YAML that caused the error, for every error found in the file. For example, when unmarshalling the
following YAML file:
ProtoYAML returns either `nil` or an error with a detailed message. For every error found in the file, the error
message includes the file name (if `Path` is set on `UnmarshalOptions`), line number, column number, and snippet
of the YAML that caused the error. For example, when unmarshalling the following YAML file:

```yaml
values:
Expand Down Expand Up @@ -99,14 +95,14 @@ testdata/basic.proto3test.yaml:14:18: expected bool, got "no"
| ^...................... expected bool, got "no"
```

In this case, we can see that, only `true` and `false` are valid values for the `single_bool` field.
Only `true` and `false` are valid values for the `single_bool` field.

For more examples, see the [internal/testdata](internal/testdata) directory.

## Validation

ProtoYAML can integrate with external validation libraries, such as
[Protovalidate](https://github.com/bufbuild/protovalidate-go), to provide additional rich error
ProtoYAML can integrate with external validation libraries such as
[Protovalidate](https://github.com/bufbuild/protovalidate-go) to provide additional rich error
information. Simply provide a `Validator` to the `UnmarshalOptions`:

```go
Expand Down Expand Up @@ -136,7 +132,7 @@ func main() {
}
```

The errors produced by the `Validator` will show up along side the ProtoYAML errors, for example:
The errors produced by the `Validator` will show up along side the ProtoYAML errors. For example:

```
testdata/validate.validate.yaml:4:18 cases[2].float_gt_lt: value must be greater than 0 and less than 10 (float.gt_lt)
Expand All @@ -146,7 +142,7 @@ testdata/validate.validate.yaml:4:18 cases[2].float_gt_lt: value must be greater

## Status: Beta

ProtoYAML is not yet stable, however the final shape is unlikely to change drastically - future edits will be somewhat minor.
ProtoYAML is not yet stable. However, the final shape is unlikely to change drasticallyfuture edits will be somewhat minor.

## Legal

Expand Down
14 changes: 7 additions & 7 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,12 @@ func (u *unmarshaler) findAnyType(node *yaml.Node) protoreflect.MessageType {
return nil
}

// Get the message type
// Get the message type.
var msgType protoreflect.MessageType
var err error
if u.options.Resolver != nil {
msgType, err = u.options.Resolver.FindMessageByURL(typeURL)
} else { // Use the global registry
} else { // Use the global registry.
msgType, err = protoregistry.GlobalTypes.FindMessageByURL(typeURL)
}
if err != nil {
Expand Down Expand Up @@ -236,7 +236,7 @@ func (u *unmarshaler) unmarshalBytes(node *yaml.Node) []byte {
return nil
}

// base64 decode the value
// base64 decode the value.
data, err := base64.StdEncoding.DecodeString(node.Value)
if err != nil {
u.addErrorf(node, "invalid base64: %v", err)
Expand Down Expand Up @@ -270,12 +270,12 @@ func (u *unmarshaler) unmarshalBool(node *yaml.Node, forKey bool) bool {
// Accepts either the enum name or number.
func (u *unmarshaler) unmarshalEnum(node *yaml.Node, field protoreflect.FieldDescriptor) protoreflect.EnumNumber {
u.checkKind(node, yaml.ScalarNode)
// Get the enum descriptor
// Get the enum descriptor.
enumDesc := field.Enum()
if enumDesc.FullName() == "google.protobuf.NullValue" {
return 0
}
// Get the enum value
// Get the enum value.
enumVal := enumDesc.Values().ByName(protoreflect.Name(node.Value))
if enumVal == nil {
neg, parsed, err := parseSignedLit(node.Value)
Expand Down Expand Up @@ -410,7 +410,7 @@ func getExpectedNodeKind(field protoreflect.FieldDescriptor, forList bool) strin
}
}

// Parses Octal, Hex, Binary, Decimal, and Unsigned Integer Float literals
// Parses Octal, Hex, Binary, Decimal, and Unsigned Integer Float literals.
//
// Conversion through JSON/YAML may have converted integers into floats, including
// exponential notation. This function will parse those values back into integers
Expand All @@ -437,7 +437,7 @@ func parseUnsignedLit(value string) (uint64, error) {
if floatErr != nil || parsedFloat < 0 {
return 0, err
}
// See if its actually an integer
// See if it's actually an integer.
parsed = uint64(parsedFloat)
if float64(parsed) != parsedFloat || parsed >= (1<<53) {
return parsed, errors.New("precision loss")
Expand Down
4 changes: 2 additions & 2 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ func Marshal(message proto.Message) ([]byte, error) {

// MarshalOptions is a configurable YAML format marshaller.
//
// It uses the similar options to protojson.MarshalOptions.
// It uses similar options to protojson.MarshalOptions.
type MarshalOptions struct {
// AllowPartial allows messages that have missing required fields to marshal
// without returning an error.
AllowPartial bool
// UseProtoNames uses proto field name instead of lowerCamelCase name in JSON
// UseProtoNames uses proto field name instead of lowerCamelCase name in YAML
// field names.
UseProtoNames bool
// UseEnumNumbers emits enum values as numbers.
Expand Down
Loading