-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
blueprint: extract unmarshalTOMLviaJSON() helper
- Loading branch information
1 parent
192b5b0
commit c839c44
Showing
2 changed files
with
28 additions
and
17 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,24 @@ | ||
package blueprint | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
// XXX: move to interal/common ? | ||
func unmarshalTOMLviaJSON(u json.Unmarshaler, data any) error { | ||
// This is the most efficient way to reuse code when unmarshaling | ||
// structs in toml, it leaks json errors which is a bit sad but | ||
// because the toml unmarshaler gives us not "[]byte" but an | ||
// already pre-processed "any" we cannot just unmarshal into our | ||
// "fooMarshaling" struct and reuse the result so we resort to | ||
// this workaround (but toml will go away long term anyway). | ||
dataJSON, err := json.Marshal(data) | ||
if err != nil { | ||
return fmt.Errorf("error unmarshaling TOML data %v: %w", data, err) | ||
} | ||
if err := u.UnmarshalJSON(dataJSON); err != nil { | ||
return fmt.Errorf("error decoding TOML %v: %w", data, err) | ||
} | ||
return nil | ||
} |