| 1 | // Copyright (c) 2018 Ernest Micklei |
| 2 | // |
| 3 | // MIT License |
| 4 | // |
| 5 | // Permission is hereby granted, free of charge, to any person obtaining |
| 6 | // a copy of this software and associated documentation files (the |
| 7 | // "Software"), to deal in the Software without restriction, including |
| 8 | // without limitation the rights to use, copy, modify, merge, publish, |
| 9 | // distribute, sublicense, and/or sell copies of the Software, and to |
| 10 | // permit persons to whom the Software is furnished to do so, subject to |
| 11 | // the following conditions: |
| 12 | // |
| 13 | // The above copyright notice and this permission notice shall be |
| 14 | // included in all copies or substantial portions of the Software. |
| 15 | // |
| 16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 20 | // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 21 | // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 22 | // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 | |
| 24 | package proto |
| 25 | |
| 26 | // Handler is a type of function that accepts a Visitee. |
| 27 | type Handler func(v Visitee) |
| 28 | |
| 29 | // Walk recursively pays a visit to all Visitees of a Proto and calls each handler with it. |
| 30 | func Walk(proto *Proto, handlers ...Handler) { |
| 31 | walk(proto, handlers...) |
| 32 | } |
| 33 | |
| 34 | func walk(container elementContainer, handlers ...Handler) { |
| 35 | for _, eachElement := range container.elements() { |
| 36 | for _, eachFilter := range handlers { |
| 37 | eachFilter(eachElement) |
| 38 | } |
| 39 | if next, ok := eachElement.(elementContainer); ok { |
| 40 | walk(next, handlers...) |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // WithImport returns a Handler that will call the apply function when the Visitee is an Import. |
| 46 | func WithImport(apply func(*Import)) Handler { |
| 47 | return func(v Visitee) { |
| 48 | if s, ok := v.(*Import); ok { |
| 49 | apply(s) |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // WithMessage returns a Handler that will call the apply function when the Visitee is a Message. |
| 55 | func WithMessage(apply func(*Message)) Handler { |
| 56 | return func(v Visitee) { |
| 57 | if s, ok := v.(*Message); ok { |
| 58 | apply(s) |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // WithOption returns a Handler that will call the apply function when the Visitee is a Option. |
| 64 | func WithOption(apply func(*Option)) Handler { |
| 65 | return func(v Visitee) { |
| 66 | if s, ok := v.(*Option); ok { |
| 67 | apply(s) |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // WithEnum returns a Handler that will call the apply function when the Visitee is a Enum. |
| 73 | func WithEnum(apply func(*Enum)) Handler { |
| 74 | return func(v Visitee) { |
| 75 | if s, ok := v.(*Enum); ok { |
| 76 | apply(s) |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // WithOneof returns a Handler that will call the apply function when the Visitee is a Oneof. |
| 82 | func WithOneof(apply func(*Oneof)) Handler { |
| 83 | return func(v Visitee) { |
| 84 | if s, ok := v.(*Oneof); ok { |
| 85 | apply(s) |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // WithService returns a Handler that will call the apply function when the Visitee is a Service. |
| 91 | func WithService(apply func(*Service)) Handler { |
| 92 | return func(v Visitee) { |
| 93 | if s, ok := v.(*Service); ok { |
| 94 | apply(s) |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // WithRPC returns a Handler that will call the apply function when the Visitee is a RPC. |
| 100 | func WithRPC(apply func(*RPC)) Handler { |
| 101 | return func(v Visitee) { |
| 102 | if s, ok := v.(*RPC); ok { |
| 103 | apply(s) |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // WithPackage returns a Handler that will call the apply function when the Visitee is a Package. |
| 109 | func WithPackage(apply func(*Package)) Handler { |
| 110 | return func(v Visitee) { |
| 111 | if s, ok := v.(*Package); ok { |
| 112 | apply(s) |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // WithNormalField returns a Handler that will call the apply function when the Visitee is a NormalField. |
| 118 | func WithNormalField(apply func(*NormalField)) Handler { |
| 119 | return func(v Visitee) { |
| 120 | if s, ok := v.(*NormalField); ok { |
| 121 | apply(s) |
| 122 | } |
| 123 | } |
| 124 | } |