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) }
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 }
// 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) } }
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() } }
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") } }
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 }
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) } }
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) }
// 获取一个字段的扩展信息 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 }
func TestLoad(t *testing.T) { expected := &redditproto.UserAgent{} if err := proto.UnmarshalText(` user_agent: "test" client_id: "id" client_secret: "secret" username: "******" password: "******" `, 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) } }
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 }
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: "******" password: "******" `, 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) } }
//build frontend config func mustFEFromText(textcf string) *config.HttpFrontend { cf := &config.HttpFrontend{} err := proto.UnmarshalText(textcf, cf) if err != nil { panic(err) } return cf }
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 }
// 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 }
// 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) }
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 }
func LoadPBTFile(filename string, msg proto.Message) error { content, err := ioutil.ReadFile(filename) if err != nil { return err } return proto.UnmarshalText(string(content), msg) }
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) } }
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) } } }
func mustLoad(filename string) *FileDescriptorSet { buf, err := ioutil.ReadFile(filename) if err != nil { log.Fatalf("Failed reading %v: %v", filename, err) } fds := new(FileDescriptorSet) if err := proto.UnmarshalText(string(buf), fds); err != nil { log.Fatalf("Failed parsing %v: %v", filename, err) } return fds }
func readConfig(configPath string, msg proto.Message) { protoBytes, err := ioutil.ReadFile(configPath) if err != nil { glog.Fatalf("Could not read config at path %v: %v", configPath, err) } err = proto.UnmarshalText(string(protoBytes), msg) if err != nil { glog.Fatalf("Could not parse config at path %v: %v", configPath, err) } }
func configureFromFile() *tao.LinuxHostConfig { d, err := ioutil.ReadFile(hostConfigPath()) if err != nil { options.Fail(err, "Can't read linux host configuration") } var cfg tao.LinuxHostConfig if err := proto.UnmarshalText(string(d), &cfg); err != nil { options.Fail(err, "Can't parse linux host configuration") } return &cfg }
func LoadFromString(configStr string) (Config, error) { configProto := pb.AlertManagerConfig{} if err := proto.UnmarshalText(configStr, &configProto); err != nil { return Config{}, err } config := Config{AlertManagerConfig: configProto} err := config.Validate() return config, err }
func main() { var secrets = make(map[string][]byte) var secret *doorky.DoorSecret var ts *doorky.Timeseries var wapi *WriteAPI var sapi *SpaceAPI var config doorky.DoorkyConfig var configPath string var configData []byte var bindAddress string var err error flag.StringVar(&configPath, "config", "Path to the configuration file", "") flag.StringVar(&bindAddress, "bind", "host:port pair to bind the HTTP server to", ":8080") flag.Parse() configData, err = ioutil.ReadFile(configPath) if err != nil { log.Fatal("Error reading config file ", configPath, ": ", err) } err = proto.UnmarshalText(string(configData), &config) if err != nil { log.Fatal("Error reading config file: ", err) } for _, secret = range config.GetSecret() { var secretInfo = sha256.Sum256([]byte(secret.GetSecret())) secrets[secret.GetName()] = secretInfo[:] } ts, err = doorky.NewTimeseries(config.GetDbServer(), time.Duration(config.GetDbTimeout())*time.Millisecond, config.GetDbCredentials(), config.GetKeyspace()) if err != nil { log.Fatal("Error connecting to timeseries database: ", err) } wapi = NewWriteAPI(ts, secrets) http.Handle("/api/doorstatus", wapi) if config.SpaceapiMd != nil { sapi = NewSpaceAPI(ts, &config) http.Handle("/api/spaceapi", sapi) } err = http.ListenAndServe(bindAddress, nil) if err != nil { log.Fatal("Error listening to ", bindAddress, ": ", err) } }
func readAndParsePolicy() *oproto.RetentionPolicyItem { item := &oproto.RetentionPolicyItem{} policyTxt, err := ioutil.ReadAll(os.Stdin) if err != nil { log.Fatalf("Error reading stdin: %s", err) } if err := proto.UnmarshalText(string(policyTxt), item); err != nil { log.Fatalf("Invalid policy text: %s", err) } return item }
func readHealthcheck(f string) (*pb.Healthcheck, error) { p := &pb.Healthcheck{} b, err := ioutil.ReadFile(f) if err != nil { return nil, err } if err = proto.UnmarshalText(string(b), p); err != nil { return nil, err } proto.SetDefaults(p) return p, nil }
// readConfig reads and parses recipe config at cfgPath. func readConfig(cfgPath string) (*recipe_engine.Package, error) { contents, err := ioutil.ReadFile(cfgPath) if err != nil { return nil, fmt.Errorf("cannot read %q: %s", cfgPath, err) } var pkg recipe_engine.Package if err := proto.UnmarshalText(string(contents), &pkg); err != nil { return nil, fmt.Errorf("cannot parse %q: %s", cfgPath, err) } return &pkg, nil }
func getHeader(sheet *xlsx.Sheet) *tool.ExportHeader { headerString := sheet.Cell(0, 0).Value var header tool.ExportHeader if err := proto.UnmarshalText(headerString, &header); err != nil { return nil } return &header }
func FromText(textcf string) (*Config, error) { cf := new(Config) err := proto.UnmarshalText(textcf, cf) if err != nil { return nil, err } err = Validate(cf) if err != nil { return nil, err } return cf, nil }