(GH-538) Define newtypes for exit_codes fields
#1383
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR Summary
This change defines two new types:
ExitCodeas a lightweight wrapper aroundi32where we can control serialization and deserialization with strings as the serialized format instead of an integer.ExitCodesMapas a lightweight wrapper forHashMap<ExitCode, String>. It implements a helper function for determining whether the map is empty or default and defines the default map for exit0(success) and1(error).The
ExitCodesMapincludes a hand-implementation ofJsonSchemato match the canonical JSON Schema, whichschemarswon't generate from the struct definition.This change not only enables us to provide the canonical JSON Schema for exit code fields in a reusable way, it also removes the need to maintain a converter function for transforming the exit codes map from
HashMap<String, String>toHashMap<i32, String>.This change also:
ResourceManifestandExtensionManifeststructs to use theExitCodesMapfor theexit_codesfield.run_process_async()andinvoke_command()to use the new type.convert_hashmap_string_keys_to_i32()function, since the exit codes are correctly typed from deserialization/instance creation.Note
Leaving this in draft until #1350 is merged, as it includes new development dependencies useful for quickly defining test cases for the newtypes.
PR Context
Prior to this change, the
exit_codesfields for the resource and extension manifests defined the map of exit codes to their semantic meanings asOption<HashMap<String, String>>.As part of schema canonicalization, we need to provide a canonical JSON Schema for that object. Unfortunately, because we don't own either the
JsonSchematrait orHashMap<T>type, we need to use a newtype wrapper.