You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
boundary/api/types.go

35 lines
690 B

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package api
import (
"encoding/json"
"fmt"
"time"
)
// Duration represents a time.Duration and supports marshaling/unmarshaling from
// a json string
type Duration struct {
time.Duration
}
func (d Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(d.Duration.String())
}
func (d *Duration) UnmarshalJSON(b []byte) error {
const op = "api.(Duration).UnmarshalJSON"
var str string
if err := json.Unmarshal(b, &str); err != nil {
return fmt.Errorf("%s: %w", op, err)
}
val, err := time.ParseDuration(str)
if err != nil {
return fmt.Errorf("%s: %w", op, err)
}
d.Duration = val
return nil
}