ComplexValue

open class ComplexValue : StructureBase, ComplexValueOrList

Encapsulates an OData complex value.

  • Decode the encoded data from the Decoder object and insantiate a new ComplexValue object from it.

    See also

    ComplexValue.encode(to encoder: Encoder) for usage examples how to encode and decode ComplexValue.

    Declaration

    Swift

    public required init(from decoder: Decoder) throws

    Parameters

    from

    decoder object which contains the original object’s serialized data in a container instance

  • Encode the object instance

    See also

    EntityValue.decode(to encoder: Encoder)

    Example of encode/decode of a ComplexValue using proxy classes

    open func encodeComplexValueExample() throws -> Void {
    let provider = OnlineODataProvider(serviceName: "TripPinService",
        serviceRoot: "http://services.odata.org/TripPinRESTierService/")
    let service = TripPinServiceProxy(provider: provider)
    
    let query = DataQuery()
    let complexValueToBeEncoded = try service.fetchPeople(matching: query).first?.addressInfo.first //get a ComplexValue
    // Encode the EntityValue with JSONEncoder (other encoder exists such as PropertyListEncoder)
    let encoder = JSONEncoder()
    if let encodedComplexValue = try? encoder.encode(complexValueToBeEncoded!) {
        if let json = String(data: encodedComplexValue, encoding: .utf8){
            print(json)
         }
        // Decoding
        // JSONDecoder is used, but other decoders are available, such as PropertyListDecoder
        let decoder = JSONDecoder()
        // Add typeInfo to the decoder
        decoder.userInfo[CSDLDocument.csdlInfoKey] = service.metadata
        //Decode
        let decodedComplexValue = try? decoder.decode(ComplexValue.self, from: encodedComplexValue)
    }
    }
    

    Example of encode/decode of a ComplexValue using dynamic API

    open func encodeComplexValueExample() throws -> Void {
        let provider = OnlineODataProvider(serviceName: "TripPinService",
            serviceRoot: "http://services.odata.org/TripPinRESTierService/")
        let service = TripPinServiceProxy(provider: provider)
    
        // Explicitly load metadata, then it must be cached manually.
        // The reason is, that type infos will not be implicitly encoded, but it will be needed at the encoding.
            // The dataservice's metadata contains all such type infos, so metadata will be first loaded then cached.
            service.serviceOptions.csdlOptions = ( service.serviceOptions.csdlOptions |
             CSDLOption.retainResolvedText | CSDLOption.retainOriginalText )
            try service.loadMetadata()
            let metadataCachedAsString = service.metadata.originalText! //
    
           // Get the entity to be encoded by dynamic API
            let peopleEntitySet = service.entitySet(withName: "People")
            let query = DataQuery().from(peopleEntitySet)
            let entityValue = try service.executeQuery(query).entityList().first()
            let personEntityType = service.metadata.entityType(withName: "Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person")
            let propType = personEntityType.property(withName: "AddressInfo")
           let complexValueToBeEncoded = propType.complexList(from: entityValue).first() //get a ComplexValue
    
            // Encode the EntityValue with JSONEncoder (other encoder exists such as PropertyListEncoder)
            let encoder = JSONEncoder()
            if let encodedComplexValue = try? encoder.encode(complexValueToBeEncoded) {
             if let json = String(data: encodedComplexValue, encoding: .utf8){
                 print(json) // Check
            }
    
             // Decoding
             // JSONDecoder is used, but other decoders are available, such as PropertyListDecoder
            let decoder = JSONDecoder()
    
             // Get type infos from cached metadata parsing it to CSDLDocument
             let parser: CSDLParser = CSDLParser()
             parser.csdlOptions = (CSDLOption.processMixedVersions | CSDLOption.retainOriginalText |
                 CSDLOption.resolveUndefinedTerms)
             var csdlDocument: CSDLDocument = parser.parseInProxy(metadataCachedAsString, url: "")
    
             // Add typeInfo to the decoder
            decoder.userInfo[CSDLDocument.csdlInfoKey] = csdlDocument
    
             // Decode
             let decodedComplexValue = try? decoder.decode(ComplexValue.self, from: encodedComplexValue)
            }
    }
    

    Declaration

    Swift

    public override func encode(to encoder: Encoder) throws

    Parameters

    to

    encoder object which can be used to serialize the object’s data one by one into a container instance

  • Contains the original values for all structural properties of this complex value. Used by DataService.updateEntity to determine the changed properties for PATCH requests.

    Declaration

    Swift

    final public var oldComplex: ComplexValue?
  • Construct a new complex value. This constructor is intended for use by custom complex value subclasses. Use ofType for regular construction.

    Declaration

    Swift

    public init(withDefaults: Bool = false, type: ComplexType? = nil, withIndexMap: SparseIndexMap? = nil)

    Parameters

    withDefaults

    Should setDefaultValues be called to initialize properties with default values?

    type

    Complex type. Should not be nil except when called by generated proxy base classes.

    withIndexMap

    If non-null, the created entities will use a sparse array implementation.

  • The complex type metadata for this value (dataType cast to ComplexType).

    Declaration

    Swift

    open var complexType: ComplexType { get }
  • Declaration

    Swift

    open func copyComplex() -> ComplexValue

    Return Value

    A copy of this complex value (only structural properties are copied).

  • Data type with a DataType.code of DataType.COMPLEX_VALUE.

    Declaration

    Swift

    override open var dataType: DataType { get }
  • Declaration

    Swift

    open class func equal(a: ComplexValue?, b: ComplexValue?) -> Bool

    Parameters

    a

    First complex value.

    b

    Second complex value.

    Return Value

    true if two complex values have equal structural properties.

  • Construct a new complex value of the specified type.

    Declaration

    Swift

    open class func ofType(_ type: ComplexType, sparse: SparseIndexMap? = nil) -> ComplexValue

    Parameters

    type

    Complex type for the new value.

    sparse

    Sparse index map. For internal use.

    Return Value

    A new complex value.

  • Convert this data value to a string. If the dataType is defined by XML Schema Part 2: Datatypes, then the corresponding lexical format is used. JSON format is used for structured values (arrays and objects).

    Declaration

    Swift

    override open func toString() -> String

    Return Value

    Lexical representation of this data value.

  • Data type code of the wrapped value, equivalent to dataType.code.

    Declaration

    Swift

    override open var typeCode: Int { get }