urn.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package urn
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strings"
  6. )
  7. const errInvalidURN = "invalid URN: %s"
  8. // URN represents an Uniform Resource Name.
  9. //
  10. // The general form represented is:
  11. //
  12. // urn:<id>:<ss>
  13. //
  14. // Details at https://tools.ietf.org/html/rfc2141.
  15. type URN struct {
  16. prefix string // Static prefix. Equal to "urn" when empty.
  17. ID string // Namespace identifier
  18. SS string // Namespace specific string
  19. norm string // Normalized namespace specific string
  20. }
  21. // Normalize turns the receiving URN into its norm version.
  22. //
  23. // Which means: lowercase prefix, lowercase namespace identifier, and immutate namespace specific string chars (except <hex> tokens which are lowercased).
  24. func (u *URN) Normalize() *URN {
  25. return &URN{
  26. prefix: "urn",
  27. ID: strings.ToLower(u.ID),
  28. SS: u.norm,
  29. }
  30. }
  31. // Equal checks the lexical equivalence of the current URN with another one.
  32. func (u *URN) Equal(x *URN) bool {
  33. return *u.Normalize() == *x.Normalize()
  34. }
  35. // String reassembles the URN into a valid URN string.
  36. //
  37. // This requires both ID and SS fields to be non-empty.
  38. // Otherwise it returns an empty string.
  39. //
  40. // Default URN prefix is "urn".
  41. func (u *URN) String() string {
  42. var res string
  43. if u.ID != "" && u.SS != "" {
  44. if u.prefix == "" {
  45. res += "urn"
  46. }
  47. res += u.prefix + ":" + u.ID + ":" + u.SS
  48. }
  49. return res
  50. }
  51. // Parse is responsible to create an URN instance from a byte array matching the correct URN syntax.
  52. func Parse(u []byte) (*URN, bool) {
  53. urn, err := NewMachine().Parse(u)
  54. if err != nil {
  55. return nil, false
  56. }
  57. return urn, true
  58. }
  59. // MarshalJSON marshals the URN to JSON string form (e.g. `"urn:oid:1.2.3.4"`).
  60. func (u URN) MarshalJSON() ([]byte, error) {
  61. return json.Marshal(u.String())
  62. }
  63. // MarshalJSON unmarshals a URN from JSON string form (e.g. `"urn:oid:1.2.3.4"`).
  64. func (u *URN) UnmarshalJSON(bytes []byte) error {
  65. var str string
  66. if err := json.Unmarshal(bytes, &str); err != nil {
  67. return err
  68. }
  69. if value, ok := Parse([]byte(str)); !ok {
  70. return fmt.Errorf(errInvalidURN, str)
  71. } else {
  72. *u = *value
  73. }
  74. return nil
  75. }