fix: Cleanup serialization and hashing code (#14317)

This commit is contained in:
Ben Reinhart
2021-04-26 14:04:40 -07:00
committed by GitHub
parent ebc938059b
commit 2a1235c0c2
10 changed files with 124 additions and 38 deletions

View File

@@ -15,14 +15,20 @@
# specific language governing permissions and limitations
# under the License.
import hashlib
import json
from typing import Any, Dict
from typing import Any, Callable, Dict, Optional
import simplejson as json
def md5_sha_from_str(val: str) -> str:
return hashlib.md5(val.encode("utf-8")).hexdigest()
def md5_sha_from_dict(opts: Dict[Any, Any]) -> str:
json_data = json.dumps(opts, sort_keys=True)
def md5_sha_from_dict(
obj: Dict[Any, Any],
ignore_nan: bool = False,
default: Optional[Callable[[Any], Any]] = None,
) -> str:
json_data = json.dumps(obj, sort_keys=True, ignore_nan=ignore_nan, default=default)
return md5_sha_from_str(json_data)