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
7package uritemplate
8
9type CompareFlags uint8
10
11const (
12 CompareVarname CompareFlags = 1 << iota
13)
14
15// Equals reports whether or not two URI Templates t1 and t2 are equivalent.
16func Equals(t1 *Template, t2 *Template, flags CompareFlags) bool {
17 if len(t1.exprs) != len(t2.exprs) {
18 return false
19 }
20 for i := 0; i < len(t1.exprs); i++ {
21 switch t1 := t1.exprs[i].(type) {
22 case literals:
23 t2, ok := t2.exprs[i].(literals)
24 if !ok {
25 return false
26 }
27 if t1 != t2 {
28 return false
29 }
30 case *expression:
31 t2, ok := t2.exprs[i].(*expression)
32 if !ok {
33 return false
34 }
35 if t1.op != t2.op || len(t1.vars) != len(t2.vars) {
36 return false
37 }
38 for n := 0; n < len(t1.vars); n++ {
39 v1 := t1.vars[n]
40 v2 := t2.vars[n]
41 if flags&CompareVarname == CompareVarname && v1.name != v2.name {
42 return false
43 }
44 if v1.maxlen != v2.maxlen || v1.explode != v2.explode {
45 return false
46 }
47 }
48 default:
49 panic("unhandled case")
50 }
51 }
52 return true
53}