func RedecodeContact(contactdb *db.ContactDB, c *pb.Contact) { if c == nil { log.Fatalf("Contact not found") } log.Printf("Original contact:\n%s\n", proto.MarshalTextString(c)) new_blobs := make([]*pb.Contact_Blob, 0) var freeform []byte for _, b := range c.Blob { if b.Format != nil && (*b.Format == pb.Contact_Blob_DATUM || *b.Format == pb.Contact_Blob_FRAME) { // Strip out datums and frames. continue } new_blobs = append(new_blobs, b) if b.Format != nil && *b.Format == pb.Contact_Blob_FREEFORM { if freeform != nil { log.Fatalf("Contact contains multiple FREEFORM blobs.") } freeform = b.InlineData } } if freeform == nil { return } data, frames := telemetry.DecodeFreeform( *c.SatelliteId, freeform, *c.StartTimestamp) for i, _ := range frames { b := new(pb.Contact_Blob) b.Format = pb.Contact_Blob_FRAME.Enum() b.InlineData = frames[i] new_blobs = append(new_blobs, b) } for i, _ := range data { b := new(pb.Contact_Blob) b.Format = pb.Contact_Blob_DATUM.Enum() b.Datum = &data[i] new_blobs = append(new_blobs, b) } c.Blob = new_blobs log.Printf("New contact:\n%s\n", proto.MarshalTextString(c)) err := contactdb.Store(c) if err != nil { log.Fatalf("Error storing contact: %s", err.Error()) } }
func decodeMorse(satellite_id, data string) { pl, err := telemetry.DecodeMorse(satellite_id, data, 0) if err != nil { fmt.Printf("DecodeMorse error: %s\n", err.Error()) return } for _, p := range pl { fmt.Printf("%s\n", proto.MarshalTextString(&p)) } }
func main() { flag.Parse() if *about { fmt.Println(proto.MarshalTextString(serverHello)) } if *httpOn { fmt.Printf("starting server: %v\n", *httpListen) ihttp.StartServer(*httpListen) } }
func (c *context) Call(service, method string, in, out proto.Message, opts *internal.CallOptions) error { req, err := proto.Marshal(in) if err != nil { return fmt.Errorf("error marshalling request: %v", err) } remReq := &pb.Request{ ServiceName: proto.String(service), Method: proto.String(method), Request: req, // NOTE(djd): RequestId is unused in the server. } req, err = proto.Marshal(remReq) if err != nil { return fmt.Errorf("proto.Marshal: %v", err) } // TODO(djd): Respect opts.Timeout? resp, err := c.client.Post(c.url, "application/octet-stream", bytes.NewReader(req)) if err != nil { return fmt.Errorf("error sending request: %v", err) } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if resp.StatusCode != http.StatusOK { return fmt.Errorf("bad response %d; body: %q", resp.StatusCode, body) } if err != nil { return fmt.Errorf("failed reading response: %v", err) } remResp := &pb.Response{} if err := proto.Unmarshal(body, remResp); err != nil { return fmt.Errorf("error unmarshalling response: %v", err) } if ae := remResp.GetApplicationError(); ae != nil { return &internal.APIError{ Code: ae.GetCode(), Detail: ae.GetDetail(), Service: service, } } if remResp.Response == nil { return fmt.Errorf("unexpected response: %s", proto.MarshalTextString(remResp)) } return proto.Unmarshal(remResp.Response, out) }
func TestSerializedDeserialize(t *testing.T) { original, err := ioutil.ReadFile("testdata/stateproto") if err != nil { t.Fatalf("Failed to read stateproto: %s", err) } state := new(disk.State) if err := proto.Unmarshal(original, state); err != nil { t.Fatalf("Failed to parse stateproto: %s", err) } ioutil.WriteFile("a", []byte(proto.MarshalTextString(state)), 0600) var buffer bytes.Buffer entities := serialise(&buffer, state) textual := append([]byte(nil), buffer.Bytes()...) ioutil.WriteFile("text", []byte(textual), 0600) newState := new(disk.State) if err := parse(newState, &buffer, entities); err != nil { t.Fatalf("Failed to parse textual stateproto: %s", err) } buffer.Reset() serialise(&buffer, state) textual2 := buffer.Bytes() if !bytes.Equal(textual, textual2) { } result, err := proto.Marshal(newState) if err != nil { t.Fatalf("Failed to serialise new state: %s", err) } if !bytes.Equal(original, result) { ioutil.WriteFile("b", []byte(proto.MarshalTextString(newState)), 0600) t.Fatalf("Result does not equal original") } }
func convertMessageType(curPkg *ProtoPackage, msg *descriptor.DescriptorProto) (schema []*Field, err error) { if glog.V(4) { glog.Info("Converting message: ", proto.MarshalTextString(msg)) } for _, fieldDesc := range msg.GetField() { field, err := convertField(curPkg, fieldDesc, msg) if err != nil { glog.Errorf("Failed to convert field %s in %s: %v", fieldDesc.GetName(), msg.GetName(), err) return nil, err } schema = append(schema, field) } return }
func decodeHexFrame(satellite_id, data string) { frame, err := hex.DecodeString(data) if err != nil { fmt.Printf("Not a hex frame: %s\n", err.Error()) return } pl, err := telemetry.DecodeFrame(satellite_id, frame, 0) if err != nil { fmt.Printf("DecodeFrame error: %s\n", err.Error()) return } for _, p := range pl { fmt.Printf("%s\n", proto.MarshalTextString(&p)) } }
func marchalText() { m := &MyMessage{Count: proto.Int32(1234), Name: proto.String("protoName"), Quote: proto.String("protoQuote")} m.Pet = []string{"1", "2", "3"} // MarshalTextString fmt.Println("// MarshalTextString...") mstr := proto.MarshalTextString(m) fmt.Println(mstr) var target MyMessage proto.UnmarshalText(mstr, &target) fmt.Printf("%#v\n", target) fmt.Printf("%s\n", target.String()) fmt.Println("// MarshalTextString...\n") // MarshalText fmt.Println("// MarshalText...") mterr := proto.MarshalText(os.Stdout, m) checkerr(mterr) var target1 MyMessage umterr := proto.UnmarshalText(mstr, &target1) checkerr(umterr) fmt.Printf("%#v\n", target1) fmt.Println(target1.String()) fmt.Println("// MarshalText...\n") // Marshal fmt.Println("// Marshal...") data, _ := proto.Marshal(m) fmt.Println(data) var target2 MyMessage uerr := proto.Unmarshal(data, &target2) checkerr(uerr) fmt.Printf("%#v\n", target2) fmt.Printf("%s\n", target2.String()) fmt.Println("// Marshal...\n") // // MarshalSet // fmt.Println("// MarshalSet...") // var mset proto.MessageSet // merr := mset.Marshal(m) // checkerr(merr) // fmt.Println(mset.Item) // fmt.Println(mset.XXX_unrecognized) // var target3 MyMessage // mset.Unmarshal(&target3) // fmt.Printf("%#v\n", target3) }
func TestRepeatedNilText(t *testing.T) { m := &pb.MessageList{ Message: []*pb.MessageList_Message{ nil, &pb.MessageList_Message{ Name: proto.String("Horse"), }, nil, }, } want := `Message <nil> Message { name: "Horse" } Message <nil> ` if s := proto.MarshalTextString(m); s != want { t.Errorf(" got: %s\nwant: %s", s, want) } }
func ExampleSummary() { temps := prometheus.NewSummary(prometheus.SummaryOpts{ Name: "pond_temperature_celsius", Help: "The temperature of the frog pond.", // Sorry, we can't measure how badly it smells. }) // Simulate some observations. for i := 0; i < 1000; i++ { temps.Observe(30 + math.Floor(120*math.Sin(float64(i)*0.1))/10) } // Just for demonstration, let's check the state of the summary by // (ab)using its Write method (which is usually only used by Prometheus // internally). metric := &dto.Metric{} temps.Write(metric) fmt.Println(proto.MarshalTextString(metric)) // Output: // summary: < // sample_count: 1000 // sample_sum: 29969.50000000001 // quantile: < // quantile: 0.5 // value: 31.1 // > // quantile: < // quantile: 0.9 // value: 41.3 // > // quantile: < // quantile: 0.99 // value: 41.9 // > // > }
func protobufToString(p proto.Message, indentLevel int) string { return prefixLines(proto.MarshalTextString(p), strings.Repeat(" ", indentLevel)) }
// WriteProtoText writes the MetricFamily to the writer in text format and // returns the number of bytes written and any error encountered. func WriteProtoText(w io.Writer, p *dto.MetricFamily) (int, error) { return fmt.Fprintf(w, "%s\n", proto.MarshalTextString(p)) }
func ExampleSummaryVec() { temps := prometheus.NewSummaryVec( prometheus.SummaryOpts{ Name: "pond_temperature_celsius", Help: "The temperature of the frog pond.", // Sorry, we can't measure how badly it smells. }, []string{"species"}, ) // Simulate some observations. for i := 0; i < 1000; i++ { temps.WithLabelValues("litoria-caerulea").Observe(30 + math.Floor(120*math.Sin(float64(i)*0.1))/10) temps.WithLabelValues("lithobates-catesbeianus").Observe(32 + math.Floor(100*math.Cos(float64(i)*0.11))/10) } // Just for demonstration, let's check the state of the summary vector // by (ab)using its Collect method and the Write method of its elements // (which is usually only used by Prometheus internally - code like the // following will never appear in your own code). metricChan := make(chan prometheus.Metric) go func() { defer close(metricChan) temps.Collect(metricChan) }() metricStrings := []string{} for metric := range metricChan { dtoMetric := &dto.Metric{} metric.Write(dtoMetric) metricStrings = append(metricStrings, proto.MarshalTextString(dtoMetric)) } sort.Strings(metricStrings) // For reproducible print order. fmt.Println(metricStrings) // Output: // [label: < // name: "species" // value: "lithobates-catesbeianus" // > // summary: < // sample_count: 1000 // sample_sum: 31956.100000000017 // quantile: < // quantile: 0.5 // value: 32.4 // > // quantile: < // quantile: 0.9 // value: 41.4 // > // quantile: < // quantile: 0.99 // value: 41.9 // > // > // label: < // name: "species" // value: "litoria-caerulea" // > // summary: < // sample_count: 1000 // sample_sum: 29969.50000000001 // quantile: < // quantile: 0.5 // value: 31.1 // > // quantile: < // quantile: 0.9 // value: 41.3 // > // quantile: < // quantile: 0.99 // value: 41.9 // > // > // ] }
package proto import ( "code.google.com/p/goprotobuf/proto" // "fmt" "os" "testing" ) var ( msg = &MyMessage{Count: proto.Int32(1234), Name: proto.String("protoName"), Quote: proto.String("protoQuote")} mardata, _ = proto.Marshal(msg) marTextStr = proto.MarshalTextString(msg) ) // func TestMain(*testing.T) { // marchalText() // } func TestUnMarshal(t *testing.T) { var imsg IMessage // var imsg proto.Message err := proto.Unmarshal(mardata, &imsg) checkerr(err) t.Log(imsg) } // func TestMarshal(t *testing.T) { // msg_new := new(MyMessage) // err := proto.Unmarshal([]byte(marTextStr), msg_new) // checkerr(err)
// String returns an ASCII serialization of the loaded configuration protobuf. func (c Config) String() string { return proto.MarshalTextString(&c.PrometheusConfig) }
func main() { uj := &Pmd.UserJsMessageForwardUserPmd_CS{} uj.Msgbytes = []byte(`{"whj":111}`) fmt.Println("XXXXX", uj.String(), proto.MarshalTextString(uj)) rc := &Pmd.ReconnectErrorLoginUserPmd_S{} rc.Desc = proto.String(`{"whj":111}`) fmt.Println("xxxxx", rc.String(), proto.MarshalTextString(rc), *rc.Desc) m := make(map[int]string) m[1] = "wabghaijun" a := 1 fmt.Println(protobuf.Encode(&a)) mset := make(map[int32]proto.Extension) //mset[1] = proto.Extension{enc: []byte("sss")} //umset, err := proto.MarshalMessageSet(mset) var b []byte fmt.Println(len(b)) nmd := &Pmd.ForwardNullUserPmd_CS{} nmd1 := &Pmd.ForwardNullUserPmd_CS{} nmd2 := &Pmd.ForwardNullUserPmd_CS{} cmd3 := &Pmd.RequestCloseNullUserPmd_CS{} cmd4 := &Pmd.RequestCloseNullUserPmd_CS{} cmd3.Reason = proto.String("2222") fmt.Println(proto.GetProperties(reflect.TypeOf(cmd3).Elem())) cmd3byte, err1 := proto.Marshal(cmd3) if err1 != nil { fmt.Println("xxxxxxxxxxx", err1) } fmt.Println(proto.Unmarshal(cmd3byte, cmd3)) fmt.Println(mset) cmd3test := proto.MarshalTextString(cmd3) fmt.Println(cmd3test) fmt.Println(proto.UnmarshalText(cmd3test, nmd)) //nmd.Prototype = proto.Uint64(2) nmd.ByCmd = proto.Uint32(0) //nmd.ByParam = proto.Uint32(0) //nmd.ByCmd = append(nmd.ByCmd, 0) //nmd.ByParam = append(nmd.ByParam, 0) sendbuf := proto.NewBuffer(nil) err := sendbuf.Marshal(nmd) if err != nil { fmt.Println("1", err) } nmd.Data = sendbuf.Bytes() fmt.Println(nmd, proto.Size(nmd), len(sendbuf.Bytes())) fmt.Println(len(sendbuf.Bytes()), sendbuf.Bytes()) //data := sendbuf.Bytes() err = sendbuf.Marshal(cmd3) if err != nil { fmt.Println("2", err) } fmt.Println(len(sendbuf.Bytes()), sendbuf.Bytes()) data := sendbuf.Bytes() fmt.Println(len(data), data) //data = append(data, byte(1)) databuf := proto.NewBuffer(data) err = databuf.Unmarshal(nmd1) if err != nil { fmt.Println("3", err) } //err = databuf.Unmarshal(nmd2) err = proto.Unmarshal(data[:2], nmd2) if err != nil { fmt.Println("4", err) } err = proto.Unmarshal(data[2:], cmd4) //err = databuf.Unmarshal(cmd4) if err != nil { fmt.Println("5", err) } fmt.Println(nmd, proto.Size(nmd)) fmt.Println(nmd1) fmt.Println(nmd2) fmt.Println(cmd4) }