本文整理汇总了Golang中goprotobuf/googlecode/com/hg/proto.String函数的典型用法代码示例。如果您正苦于以下问题:Golang String函数的具体用法?Golang String怎么用?Golang String使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了String函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestPBS
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)
}
示例2: TestStringEscaping
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)
}
}
}
示例3: readEnum
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")
}
示例4: toProto
// 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
}
示例5: makeLogin
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),
}
}
示例6: RunServiceCheck
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
}
示例7: toProto
// 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
}
示例8: valueToProto
// 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, ""
}
示例9: jobParametersFromMap
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
}
示例10: LoginURLFederated
// 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
}
示例11: runPlugin
// 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
}
示例12: resolveMessage
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())
}
}
示例13: keyToProto
// 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,
},
}
}
示例14: executeTransaction
//
// 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
}
示例15: keyToProto
// 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,
},
}
}