예제 #1
0
func ExampleCompile() {
	a := &test.NinOptNative{
		Field4: proto.Int64(1234),
		Field7: proto.Int32(123),
	}
	fp1, err := fieldpath.NewInt64Path("test", "NinOptNative", test.ThetestDescription(), "Field4")
	if err != nil {
		panic(err)
	}
	fp2, err := fieldpath.NewSint32Path("test", "NinOptNative", test.ThetestDescription(), "Field7")
	if err != nil {
		panic(err)
	}
	buf, err := proto.Marshal(a)
	if err != nil {
		panic(err)
	}
	u1 := fieldpath.NewInt64Unmarshaler(fp1, &handler64{})
	u2 := fieldpath.NewSint32Unmarshaler(fp2, &handler32{})
	c := fieldpath.Compile(u1, u2)
	err = c.Unmarshal(buf)
	if err != nil {
		panic(err)
	}
	// Output:
	// 1234
	// 123
}
예제 #2
0
파일: main.go 프로젝트: raceli/gogoprotobuf
func main() {
	if len(flag.Args()) == 0 {
		er("fieldpath needs to be provided")
	}
	var desc = &descriptor.FileDescriptorSet{}
	if len(descFilename) > 0 {
		data, err := ioutil.ReadFile(descFilename)
		if err != nil {
			er(err.Error())
		}
		if err := proto.Unmarshal(data, desc); err != nil {
			er(fmt.Sprintf("Reading descriptor_set_in filename (%v) : %v", descFilename, err))
		}
	}
	if len(protoFilename) > 0 {
		var err error
		desc, err = parser.ParseFile(protoFilename, strings.Split(protoPath, ":")...)
		if err != nil {
			er(fmt.Sprintf("Parsing proto filename (%v) with proto path (%v) : %v", protoFilename, protoPath, err))
		}
	}
	if desc == nil {
		er("either descriptor_set_in or proto_filename flag needs to be provided")
	}

	us := []*fieldpath.Unmarshaler{}
	printers := []func(v []byte){}
	for _, a := range flag.Args() {
		fieldpaths := strings.Split(a, ".")
		if len(fieldpaths) < 2 {
			er("fieldpath flag needs at least a package.message structure")
		}
		rootPkg := fieldpaths[0]
		rootMsg := fieldpaths[1]
		if len(fieldpaths) >= 3 {
			fpath := strings.Join(fieldpaths[2:], ".")
			if expandFlag {
				var err error
				fpath, err = fieldpath.Expand(rootPkg, rootMsg, fpath, desc)
				if err != nil {
					er(err.Error())
				}
			}
			field, err := fieldpath.NewGenericPath(rootPkg, rootMsg, desc, fpath)
			if err != nil {
				er(err.Error())
			}
			stdout := &ToStdout{}
			stdout.BytesFunc = func(v []byte) {
				os.Stdout.Write([]byte(fmt.Sprintf("%v\n", v)))
			}
			if field.IsMessage() {
				stdout.BytesFunc = func(v []byte) {
					err := fieldpath.ToString(rootPkg, rootMsg, desc, fpath, v, 0, os.Stdout)
					if err != nil {
						er(err.Error())
					}
				}
			}
			u := field.NewUnmarshaler(stdout)
			us = append(us, u)
		} else {
			printers = append(printers, func(v []byte) {
				err := fieldpath.ToString(rootPkg, rootMsg, desc, "", v, 0, os.Stdout)
				if err != nil {
					er(err.Error())
				}
			})
		}

	}

	var r reader
	if inputDelim == "varint" {
		r = newVarintReader(os.Stdin)
	} else if inputDelim == "big32" {
		r = newBig32Reader(os.Stdin)
	} else {
		r = newAllReader(os.Stdin)
	}

	c := fieldpath.Compile(us...)

	for {
		data := r.Read()
		if data == nil {
			return
		}
		for _, p := range printers {
			p(data)
		}
		err := c.Unmarshal(data)
		if err != nil {
			er(err.Error())
		}
		if inputDelim == "none" {
			return
		}
	}

}