Skip to content
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
12 changes: 6 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ jobs:
github.repository == 'stainless-sdks/cas-parser-go' &&
(github.event_name == 'push' || github.event.pull_request.head.repo.fork) && (github.event_name != 'push' || github.event.head_commit.message != 'codegen metadata')
steps:
- uses: actions/checkout@v6
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

- name: Get GitHub OIDC Token
if: |-
github.repository == 'stainless-sdks/cas-parser-go' &&
!startsWith(github.ref, 'refs/heads/stl/')
id: github-oidc
uses: actions/github-script@v8
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
with:
script: core.setOutput('github_token', await core.getIDToken());

Expand All @@ -53,10 +53,10 @@ jobs:
if: github.event_name == 'push' || github.event.pull_request.head.repo.fork

steps:
- uses: actions/checkout@v6
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

- name: Setup go
uses: actions/setup-go@v5
uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5.6.0
with:
go-version-file: ./go.mod

Expand All @@ -68,10 +68,10 @@ jobs:
runs-on: ${{ github.repository == 'stainless-sdks/cas-parser-go' && 'depot-ubuntu-24.04' || 'ubuntu-latest' }}
if: github.event_name == 'push' || github.event.pull_request.head.repo.fork
steps:
- uses: actions/checkout@v6
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

- name: Setup go
uses: actions/setup-go@v5
uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5.6.0
with:
go-version-file: ./go.mod

Expand Down
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "0.9.0"
".": "0.10.0"
}
4 changes: 2 additions & 2 deletions .stats.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
configured_endpoints: 21
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/cas-parser/cas-parser-e572d88c2af6e4d7bc4f7e119357fd3f68b1e67d612fd1d3a657d916cde0087c.yml
openapi_spec_hash: a9fc7d947111bffa9184f8ca8be4a579
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/cas-parser/cas-parser-904e3aa8081755d046016db9d84d13d140a4235c724e18e1cd7f8ebb7712883e.yml
openapi_spec_hash: 453b8e667c364b064e04352ad4deccfa
config_hash: 5509bb7a961ae2e79114b24c381606d4
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## 0.10.0 (2026-05-18)

