From fd12f98666d3ac1352d0ff46cf9550a43954de16 Mon Sep 17 00:00:00 2001 From: lucarin91 Date: Mon, 16 Dec 2024 18:59:18 +0100 Subject: [PATCH 1/2] feat(fqbn): implement json and sql interface --- pkg/fqbn/json.go | 27 +++++++++++++++++++++++++++ pkg/fqbn/sql.go | 26 ++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 pkg/fqbn/json.go create mode 100644 pkg/fqbn/sql.go diff --git a/pkg/fqbn/json.go b/pkg/fqbn/json.go new file mode 100644 index 00000000000..5cd9e5fb03d --- /dev/null +++ b/pkg/fqbn/json.go @@ -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()) +} diff --git a/pkg/fqbn/sql.go b/pkg/fqbn/sql.go new file mode 100644 index 00000000000..e7618397bac --- /dev/null +++ b/pkg/fqbn/sql.go @@ -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) +} From e862942dfd7b8c704d0fac069af222ae5c3a70f2 Mon Sep 17 00:00:00 2001 From: Luca Rinaldi Date: Mon, 24 Feb 2025 14:24:32 +0100 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Cristian Maglie --- pkg/fqbn/json.go | 15 +++++++++++++++ pkg/fqbn/sql.go | 15 +++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/pkg/fqbn/json.go b/pkg/fqbn/json.go index 5cd9e5fb03d..bb21521be9a 100644 --- a/pkg/fqbn/json.go +++ b/pkg/fqbn/json.go @@ -1,3 +1,18 @@ +// 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 ( diff --git a/pkg/fqbn/sql.go b/pkg/fqbn/sql.go index e7618397bac..8b4f5e99b9d 100644 --- a/pkg/fqbn/sql.go +++ b/pkg/fqbn/sql.go @@ -1,3 +1,18 @@ +// 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"