MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / UnmarshalJSON

Method UnmarshalJSON

db/revtree.go:125–208  ·  view source on GitHub ↗
(inputjson []byte)

Source from the content-addressed store, hash-verified

123}
124
125func (tree *RevTree) UnmarshalJSON(inputjson []byte) (err error) {
126
127 if tree == nil {
128 // base.Warnf(base.KeyAll, "No RevTree for input %q", inputjson)
129 return nil
130 }
131 var rep revTreeList
132 err = base.JSONUnmarshal(inputjson, &rep)
133 if err != nil {
134 return
135 }
136
137 // validate revTreeList revs and parents lists are of equal length
138 if !(len(rep.Revs) == len(rep.Parents)) {
139 return errors.New("revtreelist data is invalid, revs/parents counts are inconsistent")
140 }
141
142 if len(rep.ChannelsMap) > 0 && len(rep.Channels_Old) > 0 {
143 return errors.New("revtreelist data is invalid, both channelsMap and channels fields are populated - expected only one")
144 }
145
146 *tree = make(RevTree, len(rep.Revs))
147
148 for i, revid := range rep.Revs {
149 info := RevInfo{ID: revid}
150 stringIndex := strconv.FormatInt(int64(i), 10)
151 if rep.BodyMap != nil {
152 if body := rep.BodyMap[stringIndex]; body != "" {
153 info.Body = []byte(body)
154 }
155 } else if rep.Bodies_Old != nil && len(rep.Bodies_Old[i]) > 0 {
156 info.Body = []byte(rep.Bodies_Old[i])
157 }
158 if rep.BodyKeyMap != nil {
159 bodyKey, ok := rep.BodyKeyMap[stringIndex]
160 if ok {
161 info.BodyKey = bodyKey
162 }
163 }
164 parentIndex := rep.Parents[i]
165 if parentIndex >= 0 {
166 info.Parent = rep.Revs[parentIndex]
167 }
168 (*tree)[revid] = &info
169 }
170 if rep.Deleted != nil {
171 for _, i := range rep.Deleted {
172 info := (*tree)[rep.Revs[i]]
173 info.Deleted = true // because tree[rep.Revs[i]].Deleted=true is a compile error
174 (*tree)[rep.Revs[i]] = info
175 }
176 }
177 if rep.HasAttachments != nil {
178 for _, i := range rep.HasAttachments {
179 info := (*tree)[rep.Revs[i]]
180 info.HasAttachments = true
181 (*tree)[rep.Revs[i]] = info
182 }

Callers 1

TestRevisionPruningLoopFunction · 0.95

Calls 3

winningRevisionMethod · 0.95
LeavesMethod · 0.95
JSONUnmarshalFunction · 0.92

Tested by 1

TestRevisionPruningLoopFunction · 0.76