Пример #1
0
func BuilderMap(visited map[string]struct{}, fieldname string, repeated bool, msg *descriptor.DescriptorProto, fileDescriptorSet *descriptor.FileDescriptorSet) []string {
	s := []string{`"` + typ(fieldname, repeated, msg) + `": build` + typ(fieldname, repeated, msg) + `(emptyIfNull(null)),`}
	for _, f := range msg.GetField() {
		if !f.IsMessage() {
			continue
		}
		fieldMsg := getMessage(f, fileDescriptorSet)
		if _, ok := visited[msg.GetName()+"."+f.GetName()]; !ok {
			visited[msg.GetName()+"."+f.GetName()] = struct{}{}
			s = append(s, BuilderMap(visited, f.GetName(), f.IsRepeated(), fieldMsg, fileDescriptorSet)...)
		}
	}
	return s
}
Пример #2
0
func Builder(visited map[string]struct{}, root bool, fieldname string, repeated bool, msg *descriptor.DescriptorProto, fileDescriptorSet *descriptor.FileDescriptorSet, proto3 bool, buildField FieldBuilder) string {
	s := []string{`function build` + typ(fieldname, repeated, msg) + `(json) {`}
	if repeated {
		s = append(s, `var s = '<div class="node" type="`+typ(fieldname, repeated, msg)+`" fieldname="`+fieldname+`" repeated="true">';`)
	} else {
		s = append(s, `if (json == undefined) {
		return "";
	}
	`)
		s = append(s, `var s = '<div class="node" type="`+typ(fieldname, repeated, msg)+`" fieldname="`+fieldname+`" repeated="false">';`)
	}
	if !root {
		s = append(s, `s += '<div class="row"><div class="col-sm-2">'`)
		s = append(s, `s += '<a href="#" class="del-child btn btn-danger btn-xs" role="button" fieldname="`+fieldname+`">Remove</a>'`)
		s = append(s, `s += '</div><div class="col-sm-10">'`)
		s = append(s, `s += '<label class="heading">`+fieldname+`</label>'`)
		s = append(s, `s += '</div></div>'`)
	}
	ms := []string{}
	for _, f := range msg.GetField() {
		if f.IsMessage() {
			fieldMsg := getMessage(f, fileDescriptorSet)
			if _, ok := visited[msg.GetName()+"."+f.GetName()]; !ok {
				visited[msg.GetName()+"."+f.GetName()] = struct{}{}
				ms = append(ms, Builder(visited, false, f.GetName(), f.IsRepeated(), fieldMsg, fileDescriptorSet, proto3, buildField))
			}
		}
		s = append(s, buildField(fileDescriptorSet, msg, f, proto3))
	}
	if root {
		s = append(s, `
			s += '</div>';
			var node = $(s);
			activateLinks(node);
			return node;
		}`)
	} else {
		s = append(s, `
		s += '</div>';
		return s;
		}`)
	}
	f := strings.Join(s, "\n")
	ms = append(ms, f)
	return strings.Join(ms, "\n\n")
}
Пример #3
0
func (this *descMap) visit(pkgName string, msg *descriptor.DescriptorProto) error {
	if _, ok := this.msgToField[msg]; ok {
		return nil
	}
	fields := msg.GetField()
	if msg.HasExtension() {
		exts := this.findExts(pkgName, msg.GetName())
		fields = append(fields, exts...)
	}
	for i, f := range fields {
		if _, ok := this.msgToField[msg]; !ok {
			this.msgToField[msg] = make(map[uint64]*descriptor.FieldDescriptorProto)
		}
		this.msgToField[msg][f.GetKeyUint64()] = fields[i]
		if f.GetType() == descriptor.FieldDescriptorProto_TYPE_MESSAGE {
			newPkgName, newMsgName := this.desc.FindMessage(pkgName, msg.GetName(), f.GetName())
			if len(newMsgName) == 0 {
				return &errUnknown{pkgName + "." + msg.GetName() + "." + f.GetName()}
			}
			newMsg := this.desc.GetMessage(newPkgName, newMsgName)
			if newMsg == nil {
				return &errUnknown{newPkgName + "." + newMsgName}
			}
			this.fieldToMsg[fields[i]] = newMsg
			if _, ok := this.msgToField[newMsg]; ok {
				continue
			}
			if err := this.visit(newPkgName, newMsg); err != nil {
				return err
			}
		}
	}
	for i := range msg.GetNestedType() {
		if err := this.visit(pkgName, msg.GetNestedType()[i]); err != nil {
			return err
		}
	}
	return nil
}
Пример #4
0
func typ(fieldname string, repeated bool, msg *descriptor.DescriptorProto) string {
	if repeated {
		return "RepeatedKeyword_" + msg.GetName() + "_" + fieldname
	}
	return msg.GetName() + "_" + fieldname
}