Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat(fqbn): implement json and sql interface
  • Loading branch information
lucarin91 committed Dec 16, 2024
commit fd12f98666d3ac1352d0ff46cf9550a43954de16
27 changes: 27 additions & 0 deletions pkg/fqbn/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package fqbn

import (
"encoding/json"
"fmt"
)

// UnmarshalJSON implements the json.Unmarshaler interface for the FQBN type.
func (f *FQBN) UnmarshalJSON(data []byte) error {
var fqbnStr string
if err := json.Unmarshal(data, &fqbnStr); err != nil {
return fmt.Errorf("failed to unmarshal FQBN: %w", err)
}

fqbn, err := Parse(fqbnStr)
if err != nil {
return fmt.Errorf("invalid FQBN: %w", err)
}

*f = *fqbn
return nil
}

// MarshalJSON implements the json.Marshaler interface for the FQBN type.
func (f FQBN) MarshalJSON() ([]byte, error) {
return json.Marshal(f.String())
}
26 changes: 26 additions & 0 deletions pkg/fqbn/sql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package fqbn

import "fmt"

// Value implements the driver.Valuer interface for the FQBN type.
func (f FQBN) Value() (any, error) {
return f.String(), nil
}

// Scan implements the sql.Scanner interface for the FQBN type.
func (f *FQBN) Scan(value any) error {
if value == nil {
return nil
}

if v, ok := value.(string); ok {
ParsedFQBN, err := Parse(v)
if err != nil {
return fmt.Errorf("failed to parse FQBN: %w", err)
}
*f = *ParsedFQBN
return nil
}

return fmt.Errorf("unsupported type: %T", value)
}
Loading