func TestPBS(t *testing.T) { memStore := NewMemoryStore() stores := [1]DataStore{memStore} ds := NewPBSDataStore(&stores) test := &Test{ Label: proto.String("hello"), Type: proto.Int32(17), Optionalgroup: &Test_OptionalGroup{ RequiredField: proto.String("good bye"), }, } key, err := ds.Put(test) if err != nil { t.Fatalf("Put failed: %q", err.String()) } t.Logf("Key: %q", key) test2 := NewTest() err2 := ds.Get(key, test2) if err2 != nil { t.Fatalf("Get failed: %q", err.String()) } t.Logf("Value: %q", *test2.Label) }
func TestStringEscaping(t *testing.T) { testCases := []struct { in *pb.Strings out string }{ { // Test data from C++ test (TextFormatTest.StringEscape). // Single divergence: we don't escape apostrophes. &pb.Strings{StringField: proto.String("\"A string with ' characters \n and \r newlines and \t tabs and \001 slashes \\ and multiple spaces")}, "string_field: \"\\\"A string with ' characters \\n and \\r newlines and \\t tabs and \\001 slashes \\\\ and multiple spaces\"\n", }, { // Test data from the same C++ test. &pb.Strings{StringField: proto.String("\350\260\267\346\255\214")}, "string_field: \"\\350\\260\\267\\346\\255\\214\"\n", }, } for i, tc := range testCases { var buf bytes.Buffer proto.MarshalText(&buf, tc.in) if s := buf.String(); s != tc.out { t.Errorf("#%d: Got:\n%s\nExpected:\n%s\n", i, s, tc.out) } } }
func (p *parser) readEnum(e *EnumDescriptorProto) *parseError { if err := p.readToken("enum"); err != nil { return err } tok := p.next() if tok.err != nil { return tok.err } // TODO: check that the name is acceptable. e.Name = proto.String(tok.value) if err := p.readToken("{"); err != nil { return err } // Parse enum values for !p.done { tok := p.next() if tok.err != nil { return tok.err } if tok.value == "}" { // end of enum return nil } // TODO: verify tok.value is a valid enum value name. ev := new(EnumValueDescriptorProto) e.Value = append(e.Value, ev) ev.Name = proto.String(tok.value) if err := p.readToken("="); err != nil { return err } tok = p.next() if tok.err != nil { return tok.err } // TODO: check that tok.value is a valid enum value number. num, err := atoi32(tok.value) if err != nil { return p.error("bad enum number %q: %v", tok.value, err) } ev.Number = proto.Int32(num) if err := p.readToken(";"); err != nil { return err } } return p.error("unexpected end while parsing enum") }
// toProto converts the query to a protocol buffer. func (q *Query) toProto(appID string) (*pb.Query, os.Error) { if q.kind == "" { return nil, os.NewError("datastore: empty query kind") } x := &pb.Query{ App: proto.String(appID), Kind: proto.String(q.kind), } if q.ancestor != nil { x.Ancestor = keyToProto(appID, q.ancestor) } if q.keysOnly { x.KeysOnly = proto.Bool(true) x.RequirePerfectPlan = proto.Bool(true) } for _, qf := range q.filter { if qf.FieldName == "" { return nil, os.NewError("datastore: empty query filter field name") } p, errStr := valueToProto(appID, qf.FieldName, reflect.ValueOf(qf.Value), false) if errStr != "" { return nil, os.NewError("datastore: bad query filter value type: " + errStr) } xf := &pb.Query_Filter{ Op: operatorToProto[qf.Op], Property: []*pb.Property{p}, } if xf.Op == nil { return nil, os.NewError("datastore: unknown query filter operator") } x.Filter = append(x.Filter, xf) } for _, qo := range q.order { if qo.FieldName == "" { return nil, os.NewError("datastore: empty query order field name") } xo := &pb.Query_Order{ Property: proto.String(qo.FieldName), Direction: sortDirectionToProto[qo.Direction], } if xo.Direction == nil { return nil, os.NewError("datastore: unknown query order direction") } x.Order = append(x.Order, xo) } if q.limit != 0 { x.Limit = proto.Int(q.limit) } if q.offset != 0 { x.Offset = proto.Int(q.offset) } return x, nil }
func makeLogin(name string, authToken string, permissions uint32) (msg *protocol.Message) { login := &protocol.Login{ Name: proto.String(name), Authtoken: proto.String(authToken), Permissions: proto.Uint32(permissions), } return &protocol.Message{ Login: login, Type: protocol.NewMessage_Type(protocol.Message_LOGIN), } }
func RunServiceCheck(cmd, env []string, host, service string, shell bool, c chan *CheckResult) { var result *CheckResult if shell { cmd = append([]string{"/bin/sh", "-c"}, cmd...) } /* log.Printf("running cmd %v", cmd)*/ result = runPlugin(cmd, nil, *flagCmdTimeout*1e9) result.Hostname = proto.String(host) result.ServiceName = proto.String(service) /* log.Printf("check returned! %s", proto.CompactTextString(result))*/ c <- result }
// toProto converts the query to a protocol buffer. func (q *Query) toProto(dst *pb.Query, appID string, zlp zeroLimitPolicy) error { if q.kind == "" { return errors.New("datastore: empty query kind") } dst.Reset() dst.App = proto.String(appID) dst.Kind = proto.String(q.kind) if q.ancestor != nil { dst.Ancestor = keyToProto(appID, q.ancestor) } if q.keysOnly { dst.KeysOnly = proto.Bool(true) dst.RequirePerfectPlan = proto.Bool(true) } for _, qf := range q.filter { if qf.FieldName == "" { return errors.New("datastore: empty query filter field name") } p, errStr := valueToProto(appID, qf.FieldName, reflect.ValueOf(qf.Value), false) if errStr != "" { return errors.New("datastore: bad query filter value type: " + errStr) } xf := &pb.Query_Filter{ Op: operatorToProto[qf.Op], Property: []*pb.Property{p}, } if xf.Op == nil { return errors.New("datastore: unknown query filter operator") } dst.Filter = append(dst.Filter, xf) } for _, qo := range q.order { if qo.FieldName == "" { return errors.New("datastore: empty query order field name") } xo := &pb.Query_Order{ Property: proto.String(qo.FieldName), Direction: sortDirectionToProto[qo.Direction], } if xo.Direction == nil { return errors.New("datastore: unknown query order direction") } dst.Order = append(dst.Order, xo) } if q.limit != 0 || zlp == zeroLimitMeansZero { dst.Limit = proto.Int32(q.limit) } if q.offset != 0 { dst.Offset = proto.Int32(q.offset) } return nil }
// valueToProto converts a named value to a newly allocated Property. // The returned error string is empty on success. func valueToProto(defaultAppID, name string, v reflect.Value, multiple bool) (p *pb.Property, errStr string) { var ( pv pb.PropertyValue unsupported bool ) switch v.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: pv.Int64Value = proto.Int64(v.Int()) case reflect.Bool: pv.BooleanValue = proto.Bool(v.Bool()) case reflect.String: pv.StringValue = proto.String(v.String()) case reflect.Float32, reflect.Float64: pv.DoubleValue = proto.Float64(v.Float()) case reflect.Ptr: if k, ok := v.Interface().(*Key); ok { if k == nil { return nil, nilKeyErrStr } pv.Referencevalue = keyToReferenceValue(defaultAppID, k) } else { unsupported = true } case reflect.Slice: if b, ok := v.Interface().([]byte); ok { pv.StringValue = proto.String(string(b)) } else { // nvToProto should already catch slice values. // If we get here, we have a slice of slice values. unsupported = true } default: unsupported = true } if unsupported { return nil, "unsupported datastore value type: " + v.Type().String() } p = &pb.Property{ Name: proto.String(name), Value: &pv, Multiple: proto.Bool(multiple), } switch v.Interface().(type) { case []byte: p.Meaning = pb.NewProperty_Meaning(pb.Property_BLOB) case appengine.BlobKey: p.Meaning = pb.NewProperty_Meaning(pb.Property_BLOBKEY) case Time: p.Meaning = pb.NewProperty_Meaning(pb.Property_GD_WHEN) } return p, "" }
func jobParametersFromMap(mapparam map[string]string) (parray []*ProtoJobParameter) { parray = make([]*ProtoJobParameter, len(mapparam)) i := 0 for k, v := range mapparam { arg := new(ProtoJobParameter) arg.Key = proto.String(k) arg.Value = proto.String(v) parray[i] = arg i++ } return parray }
// LoginURLFederated is like LoginURL but accepts a user's OpenID identifier. func LoginURLFederated(c appengine.Context, dest, identity string) (string, os.Error) { req := &pb.CreateLoginURLRequest{ DestinationUrl: proto.String(dest), } if identity != "" { req.FederatedIdentity = proto.String(identity) } res := &pb.CreateLoginURLResponse{} if err := c.Call("user", "CreateLoginURL", req, res); err != nil { return "", err } return *res.LoginUrl, nil }
// chasing a moving target with exec here. currently it's kind of // clumsy to pull out the return code. with luck the target will // move again in a way i like. func runPlugin(cmd, env []string, timeout int64) (result *CheckResult) { var output bytes.Buffer rc := 0 log.Printf("running check %s", cmd) /* c := exec.Command(cmd[0], cmd...)*/ c := exec.Command(cmd[0], cmd[1:]...) c.Stdout = &output starttime := time.Nanoseconds() err := c.Start() if err != nil { log.Fatal("Error running command ", cmd, ": ", err) } defer c.Process.Release() timer := time.AfterFunc(timeout, func() { c.Process.Kill() }) err = c.Wait() timer.Stop() endtime := time.Nanoseconds() /* log.Print(msg)*/ if err != nil { if msg, ok := err.(*os.Waitmsg); ok { rc = msg.ExitStatus() } else { log.Print("Error running command ", cmd, ": ", err) } } result = &CheckResult{ StartTimestamp: proto.Int64(starttime), EndTimestamp: proto.Int64(endtime), Status: NewCheckStatus(CheckStatus(rc)), CheckPassive: proto.Bool(*flagPassive), } switch rc { case 0, 1, 2, 3: // this is ok! log.Printf("%s: returned %s", cmd, CheckStatus_name[int32(rc)]) result.Status = NewCheckStatus(CheckStatus(rc)) result.CheckOutput = proto.String(string(bytes.TrimSpace(output.Bytes()))) break default: // XXX check for timeout/sig9, presently assumed log.Printf("%s: return code %d", cmd, rc) result.Status = NewCheckStatus(CheckStatus_UNKNOWN) result.CheckOutput = proto.String(fmt.Sprintf("UNKNOWN: Command timed out after %d seconds\n", *flagCmdTimeout) + string(bytes.TrimSpace(output.Bytes()))) } return result }
func (r *resolver) resolveMessage(s *scope, d *DescriptorProto) { ms := s.dup() ms.push(d) // Resolve fields. for _, fd := range d.Field { if fd.Type != nil { if *fd.Type != FieldDescriptorProto_TYPE_MESSAGE && *fd.Type != FieldDescriptorProto_TYPE_ENUM { continue } } o := r.resolveName(ms, *fd.TypeName) if o == nil { log.Printf("Failed to resolve name %q", *fd.TypeName) continue } switch ov := o.last().(type) { case *DescriptorProto: fd.Type = NewFieldDescriptorProto_Type(FieldDescriptorProto_TYPE_MESSAGE) case *EnumDescriptorProto: fd.Type = NewFieldDescriptorProto_Type(FieldDescriptorProto_TYPE_ENUM) } //log.Printf("(resolved %q to %q)", *fd.TypeName, o.fullName()) fd.TypeName = proto.String(o.fullName()) } }
// keyToProto converts a *Key to a Reference proto. func keyToProto(defaultAppID string, k *Key) *pb.Reference { appID := k.appID if appID == "" { appID = defaultAppID } n := 0 for i := k; i != nil; i = i.parent { n++ } e := make([]*pb.Path_Element, n) for i := k; i != nil; i = i.parent { n-- e[n] = &pb.Path_Element{ Type: &i.kind, } // At most one of {Name,Id} should be set. // Neither will be set for incomplete keys. if i.stringID != "" { e[n].Name = &i.stringID } else if i.intID != 0 { e[n].Id = &i.intID } } return &pb.Reference{ App: proto.String(appID), Path: &pb.Path{ Element: e, }, } }
// // Get // func (op *TransactionOperation_Get) executeTransaction(t *Transaction, b *TransactionBlock, vs *viewState) (ret *TransactionReturn) { // TODO: Walk! var value *TransactionValue if op.Source.Variable != nil { sourceVar := b.getRealVar(op.Source.Variable.Variable) value = sourceVar.Value } else if op.Source.Object != nil { // make sure we don't use variables anymore for _, ac := range op.Accessors { ac.MakeAbsoluteValue(b) } obj, osErr := vs.getObject(op.Source.Object.Container.Value().(string), op.Source.Object.Key.Value().(string), true) if osErr != nil { return &TransactionReturn{ Error: &TransactionError{ Id: proto.Uint32(0), // TODO: ERRNO Message: proto.String(osErr.String()), }, } } value = interface2value(obj.data) } destVar := b.getRealVar(op.Destination) destVar.Value = value return }
// // Set // func (op *TransactionOperation_Set) executeTransaction(t *Transaction, b *TransactionBlock, vs *viewState) (ret *TransactionReturn) { // TODO: Walk! // set into a variable if op.Destination.Variable != nil { v := b.getRealVar(op.Destination.Variable.Variable) if op.Value.Value != nil { v.Value = op.Value.Value } else { valvar := b.getRealVar(op.Value.Variable) v.Value = valvar.Value } // set into a database object } else if op.Destination.Object != nil { // make sure we don't use variables anymore for _, ac := range op.Accessors { ac.MakeAbsoluteValue(b) } op.Value.MakeAbsoluteValue(b) partial := false // TODO: if walk, partial = true osErr := vs.mutateObject(op, partial) if osErr != nil { return &TransactionReturn{ Error: &TransactionError{ Id: proto.Uint32(0), // TODO: ERRNO Message: proto.String(osErr.String()), }, } } } return }
// keyToProto converts a *Key to a Reference proto. func keyToProto(defaultAppID string, k *Key) *pb.Reference { appID := k.appID if appID == "" { appID = defaultAppID } n := 0 for i := k; i != nil; i = i.parent { n++ } e := make([]*pb.Path_Element, n) for i := k; i != nil; i = i.parent { n-- e[n] = &pb.Path_Element{ Type: &i.kind, } // Both Name and Id are optional proto fields, but the App Server expects // that exactly one of those fields are set. if i.stringID != "" { e[n].Name = &i.stringID } else { e[n].Id = &i.intID } } return &pb.Reference{ App: proto.String(appID), Path: &pb.Path{ Element: e, }, } }
// Reads a single arbitrary type and returns the proper StateValue. func readField(val reflect.Value) *protocol.StateValue { msg := &protocol.StateValue{} switch f := val.(type) { case *reflect.BoolValue: msg.Type = protocol.NewStateValue_Type(protocol.StateValue_BOOL) msg.BoolVal = proto.Bool(f.Get()) case *reflect.IntValue: msg.Type = protocol.NewStateValue_Type(protocol.StateValue_INT) msg.IntVal = proto.Int(int(f.Get())) case *reflect.FloatValue: msg.Type = protocol.NewStateValue_Type(protocol.StateValue_FLOAT) msg.FloatVal = proto.Float32(float32(f.Get())) case *reflect.StringValue: msg.Type = protocol.NewStateValue_Type(protocol.StateValue_STRING) msg.StringVal = proto.String(f.Get()) case *reflect.SliceValue: msg.Type = protocol.NewStateValue_Type(protocol.StateValue_ARRAY) msg.ArrayVal = makeStateValueArray(f, f.Len()) case *reflect.StructValue: msg = readStructField(f) case *reflect.PtrValue: return readField(reflect.Indirect(f)) // Dereference and recurse default: panic("State value not supported: " + val.Type().String()) } return msg }
func init() { ext := &pb.Ext{ Data: proto.String("extension"), } if err := proto.SetExtension(cloneTestMessage, pb.E_Ext_More, ext); err != nil { log.Fatalf("SetExtension: %v", err) } }
func createSubmit(version int64, hash []byte, doc string, docop *ProtocolDocumentOperation) []byte { submit := &ClientSubmitRequest{} delta := &ProtocolWaveletDelta{} delta.Author = proto.String("torben") delta.HashedVersion = &ProtocolHashedVersion{Version: proto.Int64(version), HistoryHash: hash} mut := &ProtocolWaveletOperation_MutateDocument{DocumentId: proto.String(doc), DocumentOperation: docop} op := &ProtocolWaveletOperation{MutateDocument: mut} delta.Operation = [...]*ProtocolWaveletOperation{op}[:] submit.Delta = delta var buffer bytes.Buffer Marshal(submit, &buffer) log.Println(buffer.String()) return buffer.Bytes() }
func (p *parser) readMessage(d *DescriptorProto) *parseError { if err := p.readToken("message"); err != nil { return err } tok := p.next() if tok.err != nil { return tok.err } // TODO: check that the name is acceptable. d.Name = proto.String(tok.value) if err := p.readToken("{"); err != nil { return err } // Parse message fields and other things inside messages. for !p.done { tok := p.next() if tok.err != nil { return tok.err } switch tok.value { case "required", "optional", "repeated": // field p.back() f := new(FieldDescriptorProto) d.Field = append(d.Field, f) if err := p.readField(f); err != nil { return err } case "enum": // nested enum p.back() e := new(EnumDescriptorProto) d.EnumType = append(d.EnumType, e) if err := p.readEnum(e); err != nil { return err } case "message": // nested message p.back() msg := new(DescriptorProto) d.NestedType = append(d.NestedType, msg) if err := p.readMessage(msg); err != nil { return err } // TODO: more message contents case "}": // end of message return nil default: return p.error("unexpected token %q while parsing message", tok.value) } } return p.error("unexpected end while parsing message") }
func (p *parser) readField(f *FieldDescriptorProto) *parseError { tok := p.next() if tok.err != nil { return tok.err } if lab, ok := fieldLabelMap[tok.value]; ok { f.Label = lab } else { return p.error("expected required/optional/repeated, found %q", tok.value) } tok = p.next() if tok.err != nil { return tok.err } if typ, ok := fieldTypeMap[tok.value]; ok { f.Type = typ } else { f.TypeName = proto.String(tok.value) } tok = p.next() if tok.err != nil { return tok.err } // TODO: check field name correctness (character set, etc.) f.Name = proto.String(tok.value) if err := p.readToken("="); err != nil { return err } f.Number = new(int32) if err := p.readTagNumber(f.Number); err != nil { return err } // TODO: default value, options if err := p.readToken(";"); err != nil { return err } return nil }
// LogoutURL returns a URL that, when visited, signs the user out, // then redirects the user to the URL specified by 'dest'. func LogoutURL(c appengine.Context, dest string) (string, os.Error) { req := &pb.CreateLogoutURLRequest{ DestinationUrl: proto.String(dest), } res := &pb.CreateLogoutURLResponse{} if err := c.Call("user", "CreateLogoutURL", req, res); err != nil { return "", err } return *res.LogoutUrl, nil }
// UploadURL creates an upload URL for the form that the user will fill out, // passing the application path to load when the POST of the form is // completed. These URLs expire and should not be reused. func UploadURL(c appengine.Context, successPath string) (*http.URL, os.Error) { req := &pb.CreateUploadURLRequest{ SuccessPath: proto.String(successPath), } res := &pb.CreateUploadURLResponse{} if err := c.Call("blobstore", "CreateUploadURL", req, res); err != nil { return nil, err } return http.ParseURL(*res.Url) }
func main() { m1 := &test.Member{ Name: proto.String("Linus"), Age: proto.Int32(99), } m1.Skills = []test.Skill{test.Skill_ASM, test.Skill_C} m1_data, err := proto.Marshal(m1) if err != nil { log.Fatal("marshaling error: ", err) os.Exit(1) } fmt.Println("m1: ", m1) fmt.Println("m1.Skills: ", m1.Skills) fmt.Println("actual data: ", m1_data) println("=== unmarshalling m1 into m2 ===") m2 := &test.Member{} proto.Unmarshal(m1_data, m2) fmt.Println("m2: ", m2) port := 9090 fmt.Println("=== END EXAMPLES / SERVER STARTING at %d ===", port) service := fmt.Sprintf(":%d", port) tcpAddr, err := net.ResolveTCPAddr("ip4", service) checkError(err) listener, err := net.ListenTCP("tcp", tcpAddr) checkError(err) for { conn, err := listener.Accept() checkError(err) input := make([]byte, 1024) n, err := conn.Read(input) input = input[:n] fmt.Println("input: ", input) club := &test.Club{} err = proto.Unmarshal(input, club) checkError(err) sum := int32(0) fmt.Println("len: ", len(club.Member)) for _, m := range club.Member { sum += *m.Age } avg := float64(sum) / float64(len(club.Member)) fmt.Printf("avg age: %f (sum: %d)\n", avg, sum) reply := &test.Reply{Average: proto.Float64(avg)} out_data, err := proto.Marshal(reply) conn.Write(out_data) // don't care about return value conn.Close() // we're finished with this client } }
func (client *Client) sendChannelTree(channel *Channel) { chanstate := &mumbleproto.ChannelState{ ChannelId: proto.Uint32(uint32(channel.Id)), Name: proto.String(channel.Name), } if channel.parent != nil { chanstate.Parent = proto.Uint32(uint32(channel.parent.Id)) } if channel.HasDescription() { if client.Version >= 0x10202 { chanstate.DescriptionHash = channel.DescriptionBlobHashBytes() } else { buf, err := globalBlobstore.Get(channel.DescriptionBlob) if err != nil { panic("Blobstore error.") } chanstate.Description = proto.String(string(buf)) } } if channel.Temporary { chanstate.Temporary = proto.Bool(true) } chanstate.Position = proto.Int32(int32(channel.Position)) links := []uint32{} for cid, _ := range channel.Links { links = append(links, uint32(cid)) } chanstate.Links = links err := client.sendProtoMessage(MessageChannelState, chanstate) if err != nil { client.Panic(err.String()) } for _, subchannel := range channel.children { client.sendChannelTree(subchannel) } }
func main() { p := example.Person{ Name: proto.String("Taro Yamada"), Age: proto.Int32(8), } pet := example.Pet{Name: proto.String("Mike")} p.Pet = append(p.Pet, &pet) fmt.Println("-- p.String() --") fmt.Println(p.String()) fmt.Println("-- MarshalText --") fmt.Print(PrintToString(&p)) // not compact fmt.Println("-- Marshal --") m, _ := proto.Marshal(&p) fmt.Println(m) fmt.Println("-- CompactTextString --") fmt.Println(proto.CompactTextString(&p)) }
func makeDisconnect(reason int32, reasonString string) (msg *protocol.Message) { disconnect := &protocol.Disconnect{ Reason: protocol.NewDisconnect_Reason(reason), ReasonStr: proto.String(reasonString), } return &protocol.Message{ Disconnect: disconnect, Type: protocol.NewMessage_Type(protocol.Message_DISCONNECT), } }
func makeRemoveEntity(id int32, name string) (msg *protocol.Message) { removeEntity := &protocol.RemoveEntity{ Id: proto.Int32(id), Name: proto.String(name), } return &protocol.Message{ RemoveEntity: removeEntity, Type: protocol.NewMessage_Type(protocol.Message_REMOVEENTITY), } }
func makeAddEntity(id int32, name string) (msg *protocol.Message) { addEntity := &protocol.AddEntity{ Id: proto.Int32(id), Name: proto.String(name), } return &protocol.Message{ AddEntity: addEntity, Type: protocol.NewMessage_Type(protocol.Message_ADDENTITY), } }
func makeUpdateState(id int32, stateId string, value *protocol.StateValue) (msg *protocol.Message) { updateState := &protocol.UpdateState{ Id: proto.Int32(id), StateId: proto.String(stateId), Value: value, } return &protocol.Message{ UpdateState: updateState, Type: protocol.NewMessage_Type(protocol.Message_UPDATESTATE), } }