refactor: Simplify URL parsing and remove JSON-related methods

This commit is contained in:
2024-11-11 16:14:58 +02:00
committed by antanst (aider)
parent 2bb8589eb7
commit 8b341d2ac6

View File

@@ -1,12 +1,12 @@
package gemini package gemini
import ( import (
"encoding/json"
"fmt" "fmt"
"gemini-grc/logging" "net/url"
"strconv"
) )
type GeminiUrl struct { type URL struct {
Protocol string `json:"protocol,omitempty"` Protocol string `json:"protocol,omitempty"`
Hostname string `json:"hostname,omitempty"` Hostname string `json:"hostname,omitempty"`
Port int `json:"port,omitempty"` Port int `json:"port,omitempty"`
@@ -15,43 +15,62 @@ type GeminiUrl struct {
Full string `json:"full,omitempty"` Full string `json:"full,omitempty"`
} }
func (g *GeminiUrl) Scan(value interface{}) error { func (u *URL) Scan(value interface{}) error {
if value == nil { if value == nil {
// Clear the fields in the current GeminiUrl object (not the pointer itself) // Clear the fields in the current GeminiUrl object (not the pointer itself)
*g = GeminiUrl{} *u = URL{}
return nil return nil
} }
b, ok := value.(string) b, ok := value.(string)
if !ok { if !ok {
return fmt.Errorf("failed to scan GeminiUrl: expected string, got %T", value) return fmt.Errorf("failed to scan GeminiUrl: expected string, got %T", value)
} }
parsedUrl, err := ParseUrl(b, "") parsedURL, err := ParseURL(b, "")
if err != nil { if err != nil {
return err return err
} }
*g = *parsedUrl *u = *parsedURL
return nil return nil
} }
func (u GeminiUrl) String() string { func (u *URL) String() string {
return u.Full return u.Full
// return fmt.Sprintf("%s://%s:%d%s", u.Protocol, u.Hostname, u.Port, u.Path) // return fmt.Sprintf("%s://%s:%d%s", u.Protocol, u.Hostname, u.Port, u.Path)
} }
func GeminiUrltoJSON(g GeminiUrl) string { func ParseURL(input string, descr string) (*URL, error) {
// Serialize the Person struct to JSON u, err := url.Parse(input)
jsonData, err := json.Marshal(g)
if err != nil { if err != nil {
logging.LogError("Error serializing to JSON: %w", err) return nil, fmt.Errorf("%w: Input %s Error %w", ErrURLParse, input, err)
} }
return string(jsonData) protocol := u.Scheme
hostname := u.Hostname()
strPort := u.Port()
path := u.Path
if strPort == "" {
strPort = "1965"
}
port, err := strconv.Atoi(strPort)
if err != nil {
return nil, fmt.Errorf("%w: Input %s Error %w", ErrURLParse, input, err)
}
return &URL{Protocol: protocol, Hostname: hostname, Port: port, Path: path, Descr: descr, Full: u.String()}, nil
} }
func GeminiUrlFromJSON(input string) GeminiUrl { //func GeminiUrltoJSON(g URL) string {
var geminiUrl GeminiUrl // // Serialize the Person struct to JSON
err := json.Unmarshal([]byte(input), &geminiUrl) // jsonData, err := json.Marshal(g)
if err != nil { // if err != nil {
logging.LogError("Error deserializing from JSON: %w", err) // logging.LogError("Error serializing to JSON: %w", err)
} // }
return geminiUrl // return string(jsonData)
} //}
//
//func GeminiUrlFromJSON(input string) URL {
// var geminiUrl URL
// err := json.Unmarshal([]byte(input), &geminiUrl)
// if err != nil {
// logging.LogError("Error deserializing from JSON: %w", err)
// }
// return geminiUrl
//}