예제 #1
0
func (me *Attribute) makePkg(bag *PkgBag) {
	var safeName, typeName, tmp, key, defVal, impName string
	var defName = "Default"
	me.elemBase.beforeMakePkg(bag)
	if len(me.Form) == 0 {
		me.Form = bag.Schema.AttributeFormDefault
	}
	me.hasElemsSimpleType.makePkg(bag)
	if len(me.Ref) > 0 {
		key = bag.resolveQnameRef(me.Ref.String(), "", &impName)
		tmp = ustr.PrefixWithSep(impName, ".", idPrefix+"HasAttr_"+bag.safeName(me.Ref.String()[(strings.Index(me.Ref.String(), ":")+1):]))
		if bag.attRefImps[me], bag.attsKeys[me] = impName, key; len(bag.attsCache[key]) == 0 {
			bag.attsCache[key] = tmp
		}
	} else {
		safeName = bag.safeName(me.Name.String())
		if typeName = me.Type.String(); (len(typeName) == 0) && (len(me.SimpleTypes) > 0) {
			typeName = me.SimpleTypes[0].Name.String()
		} else {
			if len(typeName) == 0 {
				typeName = bag.xsdStringTypeRef()
			}
			typeName = bag.resolveQnameRef(typeName, "T", &impName)
		}
		if defVal = me.Default; len(defVal) == 0 {
			defName, defVal = "Fixed", me.Fixed
		}
		if me.Parent() == bag.Schema {
			key = safeName
		} else {
			key = safeName + "_" + bag.safeName(typeName) + "_" + bag.safeName(defVal)
		}
		if len(bag.attsCache[key]) == 0 {
			tmp = idPrefix + "HasAttr_" + key
			bag.attsKeys[me] = key
			bag.attsCache[key] = tmp
			var td = bag.addType(me, tmp, "", me.Annotation)
			td.addField(me, safeName, typeName, util.Ifs(len(bag.Schema.TargetNamespace) > 0, bag.Schema.TargetNamespace.String()+" ", "")+me.Name.String()+",attr", me.Annotation)
			if isPt := bag.isParseType(typeName); len(defVal) > 0 {
				doc := sfmt("Returns the %v value for %v -- "+util.Ifs(isPt, "%v", "%#v"), strings.ToLower(defName), safeName, defVal)
				if isPt {
					if PkgGen.ForceParseForDefaults {
						td.addMethod(nil, tmp, safeName+defName, typeName, sfmt("var x = new(%v); x.SetFromString(%#v); return *x", typeName, defVal), doc)
					} else {
						td.addMethod(nil, tmp, safeName+defName, typeName, sfmt("return %v(%v)", typeName, defVal), doc)
					}
				} else {
					td.addMethod(nil, tmp, safeName+defName, typeName, sfmt("return %v(%#v)", typeName, defVal), doc)
				}
			}
		} else {
			bag.attsKeys[me] = key
		}
	}
	me.elemBase.afterMakePkg(bag)
}
예제 #2
0
func (me *Union) makePkg(bag *PkgBag) {
	var memberTypes []string
	var rtn, rtnSafeName, safeName string
	me.elemBase.beforeMakePkg(bag)
	me.hasElemsSimpleType.makePkg(bag)
	memberTypes = ustr.Split(me.MemberTypes, " ")
	for _, st := range me.SimpleTypes {
		memberTypes = append(memberTypes, st.Name.String())
	}
	for _, mt := range memberTypes {
		rtn = bag.resolveQnameRef(mt, "T", nil)
		safeName, rtnSafeName = bag.safeName(ustr.PrependIf(bag.Stacks.CurSimpleType().Name.String(), "T")), bag.safeName(rtn)
		bag.ctd.addMethod(me, safeName, "To"+rtnSafeName, rtn, sfmt(util.Ifs(bag.isParseType(rtn), "var x = new(%v); x.SetFromString(me.String()); return *x", "return %v(me)"), rtn), sfmt("%v is an XSD union-type of several types. This is a simple type conversion to %v, but keep in mind the actual value may or may not be a valid %v value.", safeName, rtnSafeName, rtnSafeName), me.Annotation)
	}
	me.elemBase.afterMakePkg(bag)
}
예제 #3
0
파일: makepkg.go 프로젝트: cailei/go-xsd
func (me *declMethod) render(bag *PkgBag, dt *declType) {
	for _, ann := range me.Annotations {
		if ann != nil {
			ann.makePkg(bag)
		}
	}
	bag.appendFmt(false, "//\t%s", me.Doc)
	rt := bag.rewriteTypeSpec(me.ReturnType)
	bag.appendFmt(true, "func (me %s) %s %s { %s }", bag.rewriteTypeSpec(me.ReceiverType), util.Ifs(strings.Contains(me.Name, "("), me.Name, me.Name+" ()"), rt, strings.Replace(me.Body, me.ReturnType, rt, -1))
}
예제 #4
0
func (me *Element) makePkg(bag *PkgBag) {
	var (
		safeName, typeName, valueType, tmp, key, defVal, impName string
		subEl                                                    *Element
	)
	asterisk, defName, doc := "", "Default", ""
	me.elemBase.beforeMakePkg(bag)
	if len(me.Form) == 0 {
		me.Form = bag.Schema.ElementFormDefault
	}
	me.hasElemsSimpleType.makePkg(bag)
	me.hasElemComplexType.makePkg(bag)
	if len(me.Ref) > 0 {
		key = bag.resolveQnameRef(me.Ref.String(), "", &impName)
		for pref, cache := range map[string]map[string]string{"HasElem_": bag.elemsCacheOnce, "HasElems_": bag.elemsCacheMult} {
			tmp = ustr.PrefixWithSep(impName, ".", idPrefix+pref+bag.safeName(me.Ref.String()[(strings.Index(me.Ref.String(), ":")+1):]))
			if bag.elemRefImps[me], bag.elemKeys[me] = impName, key; len(cache[key]) == 0 {
				cache[key] = tmp
			}
		}
	} else {
		safeName = bag.safeName(me.Name.String())
		if typeName = me.Type.String(); (len(typeName) == 0) && ((me.ComplexType != nil) || (len(me.SimpleTypes) > 0)) {
			if me.ComplexType != nil {
				asterisk, typeName = "*", me.ComplexType.Name.String()
			} else {
				typeName = me.SimpleTypes[0].Name.String()
			}
		} else {
			if len(typeName) == 0 {
				typeName = bag.xsdStringTypeRef()
			}
			if typeName = bag.resolveQnameRef(typeName, "T", &impName); bag.Schema.RootSchema().globalComplexType(bag, typeName) != nil {
				asterisk = "*"
			}
		}
		if defVal = me.Default; len(defVal) == 0 {
			defName, defVal = "Fixed", me.Fixed
		}
		if me.Parent() == bag.Schema {
			key = safeName
		} else {
			key = bag.Stacks.FullName() + "_" + safeName + "_" + bag.safeName(typeName) + "_" + bag.safeName(defVal)
		}
		if valueType = bag.simpleContentValueTypes[typeName]; len(valueType) == 0 {
			valueType = typeName
		}
		isPt := bag.isParseType(valueType)
		if _, isChoice := me.Parent().(*Choice); isChoice && isPt {
			asterisk = "*"
		}
		for pref, cache := range map[string]map[string]string{"HasElem_": bag.elemsCacheOnce, "HasElems_": bag.elemsCacheMult} {
			if tmp = idPrefix + pref + key; !bag.elemsWritten[tmp] {
				bag.elemsWritten[tmp], bag.elemKeys[me] = true, key
				cache[key] = tmp
				var td = bag.addType(me, tmp, "", me.Annotation)
				td.addField(me, util.Ifs(pref == "HasElems_", pluralize(safeName), safeName), util.Ifs(pref == "HasElems_", "[]"+asterisk+typeName, asterisk+typeName), util.Ifs(len(bag.Schema.TargetNamespace) > 0, bag.Schema.TargetNamespace.String()+" ", "")+me.Name.String(), me.Annotation)
				if me.parent == bag.Schema {
					for _, subEl = range bag.Schema.RootSchema().globalSubstitutionElems(me) {
						td.addEmbed(subEl, idPrefix+pref+bag.safeName(subEl.Name.String()), subEl.Annotation)
					}
				}
				if len(defVal) > 0 {
					doc = sfmt("Returns the %v value for %v -- "+util.Ifs(isPt, "%v", "%#v"), strings.ToLower(defName), safeName, defVal)
					if isPt {
						if PkgGen.ForceParseForDefaults {
							td.addMethod(nil, tmp, safeName+defName, valueType, sfmt("var x = new(%v); x.SetFromString(%#v); return *x", valueType, defVal), doc)
						} else {
							td.addMethod(nil, tmp, safeName+defName, valueType, sfmt("return %v(%v)", valueType, defVal), doc)
						}
					} else {
						td.addMethod(nil, tmp, safeName+defName, valueType, sfmt("return %v(%#v)", valueType, defVal), doc)
					}
				}
			}
		}
	}
	me.elemBase.afterMakePkg(bag)
}