refactor: API: separate typed and unsafe marshalling

The typed versions are used when we call from one marshaller to another.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
Bryan Boreham 2024-04-28 20:29:03 +01:00
parent 66a1c3daad
commit e0a00f45db

View File

@ -25,10 +25,10 @@ import (
) )
func init() { func init() {
jsoniter.RegisterTypeEncoderFunc("promql.Series", marshalSeriesJSON, neverEmpty) jsoniter.RegisterTypeEncoderFunc("promql.Series", unsafeMarshalSeriesJSON, neverEmpty)
jsoniter.RegisterTypeEncoderFunc("promql.Sample", marshalSampleJSON, neverEmpty) jsoniter.RegisterTypeEncoderFunc("promql.Sample", unsafeMarshalSampleJSON, neverEmpty)
jsoniter.RegisterTypeEncoderFunc("promql.FPoint", marshalFPointJSON, neverEmpty) jsoniter.RegisterTypeEncoderFunc("promql.FPoint", unsafeMarshalFPointJSON, neverEmpty)
jsoniter.RegisterTypeEncoderFunc("promql.HPoint", marshalHPointJSON, neverEmpty) jsoniter.RegisterTypeEncoderFunc("promql.HPoint", unsafeMarshalHPointJSON, neverEmpty)
jsoniter.RegisterTypeEncoderFunc("exemplar.Exemplar", marshalExemplarJSON, neverEmpty) jsoniter.RegisterTypeEncoderFunc("exemplar.Exemplar", marshalExemplarJSON, neverEmpty)
jsoniter.RegisterTypeEncoderFunc("labels.Labels", unsafeMarshalLabelsJSON, labelsIsEmpty) jsoniter.RegisterTypeEncoderFunc("labels.Labels", unsafeMarshalLabelsJSON, labelsIsEmpty)
} }
@ -66,8 +66,12 @@ func (j JSONCodec) Encode(resp *Response) ([]byte, error) {
// < more histograms > // < more histograms >
// ], // ],
// }, // },
func marshalSeriesJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) { func unsafeMarshalSeriesJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) {
s := *((*promql.Series)(ptr)) s := *((*promql.Series)(ptr))
marshalSeriesJSON(s, stream)
}
func marshalSeriesJSON(s promql.Series, stream *jsoniter.Stream) {
stream.WriteObjectStart() stream.WriteObjectStart()
stream.WriteObjectField(`metric`) stream.WriteObjectField(`metric`)
marshalLabelsJSON(s.Metric, stream) marshalLabelsJSON(s.Metric, stream)
@ -78,7 +82,7 @@ func marshalSeriesJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) {
stream.WriteObjectField(`values`) stream.WriteObjectField(`values`)
stream.WriteArrayStart() stream.WriteArrayStart()
} }
marshalFPointJSON(unsafe.Pointer(&p), stream) marshalFPointJSON(p, stream)
} }
if len(s.Floats) > 0 { if len(s.Floats) > 0 {
stream.WriteArrayEnd() stream.WriteArrayEnd()
@ -89,7 +93,7 @@ func marshalSeriesJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) {
stream.WriteObjectField(`histograms`) stream.WriteObjectField(`histograms`)
stream.WriteArrayStart() stream.WriteArrayStart()
} }
marshalHPointJSON(unsafe.Pointer(&p), stream) marshalHPointJSON(p, stream)
} }
if len(s.Histograms) > 0 { if len(s.Histograms) > 0 {
stream.WriteArrayEnd() stream.WriteArrayEnd()
@ -123,8 +127,12 @@ func neverEmpty(unsafe.Pointer) bool {
// }, // },
// "histogram": [ 1435781451.781, { < histogram, see jsonutil.MarshalHistogram > } ] // "histogram": [ 1435781451.781, { < histogram, see jsonutil.MarshalHistogram > } ]
// }, // },
func marshalSampleJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) { func unsafeMarshalSampleJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) {
s := *((*promql.Sample)(ptr)) s := *((*promql.Sample)(ptr))
marshalSampleJSON(s, stream)
}
func marshalSampleJSON(s promql.Sample, stream *jsoniter.Stream) {
stream.WriteObjectStart() stream.WriteObjectStart()
stream.WriteObjectField(`metric`) stream.WriteObjectField(`metric`)
marshalLabelsJSON(s.Metric, stream) marshalLabelsJSON(s.Metric, stream)
@ -147,8 +155,12 @@ func marshalSampleJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) {
} }
// marshalFPointJSON writes `[ts, "1.234"]`. // marshalFPointJSON writes `[ts, "1.234"]`.
func marshalFPointJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) { func unsafeMarshalFPointJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) {
p := *((*promql.FPoint)(ptr)) p := *((*promql.FPoint)(ptr))
marshalFPointJSON(p, stream)
}
func marshalFPointJSON(p promql.FPoint, stream *jsoniter.Stream) {
stream.WriteArrayStart() stream.WriteArrayStart()
jsonutil.MarshalTimestamp(p.T, stream) jsonutil.MarshalTimestamp(p.T, stream)
stream.WriteMore() stream.WriteMore()
@ -157,8 +169,12 @@ func marshalFPointJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) {
} }
// marshalHPointJSON writes `[ts, { < histogram, see jsonutil.MarshalHistogram > } ]`. // marshalHPointJSON writes `[ts, { < histogram, see jsonutil.MarshalHistogram > } ]`.
func marshalHPointJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) { func unsafeMarshalHPointJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) {
p := *((*promql.HPoint)(ptr)) p := *((*promql.HPoint)(ptr))
marshalHPointJSON(p, stream)
}
func marshalHPointJSON(p promql.HPoint, stream *jsoniter.Stream) {
stream.WriteArrayStart() stream.WriteArrayStart()
jsonutil.MarshalTimestamp(p.T, stream) jsonutil.MarshalTimestamp(p.T, stream)
stream.WriteMore() stream.WriteMore()