This repository was archived by the owner on Mar 2, 2022. It is now read-only.
  
  
  
  
    
    
    
      
    
  
  
    
File tree Expand file tree Collapse file tree 2 files changed +73
-0
lines changed 
main/scala/reactor/util/scala/context 
test/scala/reactor/util/scala/context Expand file tree Collapse file tree 2 files changed +73
-0
lines changed Original file line number Diff line number Diff line change 1+ package  reactor .util .scala .context 
2+ 
3+ import  reactor .util .context .Context 
4+ 
5+ import  scala .reflect .ClassTag 
6+ 
7+ object  SContext  {
8+   implicit  class  ContextOps (context : Context ) {
9+ 
10+     /**  
11+       * Resolve a value given a key within the [[Context ]]. 
12+       * <p/> 
13+       * <b>Important:</b> Unlike the basic Context methods, 
14+       * this will actually check the type of the value against the given type parameter, 
15+       * and return [[None ]] if the value is not of a compatible type. 
16+       *  
17+       * @param  key  a lookup key to resolve the value within the context 
18+       * @return  [[Some ]] value for that key, 
19+       *         or [[None ]] if there is no such key (or if its value is the wrong type). 
20+       */  
21+     def  getOrNone [T ](key : Any )(implicit  valueType : ClassTag [T ]):  Option [T ] = 
22+       context.getOrDefault[Any ](key, None ) match  {
23+         case  value: T  =>  Some (value)
24+         case  _ =>  None 
25+       }
26+   }
27+ }
Original file line number Diff line number Diff line change 1+ package  reactor .util .scala .context 
2+ 
3+ import  org .scalatest .freespec .AnyFreeSpec 
4+ import  org .scalatest .matchers .should .Matchers 
5+ import  reactor .core .scala .publisher .SMono 
6+ import  reactor .core .scala .publisher .model .TestSupport 
7+ import  reactor .test .StepVerifier 
8+ import  SContext ._ 
9+ 
10+ class  SContextTest  extends  AnyFreeSpec  with  Matchers  with  TestSupport  {
11+   " SContext" -  {
12+     " .getOrNone " -  {
13+       " with missing key should return None" 
14+         StepVerifier .create(
15+           SMono .subscriberContext
16+                .map{ _.getOrNone(" BOGUS" 
17+         ).expectNext(None )
18+          .verifyComplete()
19+       }
20+       " with value of incompatible type should return None" 
21+         StepVerifier .create(
22+           SMono .subscriberContext
23+                .map{ _.getOrNone[Sedan ](" whatever key" 
24+                .subscriberContext{ _.put(" whatever key" Truck (76254 ))}
25+         ).expectNext(None )
26+          .verifyComplete()
27+       }
28+       " with value of exactly the expected type should return Some value" 
29+         StepVerifier .create(
30+           SMono .subscriberContext
31+                .map{ _.getOrNone[Truck ](" whatever key" 
32+                .subscriberContext{ _.put(" whatever key" Truck (76254 ))}
33+         ).expectNext(Some (Truck (76254 )))
34+          .verifyComplete()
35+       }
36+       " with value of a supertype of the expected type should return Some value" 
37+         StepVerifier .create(
38+           SMono .subscriberContext
39+                .map{ _.getOrNone[Vehicle ](true ) }
40+                .subscriberContext{ _.put(true , Truck (76254 ))}
41+         ).expectNext(Some (Truck (76254 )))
42+          .verifyComplete()
43+       }
44+     }
45+   }
46+ }
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments