github.com/google/martian/v3@v3.3.3/querystring/query_string_filter.go (about)

     1  // Copyright 2015 Google Inc. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package querystring
    16  
    17  import (
    18  	"encoding/json"
    19  
    20  	"github.com/google/martian/v3"
    21  	"github.com/google/martian/v3/filter"
    22  	"github.com/google/martian/v3/parse"
    23  )
    24  
    25  var noop = martian.Noop("querystring.Filter")
    26  
    27  func init() {
    28  	parse.Register("querystring.Filter", filterFromJSON)
    29  }
    30  
    31  // Filter runs modifiers iff the request query parameter for name matches value.
    32  type Filter struct {
    33  	*filter.Filter
    34  }
    35  
    36  type filterJSON struct {
    37  	Name         string               `json:"name"`
    38  	Value        string               `json:"value"`
    39  	Modifier     json.RawMessage      `json:"modifier"`
    40  	ElseModifier json.RawMessage      `json:"else"`
    41  	Scope        []parse.ModifierType `json:"scope"`
    42  }
    43  
    44  // NewFilter builds a querystring.Filter that filters on name and optionally
    45  // value.
    46  func NewFilter(name, value string) *Filter {
    47  	m := NewMatcher(name, value)
    48  	f := filter.New()
    49  	f.SetRequestCondition(m)
    50  	f.SetResponseCondition(m)
    51  	return &Filter{f}
    52  }
    53  
    54  // filterFromJSON takes a JSON message and returns a querystring.Filter.
    55  //
    56  // Example JSON:
    57  // {
    58  //   "name": "param",
    59  //   "value": "example",
    60  //   "scope": ["request", "response"],
    61  //   "modifier": { ... }
    62  // }
    63  func filterFromJSON(b []byte) (*parse.Result, error) {
    64  	msg := &filterJSON{}
    65  	if err := json.Unmarshal(b, msg); err != nil {
    66  		return nil, err
    67  	}
    68  
    69  	f := NewFilter(msg.Name, msg.Value)
    70  
    71  	r, err := parse.FromJSON(msg.Modifier)
    72  	if err != nil {
    73  		return nil, err
    74  	}
    75  
    76  	f.RequestWhenTrue(r.RequestModifier())
    77  	f.ResponseWhenTrue(r.ResponseModifier())
    78  
    79  	if len(msg.ElseModifier) > 0 {
    80  		em, err := parse.FromJSON(msg.ElseModifier)
    81  		if err != nil {
    82  			return nil, err
    83  		}
    84  
    85  		if em != nil {
    86  			f.RequestWhenFalse(em.RequestModifier())
    87  			f.ResponseWhenFalse(em.ResponseModifier())
    88  		}
    89  	}
    90  
    91  	return parse.NewResult(f, msg.Scope)
    92  }