Skip to content

Commit 7e75073

Browse files
authored
adding a noop/noop modifier with a json API (#319)
* Adding a noop modifier in its own noop package
1 parent 19163e1 commit 7e75073

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ require (
88
golang.org/x/net v0.0.0-20190628185345-da137c7871d7
99
google.golang.org/grpc v1.37.0
1010
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0 // indirect
11+
google.golang.org/protobuf v1.26.0 // indirect
1112
)

har/har.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ type contentJSON struct {
307307
Encoding string `json:"encoding,omitempty"`
308308
}
309309

310+
// MarshalJSON marshals the byte slice into json after encoding based on c.Encoding.
310311
func (c Content) MarshalJSON() ([]byte, error) {
311312
var txt string
312313
switch c.Encoding {
@@ -327,6 +328,7 @@ func (c Content) MarshalJSON() ([]byte, error) {
327328
return json.Marshal(cj)
328329
}
329330

331+
// UnmarshalJSON unmarshals the bytes slice into Content.
330332
func (c *Content) UnmarshalJSON(data []byte) error {
331333
var cj contentJSON
332334
if err := json.Unmarshal(data, &cj); err != nil {

noop/noop.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright 2021 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 noop provides a martian.RequestResponseModifier that does not
16+
// modify the request or response.
17+
package noop
18+
19+
import (
20+
"encoding/json"
21+
"net/http"
22+
23+
"github.com/google/martian/v3"
24+
"github.com/google/martian/v3/log"
25+
"github.com/google/martian/v3/parse"
26+
)
27+
28+
func init() {
29+
parse.Register("noop.Modifier", modifierFromJSON)
30+
}
31+
32+
type noopModifier struct {
33+
id string
34+
}
35+
36+
// Noop returns a modifier that does not change the request or the response.
37+
func Noop(id string) martian.RequestResponseModifier {
38+
return &noopModifier{
39+
id: id,
40+
}
41+
}
42+
43+
// ModifyRequest logs a debug line.
44+
func (nm *noopModifier) ModifyRequest(*http.Request) error {
45+
log.Debugf("noopModifier: %s: no request modification applied", nm.id)
46+
return nil
47+
}
48+
49+
// ModifyResponse logs a debug line.
50+
func (nm *noopModifier) ModifyResponse(*http.Response) error {
51+
log.Debugf("noopModifier: %s: no response modification applied", nm.id)
52+
return nil
53+
}
54+
55+
type modifierJSON struct {
56+
Name string `json:"name"`
57+
Scope []parse.ModifierType `json:"scope"`
58+
}
59+
60+
// modifierFromJSON takes a JSON message as a byte slice and returns
61+
// a headerModifier and an error.
62+
//
63+
// Example JSON configuration message:
64+
// {
65+
// "scope": ["request", "result"],
66+
// "name": "noop-name",
67+
// }
68+
func modifierFromJSON(b []byte) (*parse.Result, error) {
69+
msg := &modifierJSON{}
70+
if err := json.Unmarshal(b, msg); err != nil {
71+
return nil, err
72+
}
73+
74+
modifier := Noop(msg.Name)
75+
76+
return parse.NewResult(modifier, msg.Scope)
77+
}
78+

0 commit comments

Comments
 (0)