From 49f6e151c4cb7dbd5ad5180106ebd2bcc3054cc9 Mon Sep 17 00:00:00 2001 From: Deng Ming Date: Fri, 29 Dec 2023 16:07:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20iox.JSONReader{?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- iox/json_reader.go | 50 +++++++++++++++++++++++++++++++++ iox/json_reader_test.go | 61 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 iox/json_reader.go create mode 100644 iox/json_reader_test.go diff --git a/iox/json_reader.go b/iox/json_reader.go new file mode 100644 index 0000000..48b4322 --- /dev/null +++ b/iox/json_reader.go @@ -0,0 +1,50 @@ +// Copyright 2021 ecodeclub +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package iox + +import ( + "bytes" + "encoding/json" +) + +type JSONReader struct { + val any + bf *bytes.Reader +} + +func (j *JSONReader) Read(p []byte) (n int, err error) { + if j.bf == nil { + var data []byte + data, err = json.Marshal(j.val) + if err == nil { + j.bf = bytes.NewReader(data) + } + } + if err != nil { + return + } + return j.bf.Read(p) +} + +// NewJSONReader 用于解决将一个结构体序列化为 JSON 之后,再封装为 io.Reader 的场景。 +// 该实现没有做任何输入检查。 +// 也就是你需要自己确保 val 是一个可以被 json 正确处理的东西。 +// 非线程安全。 +// 如果你传入的是 nil,那么读到的结果应该是 null。务必小心。 +func NewJSONReader(val any) *JSONReader { + return &JSONReader{ + val: val, + } +} diff --git a/iox/json_reader_test.go b/iox/json_reader_test.go new file mode 100644 index 0000000..111390d --- /dev/null +++ b/iox/json_reader_test.go @@ -0,0 +1,61 @@ +// Copyright 2021 ecodeclub +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package iox + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestJSONReader(t *testing.T) { + testCases := []struct { + name string + input []byte + val any + + wantRes []byte + wantN int + wantErr error + }{ + { + name: "正常读取", + input: make([]byte, 10), + wantN: 10, + val: User{Name: "Tom"}, + wantRes: []byte(`{"name":"T`), + }, + { + name: "输入 nil", + input: make([]byte, 7), + wantN: 4, + wantRes: append([]byte(`null`), 0, 0, 0), + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + reader := NewJSONReader(tc.val) + n, err := reader.Read(tc.input) + assert.Equal(t, tc.wantErr, err) + assert.Equal(t, tc.wantN, n) + assert.Equal(t, tc.wantRes, tc.input) + }) + } +} + +type User struct { + Name string `json:"name"` +}