-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
66 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,7 @@ | ||
package gttp | ||
|
||
import "encoding/json" | ||
|
||
// M is a map[string]string | ||
type M map[string]string | ||
|
||
// Map is a map[string]interface{} | ||
type Map map[string]interface{} | ||
|
||
// toBytes ... | ||
func toBytes(payload any) []byte { | ||
if payload == nil { | ||
return nil | ||
} | ||
|
||
// Check if the payload is already of type []byte. | ||
if bytesPayload, ok := payload.([]byte); ok { | ||
return bytesPayload | ||
} | ||
|
||
// Check if the payload is already of type string. | ||
if stringPayload, ok := payload.(string); ok { | ||
return []byte(stringPayload) | ||
} | ||
|
||
// Marshal the payload to JSON. | ||
bytesPayload, err := json.Marshal(payload) | ||
if err != nil { | ||
return nil | ||
} | ||
|
||
return bytesPayload | ||
} |
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,43 @@ | ||
package helpers | ||
|
||
import "encoding/json" | ||
|
||
// toBytes ... | ||
func ToBytes(payload any) []byte { | ||
if payload == nil { | ||
return nil | ||
} | ||
|
||
// Check if the payload is already of type []byte. | ||
if bytesPayload, ok := payload.([]byte); ok { | ||
return bytesPayload | ||
} | ||
|
||
// Check if the payload is already of type string. | ||
if stringPayload, ok := payload.(*string); ok { | ||
return []byte(*stringPayload) | ||
} | ||
|
||
// Marshal the payload to JSON. | ||
bytesPayload, err := json.Marshal(payload) | ||
if err != nil { | ||
return nil | ||
} | ||
|
||
return bytesPayload | ||
} | ||
|
||
// FromBytes ... | ||
func FromBytes[T any](payload []byte) (T, error) { | ||
var data T | ||
if payload == nil { | ||
return data, nil | ||
} | ||
|
||
err := json.Unmarshal(payload, &data) | ||
if err != nil { | ||
return data, err | ||
} | ||
|
||
return data, nil | ||
} |