Full Changelog: [v0.9.0...v0.10.0](https://github.com/CASParser/cas-parser-go/compare/v0.9.0...v0.10.0)

### Features

* **client:** optimize json encoder for internal types ([c4d3d55](https://github.com/CASParser/cas-parser-go/commit/c4d3d55d4d7111a3dd9eb5775be3d65f838a809c))

## 0.9.0 (2026-05-08)

Full Changelog: [v0.8.0...v0.9.0](https://github.com/CASParser/cas-parser-go/compare/v0.8.0...v0.9.0)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Or to pin the version:
<!-- x-release-please-start-version -->

```sh
go get -u 'github.com/CASParser/cas-parser-go@v0.9.0'
go get -u 'github.com/CASParser/cas-parser-go@v0.10.0'
```

<!-- x-release-please-end -->
Expand Down
21 changes: 15 additions & 6 deletions internal/encoding/json/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,21 @@ import (
// JSON cannot represent cyclic data structures and Marshal does not
// handle them. Passing cyclic structures to Marshal will result in
// an error.
func Marshal(v any) ([]byte, error) {
// EDIT(begin): add optimization options
func Marshal(v any, opts ...Option) ([]byte, error) {
// EDIT(end): add optimization options
e := newEncodeState()
defer encodeStatePool.Put(e)

// SHIM(begin): don't escape HTML by default
err := e.marshal(v, encOpts{escapeHTML: shims.EscapeHTMLByDefault})
// EDIT(begin): don't escape HTML by default, and apply options
encOpts := encOpts{escapeHTML: shims.EscapeHTMLByDefault}
if opts != nil {
encOpts = encOpts.apply(opts...)
}
err := e.marshal(v, encOpts)
// ORIGINAL:
// err := e.marshal(v, encOpts{escapeHTML: true})
// SHIM(end)
// EDIT(end)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -352,6 +358,9 @@ type encOpts struct {
// EDIT(begin): save the timefmt
timefmt string
// EDIT(end)
// EDIT(begin): add optimization to skip compaction
skipCompaction bool
// EDIT(end)
}

type encoderFunc func(e *encodeState, v reflect.Value, opts encOpts)
Expand Down Expand Up @@ -483,7 +492,7 @@ func marshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) {
if err == nil {
e.Grow(len(b))
out := e.AvailableBuffer()
out, err = appendCompact(out, b, opts.escapeHTML)
out, err = appendCompact(out, b, opts)
e.Buffer.Write(out)
}
if err != nil {
Expand All @@ -509,7 +518,7 @@ func addrMarshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) {
if err == nil {
e.Grow(len(b))
out := e.AvailableBuffer()
out, err = appendCompact(out, b, opts.escapeHTML)
out, err = appendCompact(out, b, opts)
e.Buffer.Write(out)
}
if err != nil {
Expand Down
17 changes: 14 additions & 3 deletions internal/encoding/json/indent.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

package json

import "bytes"
import (
"bytes"
)

// HTMLEscape appends to dst the JSON-encoded src with <, >, &, U+2028 and U+2029
// characters inside string literals changed to \u003c, \u003e, \u0026, \u2028, \u2029
Expand Down Expand Up @@ -41,12 +43,21 @@ func appendHTMLEscape(dst, src []byte) []byte {
func Compact(dst *bytes.Buffer, src []byte) error {
dst.Grow(len(src))
b := dst.AvailableBuffer()
b, err := appendCompact(b, src, false)
b, err := appendCompact(b, src, encOpts{})
dst.Write(b)
return err
}

func appendCompact(dst, src []byte, escape bool) ([]byte, error) {
func appendCompact(dst, src []byte, opts encOpts) ([]byte, error) {
// EDIT(begin): optimize for skipCompaction
if opts.skipCompaction {
dst = append(dst, src...)
return dst, nil
}

escape := opts.escapeHTML
// EDIT(end)

origLen := len(dst)
scan := newScanner()
defer freeScanner(scan)
Expand Down
24 changes: 24 additions & 0 deletions internal/encoding/json/opt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// EDIT(begin): add custom options for JSON encoding
package json

type Option func(*encOpts)

// Every time a sub-type of [json.Marshaler] is encountered,
// skip a redundant and costly compaction step, trust it to self-compact.
//
// This is a divergence from the standard library behavior, and is only guaranteed
// safe with SDK types.
func WithSkipCompaction(b bool) Option {
return func(eos *encOpts) {
eos.skipCompaction = true
}
}

func (eos encOpts) apply(opts ...Option) encOpts {
for _, opt := range opts {
opt(&eos)
}
return eos
}

// EDIT(end)
53 changes: 28 additions & 25 deletions internal/encoding/json/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package json

import (
"bytes"
"errors"
"io"
)

Expand Down Expand Up @@ -253,30 +252,34 @@ func (enc *Encoder) SetEscapeHTML(on bool) {
enc.escapeHTML = on
}

// RawMessage is a raw encoded JSON value.
// It implements [Marshaler] and [Unmarshaler] and can
// be used to delay JSON decoding or precompute a JSON encoding.
type RawMessage []byte

// MarshalJSON returns m as the JSON encoding of m.
func (m RawMessage) MarshalJSON() ([]byte, error) {
if m == nil {
return []byte("null"), nil
}
return m, nil
}

// UnmarshalJSON sets *m to a copy of data.
func (m *RawMessage) UnmarshalJSON(data []byte) error {
if m == nil {
return errors.New("json.RawMessage: UnmarshalJSON on nil pointer")
}
*m = append((*m)[0:0], data...)
return nil
}

var _ Marshaler = (*RawMessage)(nil)
var _ Unmarshaler = (*RawMessage)(nil)
// EDIT(begin): remove RawMessage
//
// // RawMessage is a raw encoded JSON value.
// // It implements [Marshaler] and [Unmarshaler] and can
// // be used to delay JSON decoding or precompute a JSON encoding.
// type RawMessage []byte
//
// // MarshalJSON returns m as the JSON encoding of m.
// func (m RawMessage) MarshalJSON() ([]byte, error) {
// if m == nil {
// return []byte("null"), nil
// }
// return m, nil
// }
//
// // UnmarshalJSON sets *m to a copy of data.
// func (m *RawMessage) UnmarshalJSON(data []byte) error {
// if m == nil {
// return errors.New("json.RawMessage: UnmarshalJSON on nil pointer")
// }
// *m = append((*m)[0:0], data...)
// return nil
// }
//
// var _ Marshaler = (*RawMessage)(nil)
// var _ Unmarshaler = (*RawMessage)(nil)
//
// EDIT(end)

// A Token holds a value of one of these types:
//
Expand Down
2 changes: 1 addition & 1 deletion internal/encoding/json/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func timeMarshalEncoder(e *encodeState, v reflect.Value, opts encOpts) bool {
if b != nil {
e.Grow(len(b))
out := e.AvailableBuffer()
out, _ = appendCompact(out, b, opts.escapeHTML)
out, _ = appendCompact(out, b, opts)
e.Buffer.Write(out)
return true
}
Expand Down
2 changes: 1 addition & 1 deletion internal/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

package internal

const PackageVersion = "0.9.0" // x-release-please-version
const PackageVersion = "0.10.0" // x-release-please-version
4 changes: 2 additions & 2 deletions packages/param/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func MarshalWithExtras[T ParamStruct, R any](f T, underlying any, extras map[str
} else if ovr, ok := f.Overrides(); ok {
return shimjson.Marshal(ovr)
} else {
return shimjson.Marshal(underlying)
return shimjson.Marshal(underlying, shimjson.WithSkipCompaction(true))
}
}

Expand Down Expand Up @@ -96,7 +96,7 @@ func MarshalUnion[T ParamStruct](metadata T, variants ...any) ([]byte, error) {
Err: fmt.Errorf("expected union to have only one present variant, got %d", nPresent),
}
}
return shimjson.Marshal(variants[presentIdx])
return shimjson.Marshal(variants[presentIdx], shimjson.WithSkipCompaction(true))
}

// typeFor is shimmed from Go 1.23 "reflect" package
Expand Down
Loading
Loading