github.com/chrislusf/greenpack@v3.7.1-0.20170911073826-ad5bd10b7c47+incompatible/gen/fieldsempty.go (about) 1 package gen 2 3 import ( 4 "fmt" 5 "io" 6 7 "github.com/glycerine/greenpack/cfg" 8 ) 9 10 func fieldsempty(w io.Writer, cfg *cfg.GreenConfig) *fieldsEmpty { 11 return &fieldsEmpty{ 12 p: printer{w: w}, 13 cfg: cfg, 14 } 15 } 16 17 type fieldsEmpty struct { 18 passes 19 p printer 20 recvr string 21 cfg *cfg.GreenConfig 22 } 23 24 func (e *fieldsEmpty) MethodPrefix() string { 25 return e.cfg.MethodPrefix 26 } 27 28 func (e *fieldsEmpty) Method() Method { return FieldsEmpty } 29 30 func (e *fieldsEmpty) Execute(p Elem) error { 31 // if DEBUG { 32 // fmt.Printf("Execute()-ing elem = p='%#v'\n", p) 33 // } 34 if !e.p.ok() { 35 return e.p.err 36 } 37 p = e.applyall(p) 38 if p == nil { 39 return nil 40 } 41 if !IsPrintable(p) { 42 return nil 43 } 44 45 e.recvr = fmt.Sprintf("%s %s", p.Varname(), imutMethodReceiver(p)) 46 47 next(e, p) 48 return e.p.err 49 } 50 51 func (e *fieldsEmpty) gStruct(s *Struct) { 52 if e.cfg.AllTuple { 53 return 54 } 55 // fmt.Printf("\n\n 77777 gStruct starting for fieldsempty s='%#v'\n\n ******* and e.recvr = '%#v'\n\n", s, e.recvr) 56 e.p.printf("// %sfieldsNotEmpty supports omitempty tags\n", e.cfg.MethodPrefix) 57 58 e.p.printf("func (%s) %sfieldsNotEmpty(isempty []bool) uint32 {", 59 e.recvr, e.cfg.MethodPrefix) 60 61 allFieldsEmpty := !e.cfg.SerzEmpty 62 63 nfields := len(s.Fields) - s.SkipCount 64 numOE := 0 65 for i := range s.Fields { 66 if s.Fields[i].OmitEmpty { 67 numOE++ 68 } 69 } 70 71 // default is SerzEmpty false, so by 72 // default we will treat all as being omitempty. 73 // This is safe since we will zero any re-used 74 // struct's fields when reading. 75 // 76 if e.cfg.SerzEmpty { 77 // even under SerzEmpty, we still respect the specific ,omitempty tag: 78 if numOE == 0 { 79 // no fields tagged with omitempty, just return the full field count. 80 e.p.printf("\nreturn %d }\n", nfields) 81 return 82 } 83 } 84 // remember this to avoid recomputing it in other passes. 85 s.hasOmitEmptyTags = true 86 87 om := emptyOmitter(&e.p, s.vname) 88 89 e.p.printf("if len(isempty) == 0 { return %d }\n", nfields) 90 e.p.printf("var fieldsInUse uint32 = %d\n", nfields) 91 for i := range s.Fields { 92 if s.Fields[i].Skip { 93 continue 94 } 95 if allFieldsEmpty || s.Fields[i].OmitEmpty { 96 e.p.printf("isempty[%d] = ", i) 97 next(om, s.Fields[i].FieldElem) 98 99 e.p.printf("if isempty[%d] { fieldsInUse-- ; }\n", i) 100 //or: e.p.printf("if isempty[%d] { fieldsInUse-- ; fmt.Printf(\"\\n %s is not in use!\\n \")}\n", i, s.Fields[i].FieldTagZidClue) 101 } 102 } 103 //e.p.printf("\n fmt.Printf(\"\\n\\n fieldsInUse=%%v\", fieldsInUse) \n\n") 104 e.p.printf("\n return fieldsInUse \n}\n") 105 } 106 107 func (e *fieldsEmpty) gPtr(p *Ptr) { 108 //next(e, p.Value) 109 } 110 111 func (e *fieldsEmpty) gSlice(sl *Slice) { 112 //next(e, sl.Els) 113 } 114 115 func (e *fieldsEmpty) gArray(a *Array) { 116 //fmt.Printf("\n\n 66666 gArray starting for fieldsempty a='%#v'\n\n", a) 117 //next(e, a.Els) 118 } 119 120 func (e *fieldsEmpty) gMap(m *Map) { 121 // fmt.Printf("\n\n 55555 gMap starting for fieldsempty m='%#v'\n\n", m) 122 //next(e, m.Value) 123 } 124 125 func (e *fieldsEmpty) gBase(b *BaseElem) {}