// Details at https://tools.ietf.org/html/rfc2141.
typeURNstruct{
prefixstring// Static prefix. Equal to "urn" when empty.
IDstring// Namespace identifier
SSstring// Namespace specific string
normstring// Normalized namespace specific string
}
// Normalize turns the receiving URN into its norm version.
//
// Which means: lowercase prefix, lowercase namespace identifier, and immutate namespace specific string chars (except <hex> tokens which are lowercased).
func(u*URN)Normalize()*URN{
return&URN{
prefix:"urn",
ID:strings.ToLower(u.ID),
SS:u.norm,
}
}
// Equal checks the lexical equivalence of the current URN with another one.
func(u*URN)Equal(x*URN)bool{
return*u.Normalize()==*x.Normalize()
}
// String reassembles the URN into a valid URN string.
//
// This requires both ID and SS fields to be non-empty.
// Otherwise it returns an empty string.
//
// Default URN prefix is "urn".
func(u*URN)String()string{
varresstring
ifu.ID!=""&&u.SS!=""{
ifu.prefix==""{
res+="urn"
}
res+=u.prefix+":"+u.ID+":"+u.SS
}
returnres
}
// Parse is responsible to create an URN instance from a byte array matching the correct URN syntax.
funcParse(u[]byte)(*URN,bool){
urn,err:=NewMachine().Parse(u)
iferr!=nil{
returnnil,false
}
returnurn,true
}
// MarshalJSON marshals the URN to JSON string form (e.g. `"urn:oid:1.2.3.4"`).
func(uURN)MarshalJSON()([]byte,error){
returnjson.Marshal(u.String())
}
// MarshalJSON unmarshals a URN from JSON string form (e.g. `"urn:oid:1.2.3.4"`).