Post

서버 응답 json 제네릭 타입으로 디코딩 불가 이슈

Node 구조체의 멤버변수 Property의 타입이 제네릭이어서 로 했으나 서버에서 응답된 json은 제네릭 타입으로 변경 불가능. → 노션 속성의 다양한 타입을 서버에서 어떤 속성이든 한가지 형태의 객체로 재구조화하여 대응. Node struct의 멤버변수도 서버에서 정의한 객체의 형태로 변경하여 디코딩 가능하도록 변경.


  • Node struct 변경 전

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    
      struct Node {
           let id: String
           let icon: String?
           let cover: String?
           let title: String?
           let property: [Property<Any>]
           var rect: CGRect
        
       }
         
         
       //MARK: - 속성 model
       struct Property<T> {
           let name: String
           let type: PropertyType
           let value: T
       }
    
  • Node struct 변경 후

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
struct Node {
    let id: String
    let parrentId: String?
    let icon: String?
    let cover: String?
    let title: String?
    let property: [Property]
    var rect: CGRect = .zero
    
    
    
    mutating func setRect(rect: CGRect) {
        self.rect = rect
    }
    
}


//MARK: - 속성 model
struct Property {
    let name: String
    let type: PropertyType
    let value: [ValueType]
}

struct ValueType {
    let id: String
    let name: String
    let color: String
}

enum PropertyType {
    case    checkbox, // bool
            email,
            date,
            formula,
            multi_select, // array
            number,
            people,
            phone_number,
            relation, // array
            rich_text,
            select,
            status,
            title,
            url
}


This post is licensed under CC BY 4.0 by the author.