- 
                Notifications
    
You must be signed in to change notification settings  - Fork 0
 
Add scalar.proto, generate stubs, implement converter, and add unit tests. #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from 3 commits
      Commits
    
    
            Show all changes
          
          
            5 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      610bc05
              
                Add scalar.proto, generate stubs, implement converter, and add unit t…
              
              
                 67bbec6
              
                Constrain types for python scalar class. Allow prerelease for nitypes…
              
              
                 8166f1c
              
                Import TypeAlias from typing_extensions to fix RP build.
              
              
                 26be1f7
              
                Refactor if/else statements to use a lookup.
              
              
                 6eca4ac
              
                Rename internal constant.
              
              
                 File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
      
      Oops, something went wrong.
      
    
  
  
    
      This file contains hidden or 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,23 @@ | ||
| syntax = "proto3"; | ||
| 
     | 
||
| package ni.protobuf.types; | ||
| 
     | 
||
| option csharp_namespace = "NationalInstruments.Protobuf.Types"; | ||
| option go_package = "types"; | ||
| option java_multiple_files = true; | ||
| option java_outer_classname = "ScalarProto"; | ||
| option java_package = "com.ni.protobuf.types"; | ||
| option objc_class_prefix = "NIPT"; | ||
| option php_namespace = "NI\\PROTOBUF\\TYPES"; | ||
| option ruby_package = "NI::Protobuf::Types"; | ||
| 
     | 
||
| message ScalarData { | ||
| string units = 1; | ||
| 
     | 
||
| oneof value { | ||
| double double_value = 2; | ||
| int32 int32_value = 3; | ||
| bool bool_value = 4; | ||
| string string_value = 5; | ||
| } | ||
| } | 
  
    
      This file contains hidden or 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
    
  
  
    
              Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
      
      Oops, something went wrong.
      
    
  
  
    
      This file contains hidden or 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,40 @@ | ||
| """ | ||
| @generated by mypy-protobuf. Do not edit manually! | ||
| isort:skip_file | ||
| """ | ||
| 
     | 
||
| import builtins | ||
| import google.protobuf.descriptor | ||
| import google.protobuf.message | ||
| import typing | ||
| 
     | 
||
| DESCRIPTOR: google.protobuf.descriptor.FileDescriptor | ||
| 
     | 
||
| @typing.final | ||
| class ScalarData(google.protobuf.message.Message): | ||
| DESCRIPTOR: google.protobuf.descriptor.Descriptor | ||
| 
     | 
||
| UNITS_FIELD_NUMBER: builtins.int | ||
| DOUBLE_VALUE_FIELD_NUMBER: builtins.int | ||
| INT32_VALUE_FIELD_NUMBER: builtins.int | ||
| BOOL_VALUE_FIELD_NUMBER: builtins.int | ||
| STRING_VALUE_FIELD_NUMBER: builtins.int | ||
| units: builtins.str | ||
| double_value: builtins.float | ||
| int32_value: builtins.int | ||
| bool_value: builtins.bool | ||
| string_value: builtins.str | ||
| def __init__( | ||
| self, | ||
| *, | ||
| units: builtins.str = ..., | ||
| double_value: builtins.float = ..., | ||
| int32_value: builtins.int = ..., | ||
| bool_value: builtins.bool = ..., | ||
| string_value: builtins.str = ..., | ||
| ) -> None: ... | ||
| def HasField(self, field_name: typing.Literal["bool_value", b"bool_value", "double_value", b"double_value", "int32_value", b"int32_value", "string_value", b"string_value", "value", b"value"]) -> builtins.bool: ... | ||
| def ClearField(self, field_name: typing.Literal["bool_value", b"bool_value", "double_value", b"double_value", "int32_value", b"int32_value", "string_value", b"string_value", "units", b"units", "value", b"value"]) -> None: ... | ||
| def WhichOneof(self, oneof_group: typing.Literal["value", b"value"]) -> typing.Literal["double_value", "int32_value", "bool_value", "string_value"] | None: ... | ||
| 
     | 
