SHARE
TWEET

Untitled

a guest Jul 22nd, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  Malygos.swift
  3. //  Malygos
  4. //
  5. //  Created by xing jian on 11/7/2019.
  6. //  Copyright © 2019 xingjian. All rights reserved.
  7. //
  8.  
  9. import Foundation
  10.  
  11. enum JoinType{
  12.   case path
  13.   case query
  14. }
  15. public protocol NetBean {
  16.   static func createBean() -> NetBean?
  17. }
  18. extension NetBean{
  19.   static func createBean() -> NetBean? {
  20.     return nil
  21.   }
  22. }
  23. protocol NetFunction {
  24.   var beanPath:[(String,String?)]{get}
  25.   var beanQuery:[(String,String?)]{get}
  26.   func fillPathAndQuery(_ obj:Any)
  27.   func paras(_ path:String,_ factor:Character)->[(String,String?)]
  28. }
  29. extension NetFunction{
  30.   func paras(_ path:String,_ factor:Character)->[(String,String?)]{
  31.     guard path.count > 0 else{
  32.       return []
  33.     }
  34.    
  35.     let tuple = path.split(separator: factor).compactMap { item -> String? in
  36.       if item.count > 0 {
  37.         return String(item)
  38.       }
  39.       return nil
  40.       }.map { item -> (String,String?) in
  41.         let pathitem = item.trimmingCharacters(in: .whitespaces)
  42.         let findex = pathitem.firstIndex(of: "{")
  43.         guard findex != nil else{
  44.           return (pathitem,pathitem)
  45.         }
  46.         let lindex = pathitem.lastIndex(of: "}")
  47.         guard lindex != nil else{
  48.           return (pathitem,pathitem)
  49.         }
  50.        
  51.         let varname =  item[item.index(findex!, offsetBy:1)..<lindex!]
  52.         return (String(varname),nil)
  53.     }
  54.     return tuple
  55.   }
  56. }
  57. extension NetFunction{
  58.   func fillPathAndQuery(_ obj:Any){
  59.     func join(_ items:[(String,String?)],_ variables:[[String:Any]],_ type:JoinType) -> String{
  60.       guard items.count > 0 else {
  61.         return ""
  62.       }
  63.       let result = items.compactMap { item -> String? in
  64.         guard item.1 == nil else{
  65.           return item.0
  66.         }
  67.         var value :Any? = nil
  68.         for varitem in variables{
  69.           if let item = varitem[item.0]{
  70.             value = item
  71.             break;
  72.           }
  73.         }
  74.         if value != nil {
  75.           if  let vstring = value as? String{
  76.             return type == .path ? String(vstring) : String("\(item.0)=\(vstring)")
  77.           }
  78.           if let istring = value as? Int{
  79.             return type == .path ? String(istring) : String("\(item.0)=\(istring)")
  80.           }
  81.           return nil
  82.         }
  83.         return nil
  84.         }.joined(separator: type == .path ? "/":"&")
  85.       return result
  86.     }
  87.    
  88.    
  89.     let ref = Mirror(reflecting:obj.self)
  90.     let variables = ref.children.compactMap{ child ->[String:Any]? in
  91.       if let lable = child.label{
  92.         return [lable:child.value]
  93.       }
  94.       return nil
  95.     }
  96.     let newpath = join(beanPath, variables, .path)
  97.     let newquery = join(beanQuery, variables, .query)
  98.   }
  99. }
  100. @propertyDelegate
  101. public struct Get<Bean:NetBean>:NetFunction{
  102.   private var valueInternal:Bean? = nil
  103.   private(set) var beanPath:[(String,String?)] = []
  104.   private(set) var beanQuery:[(String,String?)] = []
  105.   public var value:Bean{
  106.     set(newValue){valueInternal = newValue}
  107.     get{
  108.       let bean = valueInternal ?? Bean.createBean()! as! Bean
  109.       fillPathAndQuery(bean)
  110.       return  bean
  111.     }
  112.   }
  113.   public init(path:String="",query:String=""){
  114.     beanPath = paras(path,"/")
  115.     beanQuery = paras(query,"&")
  116.   }
  117. }
  118. @propertyDelegate
  119. public struct Post<Bean:NetBean> {
  120.   private var valueInternal:Bean? = nil
  121.   public var value:Bean{
  122.     set(newValue){ valueInternal = newValue}
  123.     get{return valueInternal ?? Bean.createBean()! as! Bean}
  124.   }
  125.   public init(){
  126.   }
  127. }
  128. @propertyDelegate
  129. public struct Put<Bean:NetBean> {
  130.   private var valueInternal:Bean? = nil
  131.   public var value:Bean{
  132.     set(newValue){ valueInternal = newValue}
  133.     get{return valueInternal ?? Bean.createBean()! as! Bean}
  134.   }
  135.   public init(){
  136.   }
  137. }
  138. @propertyDelegate
  139. public struct Delete<Bean:NetBean> {
  140.   private var valueInternal:Bean? = nil
  141.   public var value:Bean{
  142.     set(newValue){ valueInternal = newValue}
  143.     get{return valueInternal ?? Bean.createBean()! as! Bean}
  144.   }
  145.   public init(){
  146.   }
  147. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top