本文整理汇总了Golang中github.com/golang/protobuf/proto.UnmarshalText函数的典型用法代码示例。如果您正苦于以下问题:Golang UnmarshalText函数的具体用法?Golang UnmarshalText怎么用?Golang UnmarshalText使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了UnmarshalText函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: load
func load(t *testing.T, reg *Registry, src string) error {
var req plugin.CodeGeneratorRequest
if err := proto.UnmarshalText(src, &req); err != nil {
t.Fatalf("proto.UnmarshalText(%s, &file) failed with %v; want success", src, err)
}
return reg.Load(&req)
}
开发者ID:JohanSJA,项目名称:grpc-gateway,代码行数:7,代码来源:registry_test.go
示例2: getHeader
func getHeader(sheet *xlsx.Sheet) (*tool.ExportHeaderV1, error) {
headerString := strings.TrimSpace(sheet.Cell(0, 0).Value)
// 可能是空的sheet
if headerString == "" {
return nil, nil
}
var header tool.ExportHeaderV1
// 有可能的字符,一定是头
if strings.Contains(headerString, "ProtoTypeName") ||
strings.Contains(headerString, "RowFieldName") {
if err := proto.UnmarshalText(headerString, &header); err != nil {
return nil, err
}
} else {
// 有字符, 但并不是头
return nil, nil
}
return &header, nil
}
开发者ID:davyxu,项目名称:tabtoy,代码行数:25,代码来源:file.go
示例3: tryParse
// tryParse attempts to parse the input, and verifies that it matches
// the FileDescriptorProto represented in text format.
func tryParse(t *testing.T, input, output string) {
want := new(pb.FileDescriptorProto)
if err := proto.UnmarshalText(output, want); err != nil {
t.Fatalf("Test failure parsing a wanted proto: %v", err)
}
p := newParser("-", input)
f := new(ast.File)
if pe := p.readFile(f); pe != nil {
t.Errorf("Failed parsing input: %v", pe)
return
}
fset := &ast.FileSet{Files: []*ast.File{f}}
if err := resolveSymbols(fset); err != nil {
t.Errorf("Resolving symbols: %v", err)
return
}
fds, err := gendesc.Generate(fset)
if err != nil {
t.Errorf("Generating FileDescriptorSet: %v", err)
return
}
if n := len(fds.File); n != 1 {
t.Errorf("Generated %d FileDescriptorProtos, want 1", n)
return
}
got := fds.File[0]
if !proto.Equal(got, want) {
t.Errorf("Mismatch!\nGot:\n%v\nWant:\n%v", got, want)
}
}
开发者ID:myitcv,项目名称:old_protobuf,代码行数:35,代码来源:parser_test.go
示例4: TestDefaultDropPolicy
func (s *MySuite) TestDefaultDropPolicy(c *C) {
policyTxt := `
interval: 3600
policy {
comment: "Throw everything away"
policy: DROP
}
`
policyProto := &oproto.RetentionPolicy{}
c.Assert(proto.UnmarshalText(policyTxt, policyProto), IsNil)
policy := New(policyProto)
input := &oproto.ValueStream{
Variable: variable.NewFromString("/test/foo/bar").AsProto(),
Value: []*oproto.Value{},
}
for i := 0; i < 10; i++ {
input.Value = append(input.Value, &oproto.Value{Timestamp: uint64(i), DoubleValue: 1.1})
}
output := policy.Apply(input)
for _, value := range output.Value {
log.Printf("Got output when none was expected: %s", value)
c.Fail()
}
}
开发者ID:dparrish,项目名称:openinstrument,代码行数:26,代码来源:policy_test.go
示例5: TestPatch
func TestPatch(t *testing.T) {
f, err := os.Open("Actor.pbt")
if err != nil {
t.Fatalf(err.Error())
return
}
data, err := ioutil.ReadAll(f)
if err != nil {
t.Fatalf(err.Error())
return
}
var file test.ActorFile
err = proto.UnmarshalText(string(data), &file)
if err != nil {
t.Fatalf(err.Error())
return
}
if file.Actor[0].Name != "A" {
t.Fatalf("fail A")
}
}
开发者ID:jwk000,项目名称:tabtoy,代码行数:29,代码来源:patch_test.go
示例6: scrapeMetrics
func scrapeMetrics(s *httptest.Server) ([]*prometheuspb.MetricFamily, error) {
req, err := http.NewRequest("GET", s.URL+"/metrics", nil)
if err != nil {
return nil, fmt.Errorf("Unable to create http request: %v", err)
}
// Ask the prometheus exporter for its text protocol buffer format, since it's
// much easier to parse than its plain-text format. Don't use the serialized
// proto representation since it uses a non-standard varint delimiter between
// metric families.
req.Header.Add("Accept", scrapeRequestHeader)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("Unable to contact metrics endpoint of master: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("Non-200 response trying to scrape metrics from master: %v", resp)
}
// Each line in the response body should contain all the data for a single metric.
var metrics []*prometheuspb.MetricFamily
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
var metric prometheuspb.MetricFamily
if err := proto.UnmarshalText(scanner.Text(), &metric); err != nil {
return nil, fmt.Errorf("Failed to unmarshal line of metrics response: %v", err)
}
glog.Infof("Got metric %q", metric.GetName())
metrics = append(metrics, &metric)
}
return metrics, nil
}
开发者ID:XbinZh,项目名称:kubernetes,代码行数:34,代码来源:metrics_test.go
示例7: main
func main() {
data, err := ioutil.ReadFile("../Config.pbt")
if err != nil {
fmt.Println(err.Error())
return
}
var config gamedef.Config
// 加载配置
err = proto.UnmarshalText(string(data), &config)
if err != nil {
fmt.Println(err.Error())
return
}
// 创建索引
table.MakeIndex(&config)
// 使用被索引的文件
for index, v := range table.SampleByID {
fmt.Println(index, v)
}
}
开发者ID:davyxu,项目名称:tabtoy,代码行数:28,代码来源:main.go
示例8: Test
func Test(t *testing.T) {
data, err := ioutil.ReadFile("Actor.pbt")
if err != nil {
t.Fatalf(err.Error())
return
}
var file test.ActorFile
err = proto.UnmarshalText(string(data), &file)
if err != nil {
t.Fatalf(err.Error())
return
}
if file.Actor[1].Name != "葫芦\n娃" {
t.Fatal("fail 1", file.Actor[1].Name)
}
if file.Actor[2].Name != "舒\"克\"" {
t.Fatal("fail 2", file.Actor[2].Name)
}
outdata, err := json.MarshalIndent(&file, "", " ")
ioutil.WriteFile("Actor_std.json", outdata, 777)
testJson(t, &file)
}
开发者ID:davyxu,项目名称:tabtoy,代码行数:32,代码来源:normal_test.go
示例9: GetFieldMeta
// 获取一个字段的扩展信息
func GetFieldMeta(field interface{}) (*tool.FieldMetaV1, bool) {
var metaStr string
cm := field.(interface {
ParseTaggedComment() []*pbmeta.TaggedComment
})
tc := cm.ParseTaggedComment()
for _, c := range tc {
if c.Name == "@" || c.Name == "tabtoy" {
metaStr = c.Data
break
}
}
var meta tool.FieldMetaV1
if err := proto.UnmarshalText(metaStr, &meta); err != nil {
log.Errorf("parse field meta failed, [%s] %s", metaStr, err.Error())
return nil, false
}
return &meta, true
}
开发者ID:davyxu,项目名称:tabtoy,代码行数:29,代码来源:fieldmeta.go
示例10: 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
示例11: createDomain
func createDomain() (*tao.Domain, *x509.CertPool, error) {
var cfg tao.DomainConfig
d, err := ioutil.ReadFile(*configPath)
if err != nil {
return nil, nil, err
}
if err := proto.UnmarshalText(string(d), &cfg); err != nil {
return nil, nil, err
}
domain, err := tao.CreateDomain(cfg, *configPath, []byte(*domainPass))
if domain == nil {
log.Printf("domainserver: no domain path - %s, pass - %s, err - %s\n",
*configPath, *domainPass, err)
return nil, nil, err
} else if err != nil {
log.Printf("domainserver: Couldn't load the config path %s: %s\n",
*configPath, err)
return nil, nil, err
}
log.Printf("domainserver: Created domain\n")
err = domain_service.InitAcls(domain, *trustedEntitiesPath)
if err != nil {
return nil, nil, err
}
err = domain.Save()
if err != nil {
return nil, nil, err
}
certPool := x509.NewCertPool()
certPool.AddCert(domain.Keys.Cert)
return domain, certPool, nil
}
开发者ID:tmroeder,项目名称:cloudproxy,代码行数:32,代码来源:domain_server.go
示例12: 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
示例13: mustFEFromText
//build frontend config
func mustFEFromText(textcf string) *config.HttpFrontend {
cf := &config.HttpFrontend{}
err := proto.UnmarshalText(textcf, cf)
if err != nil {
panic(err)
}
return cf
}
开发者ID:kzadorozhny,项目名称:backplane,代码行数:9,代码来源:frontend_test.go
示例14: loadFile
func loadFile(t *testing.T, reg *Registry, src string) *descriptor.FileDescriptorProto {
var file descriptor.FileDescriptorProto
if err := proto.UnmarshalText(src, &file); err != nil {
t.Fatalf("proto.UnmarshalText(%s, &file) failed with %v; want success", src, err)
}
reg.loadFile(&file)
return &file
}
开发者ID:JohanSJA,项目名称:grpc-gateway,代码行数:8,代码来源:registry_test.go
示例15: InitAcls
// This function reads in trusted entities from a file at trustedEntitiesPath. In particular,
// this file contains the text representation of a trusted_entities proto message, which contains
// the Tao names of trusted programs and hosts, information about trusted machines and trusted
// machine certificates.
// For each such trusted entity, this function adds ACL rules to the domain guard, and saves
// the changes before returning.
func InitAcls(domain *tao.Domain, trustedEntitiesPath string) error {
text, err := ioutil.ReadFile(trustedEntitiesPath)
if err != nil {
log.Printf("Can't open trusted entities file: %s", trustedEntitiesPath)
return err
}
trustedEntities := TrustedEntities{}
err = proto.UnmarshalText(string(text), &trustedEntities)
if err != nil {
log.Printf("Can't parse trusted entities file: %s", trustedEntitiesPath)
return err
}
for _, programTaoName := range trustedEntities.GetTrustedProgramTaoNames() {
var programPrin auth.Prin
_, err := fmt.Sscanf(programTaoName, "%v", &programPrin)
if err != nil {
log.Printf("Can't create program principal from: %s\nError: %s",
programTaoName, err)
return err
}
err = domain.Guard.Authorize(programPrin, "Execute", []string{})
if err != nil {
log.Printf("Can't authorize principal: %s\nError: %s", programPrin, err)
return err
}
}
for _, hostTaoName := range trustedEntities.GetTrustedHostTaoNames() {
var hostPrin auth.Prin
_, err := fmt.Sscanf(hostTaoName, "%v", &hostPrin)
if err != nil {
log.Printf("Can't create host principal from: %s\nError: %s",
hostTaoName, err)
return err
}
err = domain.Guard.Authorize(hostPrin, "Host", []string{})
if err != nil {
log.Printf("Can't authorize principal: %s\nError: %s", hostPrin, err)
return err
}
}
for _, machineInfo := range trustedEntities.GetTrustedMachineInfos() {
machinePrin := auth.Prin{
Type: "MachineInfo",
KeyHash: auth.Str(machineInfo),
}
err = domain.Guard.Authorize(machinePrin, "Root", []string{})
if err != nil {
log.Printf("Can't authorize principal: %s\nError: %s", machinePrin, err)
return err
}
}
err = domain.Save()
if err != nil {
log.Println("Can't save domain.", err)
}
return err
}
开发者ID:tmroeder,项目名称:cloudproxy,代码行数:63,代码来源:domain_service.go
示例16: Load
// Load reads a user agent from a protobuffer file and returns it.
func Load(filename string) (*UserAgent, error) {
buf, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
agent := &UserAgent{}
return agent, proto.UnmarshalText(bytes.NewBuffer(buf).String(), agent)
}
开发者ID:turnage,项目名称:redditproto,代码行数:10,代码来源:loader.go
示例17: parseExecuteOptions
func parseExecuteOptions(value string) (*querypb.ExecuteOptions, error) {
if value == "" {
return nil, nil
}
result := &querypb.ExecuteOptions{}
if err := proto.UnmarshalText(value, result); err != nil {
return nil, fmt.Errorf("failed to unmarshal options: %v", err)
}
return result, nil
}
开发者ID:dumbunny,项目名称:vitess,代码行数:10,代码来源:query.go
示例18: LoadPBTFile
func LoadPBTFile(filename string, msg proto.Message) error {
content, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
return proto.UnmarshalText(string(content), msg)
}
开发者ID:chogaths,项目名称:robin,代码行数:10,代码来源:config.go
示例19: TestAmbiguousAny
func TestAmbiguousAny(t *testing.T) {
pb := &anypb.Any{}
err := proto.UnmarshalText(`
type_url: "ttt/proto3_proto.Nested"
value: "\n\x05Monty"
`, pb)
t.Logf("result: %v (error: %v)", expandedMarshaler.Text(pb), err)
if err != nil {
t.Errorf("failed to parse ambiguous Any message: %v", err)
}
}
开发者ID:Rudloff,项目名称:platform,代码行数:11,代码来源:any_test.go
示例20: TestUnmarshalGolden
func TestUnmarshalGolden(t *testing.T) {
for _, tt := range goldenMessages {
want := tt.m
got := proto.Clone(tt.m)
got.Reset()
if err := proto.UnmarshalText(tt.t, got); err != nil {
t.Errorf("failed to unmarshal\n%s\nerror: %v", tt.t, err)
}
if !anyEqual(got, want) {
t.Errorf("message:\n%s\ngot:\n%s\nwant:\n%s", tt.t, got, want)
}
got.Reset()
if err := proto.UnmarshalText(tt.c, got); err != nil {
t.Errorf("failed to unmarshal\n%s\nerror: %v", tt.c, err)
}
if !anyEqual(got, want) {
t.Errorf("message:\n%s\ngot:\n%s\nwant:\n%s", tt.c, got, want)
}
}
}
开发者ID:Rudloff,项目名称:platform,代码行数:20,代码来源:any_test.go
注:本文中的github.com/golang/protobuf/proto.UnmarshalText函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论