Skip to content

Commit

Permalink
Reorder unit and package
Browse files Browse the repository at this point in the history
  • Loading branch information
haimkastner committed Dec 3, 2024
1 parent c6e1a05 commit ad3a8c4
Show file tree
Hide file tree
Showing 133 changed files with 431 additions and 181 deletions.
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ It offers support for more than 100 unit types across various unit categories, i

The API is designed to be user-friendly and straightforward to use.

The library is built on top of the [Units.NET](https://github.com/angularsen/UnitsNet) project and leverages their [definitions sources](https://github.com/angularsen/UnitsNet/tree/master/Common/UnitDefinitions) to generate the Python unit classes.
The library is built on top of the [Units.NET](https://github.com/angularsen/UnitsNet) project and leverages their [definitions sources](https://github.com/angularsen/UnitsNet/tree/master/Common/UnitDefinitions) to generate the Golang units.

###### The unitsnet-go package does not require any external dependencies or packages to function.

Expand All @@ -28,40 +28,40 @@ go get github.com/haimkastner/unitsnet-go

## Example Usage

```go
```golang
package main

import (
"log"
"github.com/haimkastner/unitsnet-go"
"github.com/haimkastner/unitsnet-go/units"
)

func main() {

// Create a factory instance
af := unitsnet_go.AngleFactory{}
af := units.AngleFactory{}

angle, _ := af.FromDegrees(180)
// equals to
angle, _ := af.CreateAngle(180, unitsnet_go.AngleDegree)
angle, _ := af.CreateAngle(180, units.AngleDegree)

log.Println(angle.Radians()) // 3.141592653589793
log.Println(angle.Microradians()) // 3141592.65358979
log.Println(angle.Gradians()) // 200
log.Println(angle.Microdegrees()) // 180000000

// As an alternative, a convert style method are also available
log.Println(angle.Convert(unitsnet_go.AngleRadian)) // 3.141592653589793
log.Println(angle.Convert(unitsnet_go.AngleMicroradian)) // 3141592.65358979
log.Println(angle.Convert(unitsnet_go.AngleGradian)) // 200
log.Println(angle.Convert(unitsnet_go.AngleMicrodegree)) // 180000000
log.Println(angle.Convert(units.AngleRadian)) // 3.141592653589793
log.Println(angle.Convert(units.AngleMicroradian)) // 3141592.65358979
log.Println(angle.Convert(units.AngleGradian)) // 200
log.Println(angle.Convert(units.AngleMicrodegree)) // 180000000

// Print the default unit to_string (The default for angle is degrees)
log.Println(angle) // 180.00 °

// Specify unit and fraction digits max length
log.Println(angle.ToString(unitsnet_go.AngleDegree, 0)) // 180 °
log.Println(angle.ToString(unitsnet_go.AngleRadian, 3)) // 3.141 rad
log.Println(angle.ToString(units.AngleDegree, 0)) // 180 °
log.Println(angle.ToString(units.AngleRadian, 3)) // 3.141 rad
}

```
Expand All @@ -70,8 +70,8 @@ func main() {

Check, compare, calculate etc. with unitsnet:

```go
lf := unitsnet_go.LengthFactory{}
```golang
lf := units.LengthFactory{}

length1, _ := lf.FromMeters(10)
length2, _ := lf.FromDecimeters(100)
Expand Down Expand Up @@ -103,16 +103,16 @@ log.Println(results4) // 3.33 m
As UnitsNet provides a convenient way to work within a running service, there are occasions where the data needs to be exposed outside of the service, typically through an API containing the unit value or consumed from an API.

To support this with a clear API schema and make it easy to convert to and from this schema to the specific format, it's recommended to use DTOs and the UnitsNet flavor converters.
```go
lf := unitsnet_go.LengthFactory{}
```golang
lf := units.LengthFactory{}

length, _ := lf.FromMeters(100.01)
// Obtain the DTO object as json, represented by the default unit - meter
lengthDtoJson, _ := length.ToDtoJSON(nil)
log.Println(string(lengthDtoJson)) // {"value":100.01,"unit":"Meter"}

// Obtain the same value but represent DTO in KM
lengthKilometer := unitsnet_go.LengthKilometer // Default value
lengthKilometer := units.LengthKilometer // Default value
lengthDtoRepresentsInKmJson, _ := length.ToDtoJSON(&lengthKilometer)
log.Println(string(lengthDtoRepresentsInKmJson)) // {"value":0.10001,"unit":"Kilometer"}

Expand All @@ -130,7 +130,7 @@ lengthDto := length.ToDto(nil)
// # Get the json representation of the DTO
lengthJson, _ := lengthDto.ToJSON() // {"value":100.01,"unit":"Meter"}
// # Obtain DTO instance from a json representation
lengthDtoFromJson, _ := unitsnet_go.LengthDtoFactory{}.FromJSON(lengthJson)
lengthDtoFromJson, _ := units.LengthDtoFactory{}.FromJSON(lengthJson)
// # Obtain Length unit from a DTO instance
lengthFromJson, _ := lf.FromDto(*lengthDtoFromJson)
log.Println(lengthFromJson) // 100.01 m
Expand Down
4 changes: 2 additions & 2 deletions tests/test_unit_arithmetics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package unitsnet_go_test
import (
"testing"

"github.com/haimkastner/unitsnet-go/unitsnet_go"
"github.com/haimkastner/unitsnet-go/units"
)

func TestUnitArithmetics(t *testing.T) {
lf := unitsnet_go.LengthFactory{}
lf := units.LengthFactory{}
length1, _ := lf.FromMeters(10)
length2, _ := lf.FromMeters(3)

Expand Down
4 changes: 2 additions & 2 deletions tests/test_unit_comparing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package unitsnet_go_test
import (
"testing"

"github.com/haimkastner/unitsnet-go/unitsnet_go"
"github.com/haimkastner/unitsnet-go/units"
)

func TestUnitComparing(t *testing.T) {
lf := unitsnet_go.LengthFactory{}
lf := units.LengthFactory{}
length1, _ := lf.FromMeters(10)
length2, _ := lf.FromDecimeters(100)
length3, _ := lf.FromMeters(3)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_unit_conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"math"
"testing"

"github.com/haimkastner/unitsnet-go/unitsnet_go"
"github.com/haimkastner/unitsnet-go/units"
)

var af = unitsnet_go.AngleFactory{}
var inf = unitsnet_go.InformationFactory{}
var af = units.AngleFactory{}
var inf = units.InformationFactory{}
var maxDelta = 0.0000001

func TestConvertFromBaseToOtherUnit(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions tests/test_unit_creation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package unitsnet_go_test
import (
"testing"

"github.com/haimkastner/unitsnet-go/unitsnet_go"
"github.com/haimkastner/unitsnet-go/units"
)

func TestUnitCreation(t *testing.T) {
// Create a factory instance
af := unitsnet_go.AngleFactory{}
af := units.AngleFactory{}

angleCreateDriven, _ := af.CreateAngle(180, unitsnet_go.AngleDegree)
angleCreateDriven, _ := af.CreateAngle(180, units.AngleDegree)
angleFromDriven, _ := af.FromDegrees(180)

if angleCreateDriven.BaseValue() != angleFromDriven.BaseValue() {
Expand Down
18 changes: 9 additions & 9 deletions tests/test_unit_dto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package unitsnet_go_test
import (
"testing"

"github.com/haimkastner/unitsnet-go/unitsnet_go"
"github.com/haimkastner/unitsnet-go/units"
)

func TestUnitDto(t *testing.T) {
lf := unitsnet_go.LengthFactory{}
lf := units.LengthFactory{}

// Create a length instance from meters
length1, _ := lf.FromMeters(100.01)
Expand All @@ -23,7 +23,7 @@ func TestUnitDto(t *testing.T) {

// Test: Create JSON from a specific unit (Centimeter)
t.Run("Test Create JSON From Specific Unit", func(t *testing.T) {
lengthInCm := unitsnet_go.LengthCentimeter
lengthInCm := units.LengthCentimeter
dtoJson, _ := length1.ToDtoJSON(&lengthInCm)
expected := `{"value":10001,"unit":"Centimeter"}`
if string(dtoJson) != expected {
Expand All @@ -42,7 +42,7 @@ func TestUnitDto(t *testing.T) {

// Test: Directly create JSON from a specific unit
t.Run("Test Directly Create JSON From Specific Unit", func(t *testing.T) {
lengthInCm := unitsnet_go.LengthCentimeter
lengthInCm := units.LengthCentimeter
dtoJson, _ := length1.ToDtoJSON(&lengthInCm)
expected := `{"value":10001,"unit":"Centimeter"}`
if string(dtoJson) != expected {
Expand All @@ -60,7 +60,7 @@ func TestUnitDto(t *testing.T) {

// Test: Create DTO from a specific unit
t.Run("Test Create DTO From Specific Unit", func(t *testing.T) {
lengthInCm := unitsnet_go.LengthCentimeter
lengthInCm := units.LengthCentimeter
dto := length1.ToDto(&lengthInCm)
if dto.Value != 10001 || dto.Unit != "Centimeter" {
t.Errorf("Expected {10001, Centimeter}, got {%f, %s}", dto.Value, dto.Unit)
Expand All @@ -78,7 +78,7 @@ func TestUnitDto(t *testing.T) {

// Test: Load from specific unit JSON
t.Run("Test Load From Specific Unit JSON", func(t *testing.T) {
lengthInCm := unitsnet_go.LengthCentimeter
lengthInCm := units.LengthCentimeter
dtoJson, _ := length1.ToDtoJSON(&lengthInCm)
lengthFromJson, _ := lf.FromDtoJSON(dtoJson)
if lengthFromJson.Decimeters() != 1000.1 {
Expand All @@ -97,7 +97,7 @@ func TestUnitDto(t *testing.T) {

// Test: Load from specific unit DTO
t.Run("Test Load From Specific Unit DTO", func(t *testing.T) {
lengthInCm := unitsnet_go.LengthCentimeter
lengthInCm := units.LengthCentimeter
dto := length1.ToDto(&lengthInCm)
lengthFromDto, _ := lf.FromDto(dto)
if lengthFromDto.Decimeters() != 1000.1 {
Expand Down Expand Up @@ -134,13 +134,13 @@ func TestUnitDto(t *testing.T) {

// Test: Should be similar values from two DTO representations
t.Run("Test Should Be Similar Values From Two DTO Representations", func(t *testing.T) {
length, _ := unitsnet_go.LengthFactory{}.FromMeters(100.01)
length, _ := units.LengthFactory{}.FromMeters(100.01)

// Obtain DTO object as JSON, represented by default unit (Meter)
lengthDtoJson, _ := length.ToDtoJSON(nil)

// Obtain same value represented in kilometers
lengthKilometer := unitsnet_go.LengthKilometer
lengthKilometer := units.LengthKilometer
lengthDtoRepresentsInKmJson, _ := length.ToDtoJSON(&lengthKilometer)

// Load JSON to DTO, and load
Expand Down
38 changes: 19 additions & 19 deletions tests/test_unit_formatting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,70 +4,70 @@ import (
"fmt"
"testing"

"github.com/haimkastner/unitsnet-go/unitsnet_go"
"github.com/haimkastner/unitsnet-go/units"
)

func TestFmtFormatDefault(t *testing.T) {
angle, _ := unitsnet_go.AngleFactory{}.FromDegrees(180)
angle, _ := units.AngleFactory{}.FromDegrees(180)
expected := "180.00 °"
if fmt.Sprintf("%v", angle) != expected {
t.Errorf("Expected '%s', got '%s'", expected, fmt.Sprintf("%v", angle))
}
}

func TestFormatBase(t *testing.T) {
angle, _ := unitsnet_go.AngleFactory{}.FromDegrees(180)
if angle.ToString(unitsnet_go.AngleDegree, 0) != "180 °" {
t.Errorf("Expected '180 °', got '%s'", angle.ToString(unitsnet_go.AngleDegree, 0))
angle, _ := units.AngleFactory{}.FromDegrees(180)
if angle.ToString(units.AngleDegree, 0) != "180 °" {
t.Errorf("Expected '180 °', got '%s'", angle.ToString(units.AngleDegree, 0))
}
}

func TestFormatOtherUnit(t *testing.T) {
angle, _ := unitsnet_go.AngleFactory{}.FromDegrees(180)
angle, _ := units.AngleFactory{}.FromDegrees(180)

if angle.ToString(unitsnet_go.AngleDegree, 0) != "180 °" {
t.Errorf("Expected '180 °', got '%s'", angle.ToString(unitsnet_go.AngleDegree, 0))
if angle.ToString(units.AngleDegree, 0) != "180 °" {
t.Errorf("Expected '180 °', got '%s'", angle.ToString(units.AngleDegree, 0))
}

if angle.ToString(unitsnet_go.AngleRadian, -1) != "3.141592653589793 rad" {
t.Errorf("Expected '3.141592653589793 rad', got '%s'", angle.ToString(unitsnet_go.AngleRadian, -1))
if angle.ToString(units.AngleRadian, -1) != "3.141592653589793 rad" {
t.Errorf("Expected '3.141592653589793 rad', got '%s'", angle.ToString(units.AngleRadian, -1))
}

if angle.ToString(unitsnet_go.AngleMilliradian, -1) != "3141.592653589793 mrad" {
t.Errorf("Expected '3141.592653589793 mrad', got '%s'", angle.ToString(unitsnet_go.AngleRadian, -1))
if angle.ToString(units.AngleMilliradian, -1) != "3141.592653589793 mrad" {
t.Errorf("Expected '3141.592653589793 mrad', got '%s'", angle.ToString(units.AngleRadian, -1))
}
}

func TestFormatWithDigitsLimit(t *testing.T) {
angle, _ := unitsnet_go.AngleFactory{}.FromDegrees(180)
angle, _ := units.AngleFactory{}.FromDegrees(180)

// Without limit
if result := angle.ToString(unitsnet_go.AngleRadian, -1); result != "3.141592653589793 rad" {
if result := angle.ToString(units.AngleRadian, -1); result != "3.141592653589793 rad" {
t.Errorf("Expected '3.141592653589793 rad', got '%s'", result)
}

// Without limit - random negative number
if result := angle.ToString(unitsnet_go.AngleRadian, -10); result != "3.141592653589793 rad" {
if result := angle.ToString(units.AngleRadian, -10); result != "3.141592653589793 rad" {
t.Errorf("Expected '3.141592653589793 rad', got '%s'", result)
}

// Limit to 0 digits
if result := angle.ToString(unitsnet_go.AngleRadian, 0); result != "3 rad" {
if result := angle.ToString(units.AngleRadian, 0); result != "3 rad" {
t.Errorf("Expected '3 rad', got '%s'", result)
}

// Limit to 1 digit
if result := angle.ToString(unitsnet_go.AngleRadian, 1); result != "3.1 rad" {
if result := angle.ToString(units.AngleRadian, 1); result != "3.1 rad" {
t.Errorf("Expected '3.1 rad', got '%s'", result)
}

// Limit to 2 digits
if result := angle.ToString(unitsnet_go.AngleRadian, 2); result != "3.14 rad" {
if result := angle.ToString(units.AngleRadian, 2); result != "3.14 rad" {
t.Errorf("Expected '3.14 rad', got '%s'", result)
}

// No change if it's already formatted correctly
if result := angle.ToString(unitsnet_go.AngleDegree, 2); result != "180.00 °" {
if result := angle.ToString(units.AngleDegree, 2); result != "180.00 °" {
t.Errorf("Expected '180.00 °', got '%s'", result)
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion unitsnet_go/angle.go → units/angle_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion unitsnet_go/area.go → units/area_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ad3a8c4

Please sign in to comment.