Skip to content

Commit

Permalink
feat: field omit rule
Browse files Browse the repository at this point in the history
  • Loading branch information
vizee committed May 1, 2023
1 parent 335cb7a commit e161afb
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 18 deletions.
2 changes: 1 addition & 1 deletion jtop.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ func transJsonObject(p *proto.Encoder, j *JsonIter, msg *Message) error {
if len(key) != 0 {
// 暂不转义 key
field := msg.FieldByName(asString(key[1 : len(key)-1]))
if field != nil {
if field != nil && field.Omit != OmitAlways {
err := transJsonField(p, j, field, lead, s)
if err != nil {
return err
Expand Down
5 changes: 3 additions & 2 deletions jtop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ func transJsonObjectCase(j string, msg *Message) (string, error) {
func Test_transJsonObjectCase(t *testing.T) {
const (
complexJson = `{"noexisted":null,"fdouble":123,"ffloat":123,"fint32":123,"fint64":123,"fuint32":123,"fuint64":123,"fsint32":123,"fsint64":123,"ffixed32":123,"ffixed64":123,"fsfixed32":123,"fsfixed64":123,"fbool":true,"fstring":"okk","fbytes":"AQID","fmap1":{"k":1},"fmap2":{"u":{"name":"abc","age":23,"male":true},"v":null},"fsubmsg":{"name":"efg","age":23,"male":true},"fint32s":[1,2,3],"fitems":[{"name":"abc","age":12,"male":true},null,{"name":"efg","age":23}]}`
complexWant = `090000000000c05e40150000f642187b207b287b307b38f60140f6014d7b000000517b000000000000005d7b000000617b00000000000000680172036f6b6b7a030102038201050a016b10018a010e0a017512090a03616263101718018a01030a01769201090a03656667101718019a0103010203a201090a03616263100c1801a20100a201070a036566671017`
complexWant = `090000000000c05e40150000f642187b207b287b307b38f60140f6014d7b000000517b000000000000005d7b000000617b00000000000000680172036f6b6b7a030102038201050a016b10018a010c0a017512070a0361626310178a01030a01769201070a0365666710179a0103010203a201070a03616263100ca20100a201070a036566671017`
)
type args struct {
j string
Expand All @@ -317,7 +317,8 @@ func Test_transJsonObjectCase(t *testing.T) {
wantErr bool
}{
{name: "empty", args: args{j: `{}`, msg: getTestSimpleMessage()}, want: ""},
{name: "simple", args: args{j: `{"name":"string","age":123,"male":true}`, msg: getTestSimpleMessage()}, want: "0a06737472696e67107b1801"},
{name: "simple", args: args{j: `{"name":"string","age":123,"male":true}`, msg: getTestSimpleMessage()}, want: "0a06737472696e67107b"},
{name: "simple2", args: args{j: `{"name":"string","age":123,"male":true}`, msg: getTestSimpleMessage2()}, want: "1801"},
{name: "complex", args: args{j: complexJson, msg: getTestComplexMessage()}, want: complexWant},
{name: "eof", args: args{j: `{`, msg: getTestSimpleMessage()}, wantErr: true},
}
Expand Down
23 changes: 17 additions & 6 deletions metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,22 @@ func (m *Message) FieldByName(name string) *Field {
return nil
}

type OmitRule uint8

const (
// OmitProtoEmpty 只在 pb 输出"空值"时跳过字段
OmitProtoEmpty OmitRule = iota
// OmitEmpty 在 pb 和 json 输出"空值"跳过字段
OmitEmpty
// OmitAlways 总是跳过字段
OmitAlways
)

type Field struct {
Name string
Kind Kind
Ref *Message
Tag uint32
Repeated bool
OmitEmpty bool
Name string
Kind Kind
Ref *Message
Tag uint32
Repeated bool
Omit OmitRule
}
5 changes: 3 additions & 2 deletions ptoj.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ func transProtoMessage(j *JsonBuilder, p *proto.Decoder, msg *Message) error {
return ErrInvalidWireType
}

if emitted[fieldIdx] {
if emitted[fieldIdx] || field.Omit == OmitAlways {
continue
}

Expand Down Expand Up @@ -388,7 +388,7 @@ func transProtoMessage(j *JsonBuilder, p *proto.Decoder, msg *Message) error {

for i := range msg.Fields {
field := &msg.Fields[i]
if emitted[i] || field.OmitEmpty {
if emitted[i] || field.Omit >= OmitEmpty {
continue
}
if !more {
Expand All @@ -407,6 +407,7 @@ func transProtoMessage(j *JsonBuilder, p *proto.Decoder, msg *Message) error {
return nil
}

// TranscodeToJson 通过 proto.Decoder 解析 pb,并且追加到 JsonBuilder 中
func TranscodeToJson(j *JsonBuilder, p *proto.Decoder, msg *Message) error {
return transProtoMessage(j, p, msg)
}
9 changes: 5 additions & 4 deletions ptoj_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func Test_transProtoMapCase(t *testing.T) {
{name: "stop", args: args{p: "8201050a01621002", tag: 17, entry: getTestMapEntry(StringKind, Int32Kind, nil), s: "0a01611001"}, want: `{"a":1}`},
{name: "int_key", args: args{p: "0a0608c803120162", tag: 1, entry: getTestMapEntry(Int32Kind, StringKind, nil), s: "087b120161"}, want: `{"123":"a","456":"b"}`},
{name: "bytes_value", args: args{p: "", tag: 1, entry: getTestMapEntry(StringKind, BytesKind, nil), s: "0a0568656c6c6f1205776f726c64"}, want: `{"hello":"d29ybGQ="}`},
{name: "message_value", args: args{p: "", tag: 1, entry: getTestMapEntry(StringKind, MessageKind, getTestSimpleMessage()), s: "0a0361626312090a03626f6210171801"}, want: `{"abc":{"name":"bob","age":23,"male":true}}`},
{name: "message_value", args: args{p: "", tag: 1, entry: getTestMapEntry(StringKind, MessageKind, getTestSimpleMessage()), s: "0a0361626312090a03626f6210171801"}, want: `{"abc":{"name":"bob","age":23}}`},
{name: "default_key", args: args{p: "", tag: 1, entry: getTestMapEntry(StringKind, Int32Kind, nil), s: "107b"}, want: `{"":123}`},
{name: "default_int32_value", args: args{p: "", tag: 1, entry: getTestMapEntry(StringKind, Int32Kind, nil), s: "0a0161"}, want: `{"a":0}`},
{name: "default_string_value", args: args{p: "", tag: 1, entry: getTestMapEntry(StringKind, StringKind, nil), s: "0a0161"}, want: `{"a":""}`},
Expand Down Expand Up @@ -285,7 +285,7 @@ func transProtoMessageCase(p string, msg *Message) (string, error) {
func Test_transProtoMessageCase(t *testing.T) {
const (
complexProto = `090000000000c05e40150000f642187b207b287b307b38f60140f6014d7b000000517b000000000000005d7b000000617b00000000000000680172036f6b6b7a030102038201050a016b10018a010e0a017512090a03616263101718018a01050a017612009201090a03656667101718019a0103010203a201090a03616263100c1801a20100a201070a036566671017`
complexWant = `{"fdouble":123,"ffloat":123,"fint32":123,"fint64":123,"fuint32":123,"fuint64":123,"fsint32":123,"fsint64":123,"ffixed32":123,"ffixed64":123,"fsfixed32":123,"fsfixed64":123,"fbool":true,"fstring":"okk","fbytes":"AQID","fmap1":{"k":1},"fmap2":{"u":{"name":"abc","age":23,"male":true},"v":{}},"fsubmsg":{"name":"efg","age":23,"male":true},"fint32s":[1,2,3],"fitems":[{"name":"abc","age":12,"male":true},{},{"name":"efg","age":23}]}`
complexWant = `{"fdouble":123,"ffloat":123,"fint32":123,"fint64":123,"fuint32":123,"fuint64":123,"fsint32":123,"fsint64":123,"ffixed32":123,"ffixed64":123,"fsfixed32":123,"fsfixed64":123,"fbool":true,"fstring":"okk","fbytes":"AQID","fmap1":{"k":1},"fmap2":{"u":{"name":"abc","age":23},"v":{}},"fsubmsg":{"name":"efg","age":23},"fint32s":[1,2,3],"fitems":[{"name":"abc","age":12},{},{"name":"efg","age":23}]}`
complexDefaultWant = `{"fdouble":0,"ffloat":0,"fint32":0,"fint64":0,"fuint32":0,"fuint64":0,"fsint32":0,"fsint64":0,"ffixed32":0,"ffixed64":0,"fsfixed32":0,"fsfixed64":0,"fbool":false,"fstring":"","fbytes":"","fmap1":{},"fmap2":{},"fsubmsg":{},"fint32s":[],"fitems":[]}`
)

Expand All @@ -300,8 +300,9 @@ func Test_transProtoMessageCase(t *testing.T) {
wantErr bool
}{
{name: "empty", args: args{p: "", msg: getTestSimpleMessage()}, want: `{}`},
{name: "simple", args: args{p: "0a03626f6210171801", msg: getTestSimpleMessage()}, want: `{"name":"bob","age":23,"male":true}`},
{name: "emitted", args: args{p: "0a03626f6210170a03626f621801", msg: getTestSimpleMessage()}, want: `{"name":"bob","age":23,"male":true}`},
{name: "simple", args: args{p: "0a03626f6210171801", msg: getTestSimpleMessage()}, want: `{"name":"bob","age":23}`},
{name: "simple2", args: args{p: "0a03626f6210171801", msg: getTestSimpleMessage2()}, want: `{"male":true}`},
{name: "emitted", args: args{p: "0a03626f6210170a03626f621801", msg: getTestSimpleMessage()}, want: `{"name":"bob","age":23}`},
{name: "complex", args: args{p: complexProto, msg: getTestComplexMessage()}, want: complexWant},
{name: "default", args: args{p: "", msg: getTestComplexMessage()}, want: complexDefaultWant},
{name: "eof", args: args{p: "0a", msg: getTestComplexMessage()}, wantErr: true},
Expand Down
14 changes: 11 additions & 3 deletions types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@ package jsonpb

func getTestSimpleMessage() *Message {
return NewMessage("Simple", []Field{
{Name: "name", Tag: 1, Kind: StringKind, OmitEmpty: true},
{Name: "age", Tag: 2, Kind: Int32Kind, OmitEmpty: true},
{Name: "male", Tag: 3, Kind: BoolKind, OmitEmpty: true},
{Name: "name", Tag: 1, Kind: StringKind, Omit: OmitEmpty},
{Name: "age", Tag: 2, Kind: Int32Kind, Omit: OmitEmpty},
{Name: "male", Tag: 3, Kind: BoolKind, Omit: OmitAlways},
}, true, true)
}

func getTestSimpleMessage2() *Message {
return NewMessage("Simple", []Field{
{Name: "name", Tag: 1, Kind: StringKind, Omit: OmitAlways},
{Name: "age", Tag: 2, Kind: Int32Kind, Omit: OmitAlways},
{Name: "male", Tag: 3, Kind: BoolKind},
}, true, true)
}

Expand Down

0 comments on commit e161afb

Please sign in to comment.