Esempio n. 1
0
func (me *PkgBag) rewriteTypeSpec(typeSpec string) (tn string) {
	tn = ustr.Replace(typeSpec, typeRenderRepls)
	if dt := me.declTypes[tn]; (dt != nil) && (len(dt.EquivalentTo) > 0) {
		tn = strings.Replace(typeSpec, tn, dt.EquivalentTo, -1)
	} else {
		tn = typeSpec
	}
	return
}
Esempio n. 2
0
func (me *PkgBag) checkType(typeSpec string) {
	var dt *declType
	tn := ustr.Replace(typeSpec, typeRenderRepls)
	if dt = me.declTypes[tn]; (dt != nil) && (len(dt.EquivalentTo) > 0) {
		tn = dt.EquivalentTo
		dt = me.declTypes[tn]
	}
	if pos := strings.Index(tn, "."); pos > 0 {
		me.impsUsed[tn[:pos]] = true
	}
	if dt != nil {
		dt.render(me)
	} // else if (tn != "string") && (tn != "bool") && (len(tn) > 0) && !strings.Contains(tn, ".") { println("TYPE NOT FOUND: " + tn) }
}
Esempio n. 3
0
func newPkgBag(schema *Schema) (bag *PkgBag) {
	var newImpname = true
	bag = &PkgBag{Schema: schema}
	bag.impName = "xsdt"
	for i := 0; newImpname; i++ {
		newImpname = false
		for _, s := range schema.allSchemas() {
			for ns, _ := range s.XMLNamespaces {
				if ns == bag.impName {
					newImpname = true
					break
				}
			}
		}
		if newImpname {
			bag.impName = sfmt("xsdt", i)
		}
	}
	bag.imports, bag.impsUsed, bag.lines = map[string]string{}, map[string]bool{}, []string{"//\tAuto-generated by the \"go-xsd\" package located at:", "//\t\tgithub.com/metaleap/go-xsd", "//\tComments on types and fields (if any) are from the XSD file located at:", "//\t\t" + bag.Schema.loadUri, "package go_" + bag.safeName(ustr.Replace(path.Base(bag.Schema.RootSchema().loadUri), map[string]string{"xsd": "", "schema": ""})), ""}
	bag.imports[bag.impName] = "github.com/metaleap/go-xsd/types"
	bag.anonCounts, bag.declTypes, bag.declElemTypes = map[string]uint64{}, map[string]*declType{}, map[element][]*declType{}
	bag.simpleContentValueTypes, bag.attsCache, bag.elemsCacheOnce, bag.elemsCacheMult = map[string]string{}, map[string]string{}, map[string]string{}, map[string]string{}
	bag.attGroups, bag.attGroupRefImps = map[*AttributeGroup]string{}, map[*AttributeGroup]string{}
	bag.attsKeys, bag.attRefImps = map[*Attribute]string{}, map[*Attribute]string{}
	bag.elemGroups, bag.elemGroupRefImps = map[*Group]string{}, map[*Group]string{}
	bag.elemKeys, bag.elemRefImps = map[*Element]string{}, map[*Element]string{}
	bag.elemsWritten, bag.parseTypes, bag.walkerTypes, bag.declConvs = map[string]bool{}, map[string]bool{}, map[string]bool{}, map[string]bool{}
	for _, pt := range []string{"Boolean", "Byte", "Double", "Float", "Int", "Integer", "Long", "NegativeInteger", "NonNegativeInteger", "NonPositiveInteger", "PositiveInteger", "Short", "UnsignedByte", "UnsignedInt", "UnsignedLong", "UnsignedShort"} {
		bag.parseTypes[bag.impName+"."+pt] = true
	}
	bag.addType(nil, idPrefix+"HasCdata", "").addField(nil, idPrefix+"CDATA", "string", ",chardata")
	return
}
Esempio n. 4
0
func (me *declType) render(bag *PkgBag) {
	if !me.rendered {
		me.rendered = true
		if me.checkForEquivalents(bag); len(me.EquivalentTo) == 0 {
			var myName = me.Name
			for _, ann := range me.Annotations {
				if ann != nil {
					ann.makePkg(bag)
				}
			}
			for _, e := range me.Embeds {
				bag.checkType(e.Name)
			}
			for _, f := range me.Fields {
				bag.checkType(f.Type)
			}
			for _, m := range me.Methods {
				bag.checkType(m.ReturnType)
			}
			if len(me.Type) > 0 {
				bag.checkType(me.Type)
				bag.appendFmt(true, "type %s %s", myName, me.Type)
			} else {
				bag.appendFmt(false, "type %s struct {", myName)
				for _, f := range me.Fields {
					f.render(bag, me)
				}
				for _, e := range me.Embeds {
					e.render(bag, me)
				}
				bag.appendFmt(true, "}")
				if PkgGen.AddWalkers && !strings.HasPrefix(myName, idPrefix+"HasAtt") {
					errCheck := sfmt("%s.OnWalkError(&err, &WalkErrors, WalkContinueOnError, WalkOnError) { return }", bag.impName)
					fnCall := "\t\tif fn != nil { if err = fn(me, %v); %s }"
					walkBody := sfmt("\n\tif fn := WalkHandlers.%s; me != nil {\n%s\n", myName, sfmt(fnCall, true, errCheck))
					ec, fc := 0, 0
					bag.walkerTypes[myName] = true
					for _, e := range me.Embeds {
						if bag.walkerTypes[e.finalTypeName] {
							ec++
							walkBody += sfmt("\t\tif err = me.%s.Walk(); %s\n", e.finalTypeName, errCheck)
						}
					}
					for _, f := range me.Fields {
						if bag.walkerTypes[strings.Replace(f.finalTypeName, "*", "", -1)] {
							fc++
							walkBody += sfmt("\t\tif err = me.%v.Walk(); %s\n", f.Name, errCheck)
						} else if strings.HasPrefix(f.finalTypeName, "[]") && bag.walkerTypes[ustr.Replace(f.finalTypeName, typeRenderRepls)] {
							walkBody += sfmt("\t\tfor _, x := range me.%s { if err = x.Walk(); %s }\n", f.Name, errCheck)
						}
					}
					walkBody += sfmt("%s\n}\n\treturn\n", sfmt(fnCall, false, errCheck))
					me.addMethod(nil, "*"+myName, "Walk", "(err error)", walkBody, sfmt("If the WalkHandlers.%v function is not nil (ie. was set by outside code), calls it with this %v instance as the single argument. Then calls the Walk() method on %v/%v embed(s) and %v/%v field(s) belonging to this %v instance.", myName, myName, ec, len(me.Embeds), fc, len(me.Fields), myName))
				}
			}
			bag.declWrittenTypes = append(bag.declWrittenTypes, me)
			for _, m := range me.Methods {
				m.render(bag, me)
			}
		}
	}
}