Skip to content

Support Cross-Technologies Unit & API Specifications #31

Closed

Description

@haimkastner

As part of the powerful and wide support in multiple languages in the unit definition, I think it will be a good idea to standardize the way how unit is represented in an API spec, and how it will be exposed/loaded.

The API object should look similar to:

export interface LengthDto {
    value: number;
    unit: LengthUnits;
}

And this is also how it will be represented in the OpenAPI specification.

The JSON will look like this:

{
   "value":100.01,
   "unit":"Meter"
}

As part of the full JSON payload something like:

{
   "someInfo":"someValue",
   "someInfoLength":{
      "value":100.01,
      "unit":"Meter"
   }
}

Any unit class will have an API to expose and load it:

export interface LengthDto {
    value: number;
    unit: LengthUnits;
}

export  class Length {

  ...
  ...

  public toDto(holdInUnit: LengthUnits = LengthUnits.Meters): LengthDto {
        return {
            value: this.convert(holdInUnit),
            unit: holdInUnit
        } ;
    }

    public static fromDto(dtoLength: LengthDto): Length {
        return new Length(dtoLength.value, dtoLength.unit);
    }
}

See also #29 for motivation

Activity

  1. self-assigned this
    on Mar 8, 2024
  2. Shentoza commented on Mar 8, 2024

    @Shentoza

    that would be a great change, currently we're holding a custom LengthDto that allows us to parse to/from out c# unitsnet backend.

    const LengthToString = (input: { value: number; unit: string }): string => {
      const unit = getLengthUnit(input.unit);
      const l = new Length(input.value, getLengthUnit(input.unit));
      return l.toString(unit);
    };
    

    the one usage we're currently not doing is actually converting the data from the backend to a UnitsNetJS Length, since it's hard to map the "Meter" string coming from the backend to the actual enum value.

    Currenlty (as far as I see it) a UnitsNetJS Length has no information about the unit it currently is representing, since it always has Meter as a base.

  3. added a commit that references this issue on Mar 8, 2024
  4. haimkastner commented on Mar 8, 2024

    @haimkastner
    OwnerAuthor

    @Shentoza
    In your time you can see my new PR #32 with an implementation of this and check it that can improve your use-cases.

    In the meantime, I have opened a discussion angularsen/UnitsNet#1378 with the UnitsNet owner to see his opinion about such a cross-language convention.

  5. Shentoza commented on Mar 8, 2024

    @Shentoza

    That's awesome. The example implementation in C# is basically what we're currently doing, to support a readable API response from our backend. Once your PR completes, we can remove all the bandaid solutions and almost feel like we're using a cross-technology framework for unit handling

  6. added a commit that references this issue on Apr 26, 2024
  7. haimkastner commented on Apr 26, 2024

    @haimkastner
    OwnerAuthor

    Implemented by #32

  8. haimkastner commented on Apr 28, 2024

    @haimkastner
    OwnerAuthor

    @Shentoza After ~ two months of waiting for progress on the UnitNet library discussion, I've decided to merge the changes into the main version. From now on, you can utilize this updated version from both JS #32 & Python (haimkastner/unitsnet-py#18)

    Regardless of the final decision made in the UnitNet thread, I'll maintain backward compatibility with this merged API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    ⌂ 换网址