-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from d-strobel/refactor/dns-package
Refactor/dns package
- Loading branch information
Showing
15 changed files
with
239 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package parsing | ||
|
||
import ( | ||
"encoding/json" | ||
"time" | ||
) | ||
|
||
// CimTimeDuration is a custom time type that embeds the time.Duration type. | ||
// It is designed to handle the unmarshalling of CimInstance time-span json blocks. | ||
type CimTimeDuration struct { | ||
time.Duration | ||
} | ||
|
||
// cimTimeDurationObject is a struct that represents the unmarshalled | ||
// json of a CimInstance time duration object. | ||
// It is used to do the initial unmarshalling of the json block. | ||
type cimTimeDurationObject struct { | ||
Days int32 `json:"Days"` | ||
Hours int32 `json:"Hours"` | ||
Minutes int32 `json:"Minutes"` | ||
Seconds int32 `json:"Seconds"` | ||
MilliSeconds int32 `json:"Milliseconds"` | ||
} | ||
|
||
// UnmarshalJSON implements the json.Unmarshaler interface for the CimTimeDuration type. | ||
// It parses a JSON-encoded CimInstance time duration JSON block and converts it into a CimTimeDuration object. | ||
func (t *CimTimeDuration) UnmarshalJSON(b []byte) error { | ||
var d cimTimeDurationObject | ||
|
||
// Unmarshal the json block into the cimTimeDurationObject struct. | ||
if err := json.Unmarshal(b, &d); err != nil { | ||
return err | ||
} | ||
|
||
// Convert the fields into a time.Duration object. | ||
duration := time.Duration(d.Days)*24*time.Hour + | ||
time.Duration(d.Hours)*time.Hour + | ||
time.Duration(d.Minutes)*time.Minute + | ||
time.Duration(d.Seconds)*time.Second + | ||
time.Duration(d.MilliSeconds)*time.Millisecond | ||
|
||
// Set the time.Duration object to the CimTimeDuration object. | ||
t.Duration = duration | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package parsing | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
// Unit test suite for all CimTimeDuration parsing functions | ||
type CimTimeDurationUnitTestSuite struct { | ||
suite.Suite | ||
// Fixtures | ||
testJson string | ||
testExpected testCimTime | ||
} | ||
|
||
// Fixture objects | ||
type testCimTime struct { | ||
Test string `json:"Test"` | ||
CimTimeDuration CimTimeDuration `json:"LeaseDuration"` | ||
} | ||
|
||
func (suite *CimTimeDurationUnitTestSuite) SetupSuite() { | ||
// Fixtures | ||
suite.testJson = `{ | ||
"Test": "Test String", | ||
"LeaseDuration": { | ||
"Ticks": 6912000000000, | ||
"Days": 8, | ||
"Hours": 0, | ||
"Milliseconds": 0, | ||
"Minutes": 0, | ||
"Seconds": 0, | ||
"TotalDays": 8, | ||
"TotalHours": 192, | ||
"TotalMilliseconds": 691200000, | ||
"TotalMinutes": 11520, | ||
"TotalSeconds": 691200 | ||
} | ||
}` | ||
suite.testExpected = testCimTime{ | ||
Test: "Test String", | ||
CimTimeDuration: CimTimeDuration{ | ||
Duration: 8 * 24 * time.Hour, | ||
}, | ||
} | ||
} | ||
|
||
func TestCimTimeDurationUnitTestSuite(t *testing.T) { | ||
suite.Run(t, &CimTimeDurationUnitTestSuite{}) | ||
} | ||
|
||
func (suite *CimTimeDurationUnitTestSuite) TestUnmarshalJSON() { | ||
suite.T().Parallel() | ||
|
||
suite.Run("should unmarshal the CimInstance duration json to CimTimeDuration", func() { | ||
cimTime := CimTimeDuration{} | ||
expectedTimeDuration, err := time.ParseDuration("1h30m") | ||
suite.Require().NoError(err) | ||
expectedCimTimeDuration := CimTimeDuration{Duration: expectedTimeDuration} | ||
|
||
err = cimTime.UnmarshalJSON([]byte(`{"Days":0,"Hours":1,"Minutes":30,"Seconds":0,"Milliseconds":0}`)) | ||
suite.NoError(err) | ||
suite.Equal(expectedCimTimeDuration, cimTime) | ||
}) | ||
|
||
suite.Run("should unmarshal the CimInstance duration json to CimTimeDuration with all possible fields", func() { | ||
cimTime := CimTimeDuration{} | ||
expectedTimeDuration, err := time.ParseDuration("98h30m5s10ms") | ||
suite.Require().NoError(err) | ||
expectedCimTimeDuration := CimTimeDuration{Duration: expectedTimeDuration} | ||
|
||
err = cimTime.UnmarshalJSON([]byte(`{"Days":4,"Hours":2,"Minutes":30,"Seconds":5,"Milliseconds":10}`)) | ||
suite.NoError(err) | ||
suite.Equal(expectedCimTimeDuration, cimTime) | ||
}) | ||
|
||
suite.Run("should unmarshal the whole CimTimeDuration correctly", func() { | ||
testCimTime := testCimTime{} | ||
err := json.Unmarshal([]byte(suite.testJson), &testCimTime) | ||
suite.NoError(err) | ||
suite.Equal(suite.testExpected, testCimTime) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package server | ||
package dns | ||
|
||
import ( | ||
"context" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.