func (self *channelAPI) Blob_Mutation(perma grapher.PermaNode, mutation grapher.MutationNode) { mutJson := map[string]interface{}{"perma": perma.BlobRef(), "seq": mutation.SequenceNumber(), "type": "mutation", "signer": mutation.Signer(), "entity": mutation.EntityBlobRef(), "field": mutation.Field(), "time": mutation.Time()} switch mutation.Operation().(type) { case []ot.StringOperation: op := mutation.Operation().([]ot.StringOperation) // The following two lines work around a problem in GO/JSON mutJson["op"] = &op case []byte: msg := json.RawMessage(mutation.Operation().([]byte)) mutJson["op"] = &msg default: panic("Unsupported operation kind") } schema, err := json.Marshal(mutJson) if err != nil { panic(err.String()) } if self.bufferOnly { self.messageBuffer = append(self.messageBuffer, string(schema)) } else { err = self.forwardToFollowers(perma.BlobRef(), string(schema)) } if err != nil { log.Printf("Err Forward: %v", err) } }
// TODO: Perform type checking for other data types. This one does only strings func decodeGenericMutation(mutation grapher.MutationNode) (mut latestMutation, err os.Error) { switch mutation.Operation().(type) { case []byte: buffer := []byte(`{"op":`) buffer = append(buffer, mutation.Operation().([]byte)...) buffer = append(buffer, []byte("}")...) var val stringDummy err = json.Unmarshal(buffer, &val) if err != nil { return mut, err } mut.Operation = val.Value default: mut.Operation = mutation.Operation() } mut.Time = mutation.Time() return }