Skip to content

Commit fd12f98

Browse files
committed
feat(fqbn): implement json and sql interface
1 parent 84fc413 commit fd12f98

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

pkg/fqbn/json.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package fqbn
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
)
7+
8+
// UnmarshalJSON implements the json.Unmarshaler interface for the FQBN type.
9+
func (f *FQBN) UnmarshalJSON(data []byte) error {
10+
var fqbnStr string
11+
if err := json.Unmarshal(data, &fqbnStr); err != nil {
12+
return fmt.Errorf("failed to unmarshal FQBN: %w", err)
13+
}
14+
15+
fqbn, err := Parse(fqbnStr)
16+
if err != nil {
17+
return fmt.Errorf("invalid FQBN: %w", err)
18+
}
19+
20+
*f = *fqbn
21+
return nil
22+
}
23+
24+
// MarshalJSON implements the json.Marshaler interface for the FQBN type.
25+
func (f FQBN) MarshalJSON() ([]byte, error) {
26+
return json.Marshal(f.String())
27+
}

pkg/fqbn/sql.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package fqbn
2+
3+
import "fmt"
4+
5+
// Value implements the driver.Valuer interface for the FQBN type.
6+
func (f FQBN) Value() (any, error) {
7+
return f.String(), nil
8+
}
9+
10+
// Scan implements the sql.Scanner interface for the FQBN type.
11+
func (f *FQBN) Scan(value any) error {
12+
if value == nil {
13+
return nil
14+
}
15+
16+
if v, ok := value.(string); ok {
17+
ParsedFQBN, err := Parse(v)
18+
if err != nil {
19+
return fmt.Errorf("failed to parse FQBN: %w", err)
20+
}
21+
*f = *ParsedFQBN
22+
return nil
23+
}
24+
25+
return fmt.Errorf("unsupported type: %T", value)
26+
}

0 commit comments

Comments
 (0)