-
-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: work around invalid all_time_since_today data schema to fix fail…
…ing import (resolve #370)
- Loading branch information
Showing
2 changed files
with
38 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package utils | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"io" | ||
) | ||
|
||
// ParseJsonDropKeys parses the given JSON input object to an object of given type, while omitting the specified keys on the way. | ||
// This can be useful if parsing would normally fail due to ambiguous typing of some key, but that key is not of interest and can be dropped to avoid parse errors. | ||
// Dropping keys only works on top level of the object. | ||
func ParseJsonDropKeys[T any](r io.Reader, dropKeys ...string) (T, error) { | ||
var ( | ||
result T | ||
resultTmp map[string]interface{} | ||
resultTmpBuf = new(bytes.Buffer) | ||
) | ||
if err := json.NewDecoder(r).Decode(&resultTmp); err != nil { | ||
return result, err | ||
} | ||
|
||
for _, k := range dropKeys { | ||
delete(resultTmp, k) | ||
} | ||
|
||
if err := json.NewEncoder(resultTmpBuf).Encode(resultTmp); err != nil { | ||
return result, err | ||
} | ||
if err := json.NewDecoder(resultTmpBuf).Decode(&result); err != nil { | ||
return result, err | ||
} | ||
|
||
return result, nil | ||
} |