||
| global___ScalarData = ScalarData | 
  
    
      This file contains hidden or 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,4 @@ | ||
| # Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT! | ||
| """Client and server classes corresponding to protobuf-defined services.""" | ||
| import grpc | ||
| 
     | 
  
    
      This file contains hidden or 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,17 @@ | ||
| """ | ||
| @generated by mypy-protobuf. Do not edit manually! | ||
| isort:skip_file | ||
| """ | ||
| 
     | 
||
| import abc | ||
| import collections.abc | ||
| import grpc | ||
| import grpc.aio | ||
| import typing | ||
| 
     | 
||
| _T = typing.TypeVar("_T") | ||
| 
     | 
||
| class _MaybeAsyncIterator(collections.abc.AsyncIterator[_T], collections.abc.Iterator[_T], metaclass=abc.ABCMeta): ... | ||
| 
     | 
||
| class _ServicerContext(grpc.ServicerContext, grpc.aio.ServicerContext): # type: ignore[misc, type-arg] | ||
| ... | 
  
    
      This file contains hidden or 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,59 @@ | ||
| """Classes to convert between measurement specific protobuf types and containers.""" | ||
| 
     | 
||
| from typing import Type, Union | ||
| 
     | 
||
| from ni.protobuf.types import scalar_pb2 | ||
| from nitypes.scalar import Scalar | ||
| from typing_extensions import TypeAlias | ||
| 
     | 
||
| from nipanel.converters import Converter | ||
| 
     | 
||
| _AnyScalarType: TypeAlias = Union[bool, int, float, str] | ||
| 
     | 
||
| 
     | 
||
| class ScalarConverter(Converter[Scalar[_AnyScalarType], scalar_pb2.ScalarData]): | ||
| """A converter for Scalar objects.""" | ||
| 
     | 
||
| @property | ||
| def python_typename(self) -> str: | ||
| """The Python type that this converter handles.""" | ||
| return Scalar.__name__ | ||
| 
     | 
||
| @property | ||
| def protobuf_message(self) -> Type[scalar_pb2.ScalarData]: | ||
| """The type-specific protobuf message for the Python type.""" | ||
| return scalar_pb2.ScalarData | ||
| 
     | 
||
| def to_protobuf_message(self, python_value: Scalar[_AnyScalarType]) -> scalar_pb2.ScalarData: | ||
| """Convert the Python Scalar to a protobuf scalar_pb2.ScalarData.""" | ||
| message = self.protobuf_message() | ||
| message.units = python_value.units | ||
| if isinstance(python_value.value, bool): | ||
| message.bool_value = python_value.value | ||
| elif isinstance(python_value.value, int): | ||
| message.int32_value = python_value.value | ||
| elif isinstance(python_value.value, float): | ||
| message.double_value = python_value.value | ||
| elif isinstance(python_value.value, str): | ||
| message.string_value = python_value.value | ||
| else: | ||
| raise TypeError(f"Unexpected type for python_value.value: {type(python_value.value)}") | ||
| 
     | 
||
| return message | ||
| 
     | 
||
| def to_python_value(self, protobuf_value: scalar_pb2.ScalarData) -> Scalar[_AnyScalarType]: | ||
| """Convert the protobuf message to a Python Scalar.""" | ||
| if protobuf_value.units is None: | ||
| raise ValueError("protobuf.units cannot be None.") | ||
| 
     | 
||
| pb_type = protobuf_value.WhichOneof("value") | ||
                
      
                  mjohanse-emr marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| if pb_type == "bool_value": | ||
| return Scalar(protobuf_value.bool_value, protobuf_value.units) | ||
| elif pb_type == "int32_value": | ||
| return Scalar(protobuf_value.int32_value, protobuf_value.units) | ||
| elif pb_type == "double_value": | ||
| return Scalar(protobuf_value.double_value, protobuf_value.units) | ||
| elif pb_type == "string_value": | ||
| return Scalar(protobuf_value.string_value, protobuf_value.units) | ||
| else: | ||
| raise ValueError(f"Unexpected value for protobuf_value.WhichOneOf: {pb_type}") | ||
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.