本文整理汇总了Golang中github.com/golang/protobuf/proto.MarshalText函数的典型用法代码示例。如果您正苦于以下问题:Golang MarshalText函数的具体用法?Golang MarshalText怎么用?Golang MarshalText使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了MarshalText函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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)
}
}
开发者ID:tjyang,项目名称:graw,代码行数:29,代码来源:client_test.go
示例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)
}
}
开发者ID:turnage,项目名称:graw,代码行数:34,代码来源:loader_test.go
示例3: BenchmarkMarshalTextUnbuffered
func BenchmarkMarshalTextUnbuffered(b *testing.B) {
w := ioutil.Discard
m := newTestMessage()
for i := 0; i < b.N; i++ {
proto.MarshalText(w, m)
}
}
开发者ID:undernewmanagement,项目名称:besticon,代码行数:7,代码来源:text_test.go
示例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)
}
开发者ID:tonistiigi,项目名称:continuity,代码行数:8,代码来源:manifest.go
示例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)
}
}
开发者ID:undernewmanagement,项目名称:besticon,代码行数:8,代码来源:text_test.go
示例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)
}
开发者ID:nt,项目名称:mv,代码行数:9,代码来源:test.go
示例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)
}
开发者ID:nt,项目名称:mv,代码行数:9,代码来源:test.go
示例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")
}
}
开发者ID:undernewmanagement,项目名称:besticon,代码行数:10,代码来源:text_test.go
示例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)
}
}
开发者ID:undernewmanagement,项目名称:besticon,代码行数:10,代码来源:text_test.go
示例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)
}
}
开发者ID:tmroeder,项目名称:cloudproxy,代码行数:52,代码来源:domain_service_test.go
示例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)
}
}
}
开发者ID:undernewmanagement,项目名称:besticon,代码行数:13,代码来源:text_test.go
示例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)
}
}
}
开发者ID:undernewmanagement,项目名称:besticon,代码行数:46,代码来源:text_test.go
示例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)
}
}
}
开发者ID:undernewmanagement,项目名称:besticon,代码行数:17,代码来源:text_test.go
示例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
}
开发者ID:bryanyzhu,项目名称:elastic-thought,代码行数:43,代码来源:solver.go
示例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
}
开发者ID:bryanyzhu,项目名称:elastic-thought,代码行数:21,代码来源:solver.go
示例16: SafeWriteFile
func SafeWriteFile(filename string, msg proto.Message) error {
f, err := ioutil.TempFile(filepath.Dir(filename), "tmpfile")
if err != nil {
return fmt.Errorf("can't create temporary file: %s", err)
}
w := bufio.NewWriter(f)
if err := proto.MarshalText(w, msg); err != nil {
f.Close()
os.Remove(f.Name())
return fmt.Errorf("marshal failed: %s", f.Name(), err)
}
w.Flush()
f.Sync()
f.Close()
f.Sync()
if err := os.Rename(f.Name(), filename); err != nil {
os.Remove(f.Name())
return fmt.Errorf("rename failed: %s", err)
}
f.Sync()
return nil
}
开发者ID:dparrish,项目名称:openinstrument,代码行数:22,代码来源:openinstrument.go
示例17:
Short: "Dump the contents of the manifest in protobuf text format",
Run: func(cmd *cobra.Command, args []string) {
var p []byte
var err error
if len(args) < 1 {
p, err = ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatalf("error reading manifest: %v", err)
}
} else {
p, err = ioutil.ReadFile(args[0])
if err != nil {
log.Fatalf("error reading manifest: %v", err)
}
}
var bm pb.Manifest
if err := proto.Unmarshal(p, &bm); err != nil {
log.Fatalf("error unmarshaling manifest: %v", err)
}
// TODO(stevvooe): For now, just dump the text format. Turn this into
// nice text output later.
if err := proto.MarshalText(os.Stdout, &bm); err != nil {
log.Fatalf("error dumping manifest: %v", err)
}
},
}
开发者ID:tonistiigi,项目名称:continuity,代码行数:30,代码来源:dump.go
示例18: outputText
func outputText(w http.ResponseWriter, r *resumepb.Resume) error {
w.Header().Add("Content-Type", "text/plain")
return proto.MarshalText(w, r)
}
开发者ID:mconbere,项目名称:goresume,代码行数:4,代码来源:goresume.go
示例19: TestWalkFS
//.........这里部分代码省略.........
{
kind: rhardlink,
path: "a-hardlink",
target: "a",
},
{
kind: rdirectory,
path: "b",
mode: 0755,
},
{
kind: rhardlink,
path: "b/a-hardlink",
target: "a",
},
{
path: "b/a",
mode: 0600 | os.ModeSticky,
},
{
kind: rdirectory,
path: "c",
mode: 0755,
},
{
path: "c/a",
mode: 0644,
},
{
kind: rrelsymlink,
path: "c/ca-relsymlink",
mode: 0600,
target: "a",
},
{
kind: rrelsymlink,
path: "c/a-relsymlink",
mode: 0600,
target: "../a",
},
{
kind: rabssymlink,
path: "c/a-abssymlink",
mode: 0600,
target: "a",
},
// TODO(stevvooe): Make sure we can test this case and get proper
// errors when it is encountered.
// {
// // create a bad symlink and make sure we don't include it.
// kind: relsymlink,
// path: "c/a-badsymlink",
// mode: 0600,
// target: "../../..",
// },
// TODO(stevvooe): Must add tests for xattrs, with symlinks,
// directorys and regular files.
{
kind: rnamedpipe,
path: "fifo",
mode: 0666 | os.ModeNamedPipe,
},
{
kind: rdirectory,
path: "/dev",
mode: 0755,
},
// NOTE(stevvooe): Below here, we add a few simple character devices.
// Block devices are untested but should be nearly the same as
// character devices.
// devNullResource,
// devZeroResource,
}
root, err := ioutil.TempDir("", "continuity-test-")
if err != nil {
t.Fatalf("error creating temporary directory: %v", err)
}
defer os.RemoveAll(root)
generateTestFiles(t, root, testResources)
ctx, err := NewContext(root)
if err != nil {
t.Fatalf("error getting context: %v", err)
}
bm, err := BuildManifest(ctx)
if err != nil {
t.Fatalf("error building manifest: %v, %#T", err, err)
}
proto.MarshalText(os.Stdout, bm)
t.Fail() // TODO(stevvooe): Actually test input/output matches
}
开发者ID:endophage,项目名称:continuity,代码行数:101,代码来源:manifest_test.go
示例20: fuzz
func fuzz(data []byte, text bool) int {
ctors := []func() proto.Message{
func() proto.Message { return new(pb.M0) },
func() proto.Message { return new(pb.M1) },
func() proto.Message { return new(pb.M2) },
func() proto.Message { return new(pb.M3) },
func() proto.Message { return new(pb.M4) },
func() proto.Message { return new(pb.M5) },
func() proto.Message { return new(pb.M6) },
func() proto.Message { return new(pb.M7) },
func() proto.Message { return new(pb.M8) },
func() proto.Message { return new(pb.M9) },
func() proto.Message { return new(pb.M10) },
func() proto.Message { return new(pb.M11) },
func() proto.Message { return new(pb.M12) },
func() proto.Message { return new(pb.M13) },
func() proto.Message { return new(pb.M14) },
func() proto.Message { return new(pb.M15) },
func() proto.Message { return new(pb.M16) },
func() proto.Message { return new(pb.M17) },
func() proto.Message { return new(pb.M18) },
func() proto.Message { return new(pb.M19) },
func() proto.Message { return new(pb.M20) },
func() proto.Message { return new(pb.M21) },
func() proto.Message { return new(pb.M22) },
func() proto.Message { return new(pb.M23) },
func() proto.Message { return new(pb.M24) },
func() proto.Message { return new(pb.M25) },
}
datas := ""
if text {
datas = string(data)
}
score := 0
for _, ctor := range ctors {
v := ctor()
var err error
if text {
err = proto.UnmarshalText(datas, v)
} else {
err = proto.Unmarshal(data, v)
}
if err != nil {
continue
}
score = 1
var data1 []byte
if text {
var buf bytes.Buffer
err = proto.MarshalText(&buf, v)
data1 = buf.Bytes()
} else {
data1, err = proto.Marshal(v)
}
if err != nil {
panic(err)
}
v1 := ctor()
if text {
err = proto.UnmarshalText(string(data1), v1)
} else {
err = proto.Unmarshal(data1, v1)
}
if err != nil {
panic(err)
}
if !DeepEqual(v, v1) {
fmt.Printf("v0: %#v\n", v)
fmt.Printf("v1: %#v\n", v1)
panic(fmt.Sprintf("non idempotent marshal of %T", v))
}
if text {
// TODO: Marshal/Unmarshal to binary.
} else {
var buf bytes.Buffer
err := proto.MarshalText(&buf, v)
if err != nil {
fmt.Printf("failed to MarshalText: %#v\n", v)
panic(err)
}
v2 := ctor()
err = proto.UnmarshalText(buf.String(), v2)
if err != nil {
if strings.Contains(err.Error(), "unexpected byte 0x2f") {
continue // known bug
}
fmt.Printf("failed to UnmarshalText: %q\n", buf.Bytes())
panic(err)
}
if !DeepEqual(v, v2) {
fmt.Printf("v0: %#v\n", v)
fmt.Printf("v2: %#v\n", v2)
panic(fmt.Sprintf("non idempotent text marshal of %T", v))
}
}
}
return score
}
开发者ID:sjn1978,项目名称:go-fuzz,代码行数:98,代码来源:main.go
注:本文中的github.com/golang/protobuf/proto.MarshalText函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论