-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
file_input add mapstructure support (#25)
* file_input add mapstructure support Co-authored-by: Daniel Jaglowski <[email protected]>
- Loading branch information
1 parent
fa642e3
commit c20a108
Showing
11 changed files
with
202 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package helper | ||
|
||
import ( | ||
"encoding/json" | ||
"reflect" | ||
|
||
"github.com/mitchellh/mapstructure" | ||
) | ||
|
||
// make mapstructure use struct UnmarshalJSON to decode | ||
func JSONUnmarshalerHook() mapstructure.DecodeHookFunc { | ||
return func(from reflect.Value, to reflect.Value) (interface{}, error) { | ||
if to.CanAddr() { | ||
to = to.Addr() | ||
} | ||
|
||
// If the destination implements the unmarshaling interface | ||
u, ok := to.Interface().(json.Unmarshaler) | ||
if !ok { | ||
return from.Interface(), nil | ||
} | ||
|
||
// If it is nil and a pointer, create and assign the target value first | ||
if to.IsNil() && to.Type().Kind() == reflect.Ptr { | ||
to.Set(reflect.New(to.Type().Elem())) | ||
u = to.Interface().(json.Unmarshaler) | ||
} | ||
v := from.Interface() | ||
bytes, err := json.Marshal(v) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if err := u.UnmarshalJSON(bytes); err != nil { | ||
return to.Interface(), err | ||
} | ||
return to.Interface(), nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters