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

Skip to content

gripmock/types

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Gripmock Types

A package of custom types for Gripmock that solves JSON serialization/deserialization problems.

Problem

In issue #648, a problem was identified with deserializing string time values into time.Duration. When trying to use JSON like:

{
  "output": {
    "delay": "100ms"
  }
}

The following error occurred:

json: cannot unmarshal number { into Go struct field Output.Delay of type time.Duration

Solution

The types package provides a custom Duration type that correctly handles string time values in JSON.

Available Types

Duration

A custom type alias for time.Duration that provides JSON marshaling/unmarshaling support for string values.

Supported formats

Only string values are supported:

{
  "delay": "100ms"
}

Valid duration formats:

  • "100ms" - milliseconds
  • "2s" - seconds
  • "1m" - minutes
  • "1h" - hours
  • "1h30m" - combined formats

Usage

type Output struct {
    Delay types.Duration `json:"delay"`
    Data  map[string]interface{} `json:"data"`
}

var output Output
json.Unmarshal([]byte(`{"delay": "100ms"}`), &output)

// Access the duration value
duration := time.Duration(output.Delay)

Installation

go get github.com/gripmock/types

Testing

go test ./...

License

MIT License - see LICENSE file.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published