All Downloads are FREE. Search and download functionalities are using the official Maven repository.

rythm-swift.record.txt Maven / Gradle / Ivy

The newest version!
@args com.linkedin.pegasus.generator.spec.RecordTemplateSpec record, org.coursera.courier.swift.SwiftSyntax syntax, org.coursera.courier.swift.SwiftyJSON swifty
@import com.linkedin.pegasus.generator.spec.ClassTemplateSpec
@import com.linkedin.pegasus.generator.spec.RecordTemplateSpec.Field
@import com.linkedin.pegasus.generator.spec.PrimitiveTemplateSpec
@import com.linkedin.pegasus.generator.spec.MapTemplateSpec
@import com.linkedin.pegasus.generator.spec.UnionTemplateSpec
@import com.linkedin.pegasus.generator.spec.ArrayTemplateSpec
@import org.coursera.courier.api.ClassTemplateSpecs
@import org.coursera.courier.schema.TypedDefinitions
@import com.linkedin.data.schema.DataSchema.Type

@maybePackageAndImports(record)

@{ String className = syntax.escapeKeyword(record.getClassName()) }

@classAnnotations(record.getSchema())
public struct @className: Serializable@if (syntax.isEquatable()) {, Equatable} {
  @for(Field field: record.getFields()) {
    @{ String fieldName = syntax.escapeKeyword(field.getSchemaField().getName()) }
    @fieldAnnotations(field)
    public let @fieldName: @syntax.toType(field.getType(), syntax.isOptional(field))
  }

  public init(@for(Field field: record.getFields()) {
    @{String fieldName = syntax.escapeKeyword(field.getSchemaField().getName())}
    @{String defaultLiteral = syntax.defaultToLiteral(field)}
    @(fieldName): @(syntax.toType(field.getType(), syntax.isOptional(field)))@if(defaultLiteral != null){ = @defaultLiteral}@if(!field_isLast){,}
  }) {
    @for(Field field: record.getFields()) {
      @{ String fieldName = syntax.escapeKeyword(field.getSchemaField().getName()) }
      self.@fieldName = @fieldName
    }
  }

  @enclosedTypes(record, syntax)

  public static func readJSON(json: JSON) throws -> @className {
    return @(className)(
      @for(Field field: record.getFields()) {
      @(syntax.escapeKeyword(field.getSchemaField().getName())): @(swifty.toGetAccessor("json[\"" + field.getSchemaField().getName() + "\"]", field))@if(!field_isLast){,}
      })
  }

  public func writeData() -> [String: AnyObject] {
    @if(record.getFields().size() == 0) {
      return [:]
    } else {
      var dict: [String : AnyObject] = [:]

      @for(Field field: record.getFields()) {
        @{String fieldName = syntax.escapeKeyword(field.getSchemaField().getName())}
        @if(syntax.isOptional(field)) {
          if let @fieldName = self.@(fieldName) {
            dict["@(field.getSchemaField().getName())"] = @swifty.toSetAccessor(fieldName, field)
          }
        } else {
          dict["@(field.getSchemaField().getName())"] = @swifty.toSetAccessor(fieldName, field)
        }
      }

      return dict
    }
  }

  @* TODO: implement
  public var hashValue: Int {
    var hash = 1
    @for(Field field: record.getFields()) {
    @{ String fieldName = syntax.escapeKeyword(field.getSchemaField().getName()) }
      hash = hash * 17 + hashOf(self.@fieldName)
    }
    return hash
  }
  *@
}

@if (syntax.isEquatable()) {
public func ==(lhs: @className, rhs: @className) -> Bool {
    return (
      @for(Field field: record.getFields()) { @{ String fieldName = syntax.escapeKeyword(field.getSchemaField().getName()) }
        @if (syntax.isOptional(field)) {(lhs.@fieldName == nil ? (rhs.@fieldName == nil) : lhs.@fieldName! == rhs.@fieldName!)} else {lhs.@fieldName == rhs.@fieldName} &&
      } true
    )
}
}

@enclosedTypeGlobals(record, className, syntax)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy