本文整理汇总了Golang中github.com/golang/protobuf/proto.MarshalText函数的典型用法代码示例。如果您正苦于以下问题:Golang MarshalText函数的具体用法?Golang MarshalText怎么用?Golang MarshalText使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了MarshalText函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestNew
func TestNew(t *testing.T) {
if _, err := New("fakefile"); err == nil {
t.Errorf("wanted to return error for nonexistent file")
}
testInput := &redditproto.UserAgent{}
if err := proto.UnmarshalText(`
user_agent: "test"
client_id: "id"
client_secret: "secret"
username: "user"
password: "pass"
`, testInput); err != nil {
t.Errorf("failed to build test expectation proto: %v", err)
}
testFile, err := ioutil.TempFile("", "user_agent")
if err != nil {
t.Errorf("failed to make test input file: %v", err)
}
if err := proto.MarshalText(testFile, testInput); err != nil {
t.Errorf("failed to write test input file: %v", err)
}
if _, err := New(testFile.Name()); err != nil {
t.Errorf("error: %v", err)
}
}
示例2: TestLoad
func TestLoad(t *testing.T) {
expected := &redditproto.UserAgent{}
if err := proto.UnmarshalText(`
user_agent: "test"
client_id: "id"
client_secret: "secret"
username: "user"
password: "pass"
`, expected); err != nil {
t.Errorf("failed to build test expectation proto: %v", err)
}
testFile, err := ioutil.TempFile("", "user_agent")
if err != nil {
t.Errorf("failed to make test input file: %v", err)
}
if err := proto.MarshalText(testFile, expected); err != nil {
t.Errorf("failed to write test input file: %v", err)
}
if _, err := loadAgentFile("notarealfile"); err == nil {
t.Errorf("wanted error returned with nonexistent file as input")
}
actual, err := loadAgentFile(testFile.Name())
if err != nil {
t.Errorf("failed: %v", err)
}
if !proto.Equal(expected, actual) {
t.Errorf("got %v; wanted %v", actual, expected)
}
}
示例3: BenchmarkMarshalTextUnbuffered
func BenchmarkMarshalTextUnbuffered(b *testing.B) {
w := ioutil.Discard
m := newTestMessage()
for i := 0; i < b.N; i++ {
proto.MarshalText(w, m)
}
}
示例4: MarshalText
func MarshalText(w io.Writer, m *Manifest) error {
var bm pb.Manifest
for _, resource := range m.Resources {
bm.Resource = append(bm.Resource, toProto(resource))
}
return proto.MarshalText(w, &bm)
}
示例5: BenchmarkMarshalTextBuffered
func BenchmarkMarshalTextBuffered(b *testing.B) {
buf := new(bytes.Buffer)
m := newTestMessage()
for i := 0; i < b.N; i++ {
buf.Reset()
proto.MarshalText(buf, m)
}
}
示例6: dumpWeights
func dumpWeights(w http.ResponseWriter, r *http.Request) {
def := &caffe2.TensorProtos{}
data, err := ioutil.ReadFile("inception_tensors.pb")
if err != nil {
log.Fatal(err)
}
proto.Unmarshal(data, def)
proto.MarshalText(w, def)
}
示例7: dumpNet
func dumpNet(w http.ResponseWriter, r *http.Request) {
def := &caffe2.NetDef{}
data, err := ioutil.ReadFile("inception_net.pb")
if err != nil {
log.Fatal(err)
}
proto.Unmarshal(data, def)
proto.MarshalText(w, def)
}
示例8: TestMarshalTextCustomMessage
func TestMarshalTextCustomMessage(t *testing.T) {
buf := new(bytes.Buffer)
if err := proto.MarshalText(buf, &textMessage{}); err != nil {
t.Fatalf("proto.MarshalText: %v", err)
}
s := buf.String()
if s != "custom" {
t.Errorf("Got %q, expected %q", s, "custom")
}
}
示例9: TestMarshalText
func TestMarshalText(t *testing.T) {
buf := new(bytes.Buffer)
if err := proto.MarshalText(buf, newTestMessage()); err != nil {
t.Fatalf("proto.MarshalText: %v", err)
}
s := buf.String()
if s != text {
t.Errorf("Got:\n===\n%v===\nExpected:\n===\n%v===\n", s, text)
}
}
示例10: TestInitAcls
func TestInitAcls(t *testing.T) {
if _, err := os.Stat("./tmpdir"); os.IsNotExist(err) {
err = os.Mkdir("./tmpdir", 0777)
if err != nil {
t.Fatal(err)
}
}
trustedEntities := TrustedEntities{
TrustedProgramTaoNames: []string{fmt.Sprintf("%v", programName)},
TrustedHostTaoNames: []string{fmt.Sprintf("%v", hostName)},
TrustedMachineInfos: []string{machineName}}
f, err := os.Create("./tmpdir/TrustedEntities")
if err != nil {
t.Fatal(err)
}
err = proto.MarshalText(f, &trustedEntities)
if err != nil {
t.Fatal(err)
}
err = f.Close()
if err != nil {
t.Fatal(err)
}
aclGuardType := "ACLs"
aclGuardPath := "acls"
cfg := tao.DomainConfig{
DomainInfo: &tao.DomainDetails{
GuardType: &aclGuardType},
AclGuardInfo: &tao.ACLGuardDetails{
SignedAclsPath: &aclGuardPath}}
domain, err := tao.CreateDomain(cfg, "./tmpdir/domain", []byte("xxx"))
if err != nil {
t.Fatal(err)
}
err = InitAcls(domain, "./tmpdir/TrustedEntities")
if err != nil {
t.Fatal(err)
}
machinePrin := auth.Prin{
Type: "MachineInfo",
KeyHash: auth.Str(machineName),
}
if !domain.Guard.IsAuthorized(*programName, "Execute", []string{}) ||
!domain.Guard.IsAuthorized(*hostName, "Host", []string{}) ||
!domain.Guard.IsAuthorized(machinePrin, "Root", []string{}) {
t.Fatal("Authorization checks fail")
}
err = os.RemoveAll("./tmpdir")
if err != nil {
t.Fatal(err)
}
}
示例11: TestMarshalTextNil
func TestMarshalTextNil(t *testing.T) {
want := "<nil>"
tests := []proto.Message{nil, (*pb.MyMessage)(nil)}
for i, test := range tests {
buf := new(bytes.Buffer)
if err := proto.MarshalText(buf, test); err != nil {
t.Fatal(err)
}
if got := buf.String(); got != want {
t.Errorf("%d: got %q want %q", i, got, want)
}
}
}
示例12: 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",
},
{
// Some UTF-8.
&pb.Strings{StringField: proto.String("\x00\x01\xff\x81")},
`string_field: "\000\001\377\201"` + "\n",
},
}
for i, tc := range testCases {
var buf bytes.Buffer
if err := proto.MarshalText(&buf, tc.in); err != nil {
t.Errorf("proto.MarsalText: %v", err)
continue
}
s := buf.String()
if s != tc.out {
t.Errorf("#%d: Got:\n%s\nExpected:\n%s\n", i, s, tc.out)
continue
}
// Check round-trip.
pb := new(pb.Strings)
if err := proto.UnmarshalText(s, pb); err != nil {
t.Errorf("#%d: UnmarshalText: %v", i, err)
continue
}
if !proto.Equal(pb, tc.in) {
t.Errorf("#%d: Round-trip failed:\nstart: %v\n end: %v", i, tc.in, pb)
}
}
}
示例13: TestMarshalTextFailing
func TestMarshalTextFailing(t *testing.T) {
// Try lots of different sizes to exercise more error code-paths.
for lim := 0; lim < len(text); lim++ {
buf := new(limitedWriter)
buf.limit = lim
err := proto.MarshalText(buf, newTestMessage())
// We expect a certain error, but also some partial results in the buffer.
if err != outOfSpace {
t.Errorf("Got:\n===\n%v===\nExpected:\n===\n%v===\n", err, outOfSpace)
}
s := buf.b.String()
x := text[:buf.limit]
if s != x {
t.Errorf("Got:\n===\n%v===\nExpected:\n===\n%v===\n", s, x)
}
}
}
示例14: modifySolverNetSpec
func modifySolverNetSpec(sourceBytes []byte) ([]byte, error) {
// read into object with protobuf (must have already generated go protobuf code)
netParam := &caffe.NetParameter{}
if err := proto.UnmarshalText(string(sourceBytes), netParam); err != nil {
return nil, err
}
// modify object fields
for _, layerParam := range netParam.Layers {
switch *layerParam.Type {
case caffe.V1LayerParameter_IMAGE_DATA:
if isTrainingPhase(layerParam) {
layerParam.ImageDataParam.Source = proto.String(TRAINING_INDEX)
}
if isTestingPhase(layerParam) {
layerParam.ImageDataParam.Source = proto.String(TESTING_INDEX)
}
case caffe.V1LayerParameter_DATA:
if isTrainingPhase(layerParam) {
layerParam.DataParam.Source = proto.String(TRAINING_DIR)
}
if isTestingPhase(layerParam) {
layerParam.DataParam.Source = proto.String(TESTING_DIR)
}
}
}
buf := new(bytes.Buffer)
if err := proto.MarshalText(buf, netParam); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
示例15: modifySolverSpec
func modifySolverSpec(source []byte) ([]byte, error) {
// read into object with protobuf (must have already generated go protobuf code)
solverParam := &caffe.SolverParameter{}
if err := proto.UnmarshalText(string(source), solverParam); err != nil {
return nil, err
}
// modify object fields
solverParam.Net = proto.String("solver-net.prototxt")
solverParam.SnapshotPrefix = proto.String("snapshot")
buf := new(bytes.Buffer)
if err := proto.MarshalText(buf, solverParam); err != nil {
return nil, err
}
return buf.Bytes(), nil
}