summaryrefslogtreecommitdiff
path: root/vendor/github.com/bytedance/sonic/internal/encoder/compiler.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/bytedance/sonic/internal/encoder/compiler.go')
-rw-r--r--vendor/github.com/bytedance/sonic/internal/encoder/compiler.go29
1 files changed, 20 insertions, 9 deletions
diff --git a/vendor/github.com/bytedance/sonic/internal/encoder/compiler.go b/vendor/github.com/bytedance/sonic/internal/encoder/compiler.go
index 902fbc98b..737dd3e07 100644
--- a/vendor/github.com/bytedance/sonic/internal/encoder/compiler.go
+++ b/vendor/github.com/bytedance/sonic/internal/encoder/compiler.go
@@ -219,7 +219,7 @@ func (self *Compiler) compileOps(p *ir.Program, sp int, vt reflect.Type) {
case reflect.Struct:
self.compileStruct(p, sp, vt)
default:
- panic(vars.Error_type(vt))
+ self.compileUnsupportedType(p, vt)
}
}
@@ -440,7 +440,8 @@ func (self *Compiler) compileStructBody(p *ir.Program, sp int, vt reflect.Type)
p.Add(ir.OP_cond_set)
/* compile each field */
- for _, fv := range resolver.ResolveStruct(vt) {
+ fvs := resolver.ResolveStruct(vt)
+ for i, fv := range fvs {
var s []int
var o resolver.Offset
@@ -463,7 +464,12 @@ func (self *Compiler) compileStructBody(p *ir.Program, sp int, vt reflect.Type)
/* check for "omitempty" option */
if fv.Type.Kind() != reflect.Struct && fv.Type.Kind() != reflect.Array && (fv.Opts&resolver.F_omitempty) != 0 {
s = append(s, p.PC())
- self.compileStructFieldZero(p, fv.Type)
+ self.compileStructFieldEmpty(p, fv.Type)
+ }
+ /* check for "omitzero" option */
+ if fv.Opts&resolver.F_omitzero != 0 {
+ s = append(s, p.PC())
+ p.VField(ir.OP_is_zero, &fvs[i])
}
/* add the comma if not the first element */
@@ -574,7 +580,7 @@ func (self *Compiler) compileStructFieldStr(p *ir.Program, sp int, vt reflect.Ty
}
}
-func (self *Compiler) compileStructFieldZero(p *ir.Program, vt reflect.Type) {
+func (self *Compiler) compileStructFieldEmpty(p *ir.Program, vt reflect.Type) {
switch vt.Kind() {
case reflect.Bool:
p.Add(ir.OP_is_zero_1)
@@ -626,16 +632,16 @@ func (self *Compiler) compileStructFieldQuoted(p *ir.Program, sp int, vt reflect
}
func (self *Compiler) compileInterface(p *ir.Program, vt reflect.Type) {
- x := p.PC()
- p.Add(ir.OP_is_nil_p1)
-
/* iface and efaces are different */
if vt.NumMethod() == 0 {
p.Add(ir.OP_eface)
- } else {
- p.Add(ir.OP_iface)
+ return
}
+ x := p.PC()
+ p.Add(ir.OP_is_nil_p1)
+ p.Add(ir.OP_iface)
+
/* the "null" value */
e := p.PC()
p.Add(ir.OP_goto)
@@ -644,6 +650,11 @@ func (self *Compiler) compileInterface(p *ir.Program, vt reflect.Type) {
p.Pin(e)
}
+func (self *Compiler) compileUnsupportedType(p *ir.Program, vt reflect.Type) {
+ p.Rtt(ir.OP_unsupported, vt)
+}
+
+
func (self *Compiler) compileMarshaler(p *ir.Program, op ir.Op, vt reflect.Type, mt reflect.Type) {
pc := p.PC()
vk := vt.Kind()