示例#1
0
//FieldNamesToNumbers rewrites field names contained in the grammar to their respective field numbers found in the protocol buffer filedescriptorset.
//This allows for more speedy field comparisons in validation when used in conjunction with the ProtoNumParser.
func FieldNamesToNumbers(pkgName, msgName string, desc *descriptor.FileDescriptorSet, grammar *ast.Grammar) (*ast.Grammar, error) {
	g := grammar.Clone()
	descMap, err := proto.NewDescriptorMap(pkgName, msgName, desc)
	if err != nil {
		return nil, err
	}
	root := descMap.GetRoot()
	refs := ast.NewRefLookup(g)
	nameToNumber := &nameToNumber{
		refs:    make(map[string]*context),
		descMap: descMap,
	}
	nameToNumber.refs["main"] = &context{root, false}
	newRefs := make(map[string]*ast.Pattern)
	oldContexts := 0
	newContexts := 1
	for oldContexts != newContexts {
		oldContexts = newContexts
		for name, context := range nameToNumber.refs {
			newp, err := nameToNumber.translate(context, refs[name])
			if err != nil {
				return nil, err
			}
			newRefs[name] = newp
		}
		newContexts = len(nameToNumber.refs)
	}
	return ast.NewGrammar(newRefs), nil
}
示例#2
0
文件: proto.go 项目: katydid/katydid
//NewEncoder returns an Encoder that can marshal a parser.Interface into the specified protocol buffer message.
func NewEncoder(desc *descriptor.FileDescriptorSet, pkgName, msgName string) (Encoder, error) {
	descMap, err := protoparser.NewDescriptorMap(pkgName, msgName, desc)
	if err != nil {
		return nil, err
	}
	return &encoder{msg: newMsg(descMap, descMap.GetRoot())}, nil
}