- 
                Notifications
    
You must be signed in to change notification settings  - Fork 338
 
Add Dynamic.toMixin() method #1257
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
          
     Open
      
      
            Mikulas
  wants to merge
  1
  commit into
  apple:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
Mikulas:tomixin
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
      
        
          +308
        
        
          −0
        
        
          
        
      
    
  
  
     Open
                    Changes from all commits
      Commits
    
    
  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
  
    
      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
    
  
  
    
              
        
          
          
            114 changes: 114 additions & 0 deletions
          
          114 
        
  pkl-core/src/test/files/LanguageSnippetTests/input/api/dynamicToMixin.pkl
  
  
      
      
   
        
      
      
    
  
    
      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,114 @@ | ||
| amends "../snippetTest.pkl" | ||
| 
     | 
||
| local class Person { | ||
| name: String | ||
| age: Int = 0 | ||
| } | ||
| 
     | 
||
| examples { | ||
| ["basic conversion"] { | ||
| // Convert Dynamic with properties to Mixin | ||
| local dynamic1 = new Dynamic { | ||
| name = "Pigeon" | ||
| age = 42 | ||
| } | ||
| local mixin1 = dynamic1.toMixin() | ||
| new Dynamic { name = "Original" } |> mixin1 | ||
| } | ||
| 
     | 
||
| ["empty Dynamic"] { | ||
| // Empty Dynamic should create identity mixin | ||
| local emptyDynamic = new Dynamic {} | ||
| local emptyMixin = emptyDynamic.toMixin() | ||
| new Dynamic { existing = "value" } |> emptyMixin | ||
| } | ||
| 
     | 
||
| ["with elements"] { | ||
| // Elements should be appended | ||
| local dynamicWithElements = new Dynamic { | ||
| "element1" | ||
| "element2" | ||
| } | ||
| local mixinWithElements = dynamicWithElements.toMixin() | ||
| new Dynamic { "base" } |> mixinWithElements | ||
| } | ||
| 
     | 
||
| ["with entries"] { | ||
| // Entries should be merged | ||
| local dynamicWithEntries = new Dynamic { | ||
| ["key1"] = "value1" | ||
| ["key2"] = "value2" | ||
| } | ||
| local mixinWithEntries = dynamicWithEntries.toMixin() | ||
| new Dynamic { ["baseKey"] = "baseValue" } |> mixinWithEntries | ||
| } | ||
| 
     | 
||
| ["with mixed members"] { | ||
| // Properties, elements, and entries should all be merged | ||
| local dynamicMixed = new Dynamic { | ||
| prop = "property" | ||
| "element" | ||
| ["entry"] = "entryValue" | ||
| } | ||
| local mixinMixed = dynamicMixed.toMixin() | ||
| new Dynamic { baseProp = "base" } |> mixinMixed | ||
| } | ||
| 
     | 
||
| ["applying to Typed object"] { | ||
| // Mixin should work with typed objects | ||
| local dynamicPerson = new Dynamic { | ||
| name = "Modified" | ||
| age = 100 | ||
| } | ||
| local mixinPerson = dynamicPerson.toMixin() | ||
| new Person { name = "Original"; age = 20 } |> mixinPerson | ||
| } | ||
| 
     | 
||
| ["chaining mixins"] { | ||
| // Multiple mixins can be chained | ||
| local mixin1 = new Dynamic { extra = "value" }.toMixin() | ||
| local mixin2 = new Dynamic { another = "field" }.toMixin() | ||
| new Dynamic { base = "start" } |> mixin1 |> mixin2 | ||
| } | ||
| 
     | 
||
| ["reusable mixin"] { | ||
| // Mixin can be applied to multiple objects | ||
| local reusableMixin = new Dynamic { shared = "config" }.toMixin() | ||
| new { | ||
| first = new Dynamic { id = 1 } |> reusableMixin | ||
| second = new Dynamic { id = 2 } |> reusableMixin | ||
| } | ||
| } | ||
| 
     | 
||
| ["overriding properties"] { | ||
| // Properties from mixin should override base properties | ||
| local overrideDynamic = new Dynamic { | ||
| name = "Override" | ||
| value = 999 | ||
| } | ||
| local overrideMixin = overrideDynamic.toMixin() | ||
| new Dynamic { name = "Original"; value = 1; other = "keep" } |> overrideMixin | ||
| } | ||
| 
     | 
||
| ["replacement vs merge for nested objects"] { | ||
| // Test replacement vs merge semantics | ||
| local base = new { | ||
| a1 { | ||
| b1 = 2 | ||
| } | ||
| a2 { | ||
| b1 = 2 | ||
| } | ||
| } | ||
| local overrideValue = new Dynamic { | ||
| a1 = new Dynamic { | ||
| b2 = 2 | ||
| } | ||
| a2 { | ||
| b2 = 2 | ||
| } | ||
| } | ||
| (base) |> overrideValue.toMixin() | ||
| } | ||
| } | ||
| 
     | 
        
          
          
            78 changes: 78 additions & 0 deletions
          
          78 
        
  pkl-core/src/test/files/LanguageSnippetTests/output/api/dynamicToMixin.pcf
  
  
      
      
   
        
      
      
    
  
    
      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,78 @@ | ||
| examples { | ||
| ["basic conversion"] { | ||
| new { | ||
| name = "Pigeon" | ||
| age = 42 | ||
| } | ||
| } | ||
| ["empty Dynamic"] { | ||
| new { | ||
| existing = "value" | ||
| } | ||
| } | ||
| ["with elements"] { | ||
| new { | ||
| "base" | ||
| "element1" | ||
| "element2" | ||
| } | ||
| } | ||
| ["with entries"] { | ||
| new { | ||
| ["baseKey"] = "baseValue" | ||
| ["key1"] = "value1" | ||
| ["key2"] = "value2" | ||
| } | ||
| } | ||
| ["with mixed members"] { | ||
| new { | ||
| baseProp = "base" | ||
| prop = "property" | ||
| ["entry"] = "entryValue" | ||
| "element" | ||
| } | ||
| } | ||
| ["applying to Typed object"] { | ||
| new { | ||
| name = "Modified" | ||
| age = 100 | ||
| } | ||
| } | ||
| ["chaining mixins"] { | ||
| new { | ||
| base = "start" | ||
| extra = "value" | ||
| another = "field" | ||
| } | ||
| } | ||
| ["reusable mixin"] { | ||
| new { | ||
| first { | ||
| id = 1 | ||
| shared = "config" | ||
| } | ||
| second { | ||
| id = 2 | ||
| shared = "config" | ||
| } | ||
| } | ||
| } | ||
| ["overriding properties"] { | ||
| new { | ||
| name = "Override" | ||
| value = 999 | ||
| other = "keep" | ||
| } | ||
| } | ||
| ["replacement vs merge for nested objects"] { | ||
| new { | ||
| a1 { | ||
| b2 = 2 | ||
| } | ||
| a2 { | ||
| b1 = 2 | ||
| b2 = 2 | ||
| } | ||
| } | ||
| } | ||
| } | 
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              
  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.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a sound assumption to make. Mappings and Dynamics can have entries with Int (in Pkl, Long in Java) keys. Probably better to check
member.isElement()here instead (and you can then assume the key is a Long).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would be the internal field representation, not the userland defined keys. I will see if I can get the key to be the right type at compile time and replace this check