1
+ // AXModels.swift - Contains Codable data models for the AXHelper utility
2
+
3
+ import Foundation
4
+
5
+ // Original models from the older main.swift version, made public
6
+ public struct CommandEnvelope : Codable {
7
+ public enum Verb : String , Codable { case query, perform }
8
+ public let cmd : Verb
9
+ public let locator : Locator
10
+ public let attributes : [ String ] ? // for query
11
+ public let action : String ? // for perform
12
+ public let multi : Bool ? // NEW in that version
13
+ public let requireAction : String ? // NEW in that version
14
+ // Added new fields from more recent versions
15
+ public let debug_logging : Bool ?
16
+ public let max_elements : Int ?
17
+ public let output_format : String ?
18
+ }
19
+
20
+ public struct Locator : Codable {
21
+ public let app : String
22
+ public let role : String
23
+ public let match : [ String : String ]
24
+ public let pathHint : [ String ] ?
25
+ }
26
+
27
+ public struct QueryResponse : Codable {
28
+ public let attributes : [ String : AnyCodable ]
29
+ public var debug_logs : [ String ] ? // Added
30
+
31
+ public init ( attributes: [ String : Any ] , debug_logs: [ String ] ? = nil ) { // Updated init
32
+ self . attributes = attributes. mapValues ( AnyCodable . init)
33
+ self . debug_logs = debug_logs
34
+ }
35
+ }
36
+
37
+ public struct MultiQueryResponse : Codable {
38
+ public let elements : [ [ String : AnyCodable ] ]
39
+ public var debug_logs : [ String ] ? // Added
40
+
41
+ public init ( elements: [ [ String : Any ] ] , debug_logs: [ String ] ? = nil ) { // Updated init
42
+ self . elements = elements. map { element in
43
+ element. mapValues ( AnyCodable . init)
44
+ }
45
+ self . debug_logs = debug_logs
46
+ }
47
+ }
48
+
49
+ public struct PerformResponse : Codable {
50
+ public let status : String
51
+ public var debug_logs : [ String ] ? // Added
52
+ }
53
+
54
+ public struct ErrorResponse : Codable {
55
+ public let error : String
56
+ public var debug_logs : [ String ] ? // Added
57
+ }
58
+
59
+ // Added new response type from more recent versions
60
+ public struct TextContentResponse : Codable {
61
+ public let text_content : String
62
+ public var debug_logs : [ String ] ?
63
+ }
64
+
65
+ // AnyCodable wrapper type for JSON encoding of Any values
66
+ public struct AnyCodable : Codable {
67
+ public let value : Any
68
+
69
+ public init ( _ value: Any ) {
70
+ self . value = value
71
+ }
72
+
73
+ public init ( from decoder: Decoder ) throws {
74
+ let container = try decoder. singleValueContainer ( )
75
+
76
+ if container. decodeNil ( ) {
77
+ self . value = NSNull ( )
78
+ } else if let bool = try ? container. decode ( Bool . self) {
79
+ self . value = bool
80
+ } else if let int = try ? container. decode ( Int . self) {
81
+ self . value = int
82
+ } else if let double = try ? container. decode ( Double . self) {
83
+ self . value = double
84
+ } else if let string = try ? container. decode ( String . self) {
85
+ self . value = string
86
+ } else if let array = try ? container. decode ( [ AnyCodable ] . self) {
87
+ self . value = array. map { $0. value }
88
+ } else if let dict = try ? container. decode ( [ String : AnyCodable ] . self) {
89
+ self . value = dict. mapValues { $0. value }
90
+ } else {
91
+ throw DecodingError . dataCorruptedError (
92
+ in: container,
93
+ debugDescription: " AnyCodable cannot decode value "
94
+ )
95
+ }
96
+ }
97
+
98
+ public func encode( to encoder: Encoder ) throws {
99
+ var container = encoder. singleValueContainer ( )
100
+
101
+ switch value {
102
+ case is NSNull :
103
+ try container. encodeNil ( )
104
+ case let bool as Bool :
105
+ try container. encode ( bool)
106
+ case let int as Int :
107
+ try container. encode ( int)
108
+ case let double as Double :
109
+ try container. encode ( double)
110
+ case let string as String :
111
+ try container. encode ( string)
112
+ case let array as [ Any ] :
113
+ try container. encode ( array. map ( AnyCodable . init) )
114
+ case let dict as [ String : Any ] :
115
+ try container. encode ( dict. mapValues ( AnyCodable . init) )
116
+ default :
117
+ try container. encode ( String ( describing: value) )
118
+ }
119
+ }
120
+ }
121
+
122
+ public typealias ElementAttributes = [ String : Any ]
0 commit comments