Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fqbn): implement json and sql interface #2785

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
42 changes: 42 additions & 0 deletions pkg/fqbn/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// This file is part of arduino-cli.
//
// Copyright 2025 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to license@arduino.cc.

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())
}
41 changes: 41 additions & 0 deletions pkg/fqbn/sql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// This file is part of arduino-cli.
//
// Copyright 2025 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to license@arduino.cc.

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)
}