从字节切片中删除单个结构字段
Remove a single struct field from byte slice
我想知道如何修改我的 Write 方法中的字节。
要删除的字节构成日志记录服务类型的堆栈跟踪,它是 type errWithStackTrace struct
的一部分,因此我可以通过在字节切片上使用 json.Unmarshal 来获取该值见下文。如果存在该堆栈跟踪,我希望除此之外的所有内容都已写入。
func (w *customWriter) Write(p []byte) (int, error) {
e := loggedEvent{}
if err := json.Unmarshal(p, &e); err != nil {
return len(p), nil
}
if e.ErrWithStack.Stacktrace != nil {
// modify p to remove e.ErrWithStack.Stacktrace bytes?
return os.Stdout(p)
}
return os.Stdout(p)
}
我尝试将 e.ErrWithStack.Stacktrace
转换为字节然后替换它,但它仍然写入整个事件。
if e.ErrWithStack.Stacktrace != nil {
reqBytes := new(bytes.Buffer)
json.NewEncoder(reqBytes).Encode(e.ErrWithStack.Stacktrace)
res := bytes.Replace(p, reqBytes.Bytes(), []byte(""), 1)
return os.Stdout.Write(res)
}
return os.Stdout(p)
在这里没有意义,但无论如何,最终的问题是,如果输入是json,并且你已经解码了它,你不知道原始输入是。 找到要删除的字符串并不容易,因为你已经解码了。
您当然可以编写自己的 json 解析器并以这种方式查找和删除项目。这是保留 rest 编码的唯一方法。
一个更简单的选项是删除错误和 re-encode。你在这里没有展示的东西已经够多了,很难知道这是否可行,但我采用了你的示例例程,将其制作成 compile-able,并为其构建了一个小型测试框架,但在这里(和 a link to the Go Playground):
package main
import (
"encoding/json"
"fmt"
"os"
)
type loggedEvent struct {
Thing1 string `json:"thing1"`
ErrWithStackTrace *string `json:"err,omitempty"`
Thing2 string `json:"thing2"`
}
var inputs [][]byte = [][]byte{
[]byte(`{"thing1":"a string","err":"here is a stack trace","thing2":"another string"}`),
[]byte(`{"thing1":"a\u0020string","err":"here\u0020is\u0020a\u0020stack\u0020trace","thing2":"u-encoded"}`),
[]byte(`{"thing1":"more strings","thing2":"no error this time"}`),
}
type customWriter struct{}
func (w *customWriter) Write(p []byte) (int, error) {
e := loggedEvent{}
if err := json.Unmarshal(p, &e); err != nil {
fmt.Printf("note: unable to unmarshal %q\n", p)
return len(p), nil
}
if e.ErrWithStackTrace != nil {
// fmt.Printf("note: removing ErrWithStackTrace part\n")
e.ErrWithStackTrace = nil
}
s, err := json.Marshal(e)
if err != nil {
fmt.Printf("help, failed to re-marshal e=%v: err=%v\n", e, err)
return 0, err
}
s = append(s, '\n')
return os.Stdout.Write(s)
}
func main() {
w := &customWriter{}
for _, inp := range inputs {
ret, err := w.Write(inp)
if err != nil {
fmt.Printf("ret = %d, err = %v\n", ret, err)
}
}
}
我想知道如何修改我的 Write 方法中的字节。
要删除的字节构成日志记录服务类型的堆栈跟踪,它是 type errWithStackTrace struct
的一部分,因此我可以通过在字节切片上使用 json.Unmarshal 来获取该值见下文。如果存在该堆栈跟踪,我希望除此之外的所有内容都已写入。
func (w *customWriter) Write(p []byte) (int, error) {
e := loggedEvent{}
if err := json.Unmarshal(p, &e); err != nil {
return len(p), nil
}
if e.ErrWithStack.Stacktrace != nil {
// modify p to remove e.ErrWithStack.Stacktrace bytes?
return os.Stdout(p)
}
return os.Stdout(p)
}
我尝试将 e.ErrWithStack.Stacktrace
转换为字节然后替换它,但它仍然写入整个事件。
if e.ErrWithStack.Stacktrace != nil {
reqBytes := new(bytes.Buffer)
json.NewEncoder(reqBytes).Encode(e.ErrWithStack.Stacktrace)
res := bytes.Replace(p, reqBytes.Bytes(), []byte(""), 1)
return os.Stdout.Write(res)
}
return os.Stdout(p)
在这里没有意义,但无论如何,最终的问题是,如果输入是json,并且你已经解码了它,你不知道原始输入是。 找到要删除的字符串并不容易,因为你已经解码了。
您当然可以编写自己的 json 解析器并以这种方式查找和删除项目。这是保留 rest 编码的唯一方法。
一个更简单的选项是删除错误和 re-encode。你在这里没有展示的东西已经够多了,很难知道这是否可行,但我采用了你的示例例程,将其制作成 compile-able,并为其构建了一个小型测试框架,但在这里(和 a link to the Go Playground):
package main
import (
"encoding/json"
"fmt"
"os"
)
type loggedEvent struct {
Thing1 string `json:"thing1"`
ErrWithStackTrace *string `json:"err,omitempty"`
Thing2 string `json:"thing2"`
}
var inputs [][]byte = [][]byte{
[]byte(`{"thing1":"a string","err":"here is a stack trace","thing2":"another string"}`),
[]byte(`{"thing1":"a\u0020string","err":"here\u0020is\u0020a\u0020stack\u0020trace","thing2":"u-encoded"}`),
[]byte(`{"thing1":"more strings","thing2":"no error this time"}`),
}
type customWriter struct{}
func (w *customWriter) Write(p []byte) (int, error) {
e := loggedEvent{}
if err := json.Unmarshal(p, &e); err != nil {
fmt.Printf("note: unable to unmarshal %q\n", p)
return len(p), nil
}
if e.ErrWithStackTrace != nil {
// fmt.Printf("note: removing ErrWithStackTrace part\n")
e.ErrWithStackTrace = nil
}
s, err := json.Marshal(e)
if err != nil {
fmt.Printf("help, failed to re-marshal e=%v: err=%v\n", e, err)
return 0, err
}
s = append(s, '\n')
return os.Stdout.Write(s)
}
func main() {
w := &customWriter{}
for _, inp := range inputs {
ret, err := w.Write(inp)
if err != nil {
fmt.Printf("ret = %d, err = %v\n", ret, err)
}
}
}