MCPcopy Create free account
hub / github.com/cloudwan/gohan / syncEvent

Method syncEvent

server/sync_writer.go:150–250  ·  view source on GitHub ↗
(resource *schema.Resource)

Source from the content-addressed store, hash-verified

148}
149
150func (writer *SyncWriter) syncEvent(resource *schema.Resource) error {
151 schemaManager := schema.GetManager()
152 eventSchema, _ := schemaManager.Schema("event")
153 return db.Within(writer.db, func(tx transaction.Transaction) error {
154 var err error
155 eventType := resource.Get("type").(string)
156 resourcePath := resource.Get("path").(string)
157 body := resource.Get("body").(string)
158 syncPlain := resource.Get("sync_plain").(bool)
159 syncProperty := resource.Get("sync_property").(string)
160
161 path := generatePath(resourcePath, body)
162
163 version, ok := resource.Get("version").(int)
164 if !ok {
165 log.Debug("cannot cast version value in int for %s", path)
166 }
167 log.Debug("event %s", eventType)
168
169 if eventType == "create" || eventType == "update" {
170 log.Debug("set %s on sync", path)
171
172 content := body
173
174 var data map[string]interface{}
175 if syncProperty != "" {
176 err = json.Unmarshal(([]byte)(body), &data)
177 if err != nil {
178 return fmt.Errorf("failed to unmarshal body on sync: %s", err)
179 }
180 target, ok := data[syncProperty]
181 if !ok {
182 return fmt.Errorf("could not find property `%s`", syncProperty)
183 }
184 jsonData, err := json.Marshal(target)
185 if err != nil {
186 return err
187 }
188 content = string(jsonData)
189 }
190
191 if syncPlain {
192 var target interface{}
193 json.Unmarshal([]byte(content), &target)
194 switch target.(type) {
195 case string:
196 content = fmt.Sprintf("%v", target)
197 }
198 } else {
199 data, err := json.Marshal(map[string]interface{}{
200 "body": content,
201 "version": version,
202 })
203 if err != nil {
204 return fmt.Errorf("failed to marshal marshalling sync object: %s", err)
205 }
206 content = string(data)
207 }

Callers 1

SyncMethod · 0.95

Calls 11

generatePathFunction · 0.85
SyncKeyTemplateMethod · 0.80
GenerateCustomPathMethod · 0.80
SchemaMethod · 0.65
WithinMethod · 0.65
GetMethod · 0.65
DebugMethod · 0.65
ErrorfMethod · 0.65
UpdateMethod · 0.65
DeleteMethod · 0.65
ErrorMethod · 0.65

Tested by

no test coverage detected