equals.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright (C) 2016 Kohei YOSHIDA. All rights reserved.
  2. //
  3. // This program is free software; you can redistribute it and/or
  4. // modify it under the terms of The BSD 3-Clause License
  5. // that can be found in the LICENSE file.
  6. package uritemplate
  7. type CompareFlags uint8
  8. const (
  9. CompareVarname CompareFlags = 1 << iota
  10. )
  11. // Equals reports whether or not two URI Templates t1 and t2 are equivalent.
  12. func Equals(t1 *Template, t2 *Template, flags CompareFlags) bool {
  13. if len(t1.exprs) != len(t2.exprs) {
  14. return false
  15. }
  16. for i := 0; i < len(t1.exprs); i++ {
  17. switch t1 := t1.exprs[i].(type) {
  18. case literals:
  19. t2, ok := t2.exprs[i].(literals)
  20. if !ok {
  21. return false
  22. }
  23. if t1 != t2 {
  24. return false
  25. }
  26. case *expression:
  27. t2, ok := t2.exprs[i].(*expression)
  28. if !ok {
  29. return false
  30. }
  31. if t1.op != t2.op || len(t1.vars) != len(t2.vars) {
  32. return false
  33. }
  34. for n := 0; n < len(t1.vars); n++ {
  35. v1 := t1.vars[n]
  36. v2 := t2.vars[n]
  37. if flags&CompareVarname == CompareVarname && v1.name != v2.name {
  38. return false
  39. }
  40. if v1.maxlen != v2.maxlen || v1.explode != v2.explode {
  41. return false
  42. }
  43. }
  44. default:
  45. panic("unhandled case")
  46. }
  47. }
  48. return true
  49. }