Skip to content

Commit 82543cd

Browse files
authored
Add OGGraphArchiveJSON2 and update OGDescription (#137)
* Fix conflict with ObjectiveC nil * Add OGGraphArchiveJSON2 and update OGDescription * Add archiveJSON implementation in Swift * Fix OGDescriptionFormatDictionary * Fix link issue on Linux * Workaround Linux compiler issue * Update archiveJSON Swift implementation * Fix missing OGGraphDescription.h include * Fix OGGraphArchiveJSON API link issue
1 parent e32d95d commit 82543cd

File tree

18 files changed

+189
-54
lines changed

18 files changed

+189
-54
lines changed

Package.resolved

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ let openGraphSPITarget = Target.target(
147147
name: "OpenGraph_SPI",
148148
cSettings: sharedCSettings + [
149149
.define("__COREFOUNDATION_FORSWIFTFOUNDATIONONLY__", to: "1", .when(platforms: .nonDarwinPlatforms)),
150+
],
151+
linkerSettings: [
152+
.linkedLibrary("z"),
150153
]
151154
)
152155
let openGraphShimsTarget = Target.target(

Sources/OpenGraph/Graph/Graph.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,33 @@ extension Graph {
7171
@inlinable
7272
public static func setOutputValue<Value>(_ value: UnsafePointer<Value>)
7373
}
74+
75+
#if canImport(Darwin)
76+
import Foundation
77+
#endif
78+
79+
extension Graph {
80+
public func archiveJSON(name: String?) {
81+
#if canImport(Darwin)
82+
let options: NSDictionary = [
83+
Graph.descriptionFormat: "graph/dict",
84+
Graph.descriptionIncludeValues: true,
85+
]
86+
guard let description = Graph.description(self, options: options) as? [String: Any] else {
87+
return
88+
}
89+
var name = name ?? "graph"
90+
name.append(".ag-json")
91+
let path = (NSTemporaryDirectory() as NSString).appendingPathComponent(name)
92+
guard let data = try? JSONSerialization.data(withJSONObject: description, options: []) else {
93+
return
94+
}
95+
let url = URL(fileURLWithPath: path)
96+
do {
97+
try data.write(to: url, options: [])
98+
print("Wrote graph data to \"\(path)\".")
99+
} catch {
100+
}
101+
#endif
102+
}
103+
}

Sources/OpenGraphShims/Graph+Debug.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import Foundation
99
@_spi(Debug)
1010
extension Graph {
1111
public var dict: [String: Any]? {
12-
let options = ["format": "graph/dict"] as NSDictionary
12+
let options = [
13+
Graph.descriptionFormat: Graph.descriptionFormatDictionary
14+
] as NSDictionary
1315
guard let description = Graph.description(nil, options: options) else {
1416
return nil
1517
}
@@ -25,7 +27,9 @@ extension Graph {
2527
// color:
2628
// - red: is_changed
2729
public var dot: String? {
28-
let options = ["format": "graph/dot"] as NSDictionary
30+
let options = [
31+
Graph.descriptionFormat: Graph.descriptionFormatDot
32+
] as NSDictionary
2933
guard let description = Graph.description(self, options: options)
3034
else {
3135
return nil

Sources/OpenGraph_SPI/Attribute/AttributeID.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class AttributeID final {
2222
enum class Kind: uint8_t {
2323
Direct = 0x0,
2424
Indirect = 0x1,
25-
Nil = 0x2,
25+
Null = 0x2,
2626
};
2727
private:
2828
uint32_t _rawValue;
@@ -59,8 +59,8 @@ class AttributeID final {
5959
}
6060

6161
OG_INLINE OG_CONSTEXPR
62-
const bool isNil() const OG_NOEXCEPT {
63-
return getKind() == Kind::Nil;
62+
const bool isNull() const OG_NOEXCEPT {
63+
return getKind() == Kind::Null;
6464
}
6565

6666
OG_INLINE OG_CONSTEXPR

Sources/OpenGraph_SPI/Attribute/OGAttribute.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "../Util/assert.hpp"
1212
#include <optional>
1313

14-
const OGAttribute OGAttributeNil = OGAttribute(OG::AttributeID::Kind::Nil);
14+
const OGAttribute OGAttributeNil = OGAttribute(OG::AttributeID::Kind::Null);
1515

1616
OGAttribute OGGraphGetCurrentAttribute() {
1717
// TODO

Sources/OpenGraph_SPI/Debug/og-debug-server.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@
202202
NSString *command = dic[@"command"];
203203
if ([command isEqual:@"graph/description"]) {
204204
NSMutableDictionary *mutableDic = [NSMutableDictionary dictionaryWithDictionary:dic];
205-
mutableDic[(__bridge NSString *)OGDescriptionFormat] = @"graph/dict";
205+
mutableDic[(__bridge NSString *)OGDescriptionFormat] = (__bridge NSString *)OGDescriptionFormatDictionary;
206206
CFTypeRef description = OG::Graph::description(nullptr, mutableDic);
207207
if (description) {
208208
NSData *descriptionData = [NSJSONSerialization dataWithJSONObject:(__bridge id)description options:0 error:NULL];

Sources/OpenGraph_SPI/Graph/Graph.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
//
22
// Graph.cpp
3-
//
4-
//
5-
// Created by Kyle on 2024/1/18.
6-
//
3+
// OpenGraph_SPI
74

85
#include "Graph.hpp"
96
#include "Subgraph.hpp"
7+
#include "OGGraphDescription.h"
108

119
#if !OG_TARGET_OS_WASI
1210
#include <dispatch/dispatch.h>
1311
#endif
12+
1413
#include <pthread.h>
1514

1615
pthread_key_t OG::Graph::_current_update_key;
1716

1817
OG::Graph::Graph() OG_NOEXCEPT {
1918
// TODO
20-
19+
2120
// libdispatch is not supported on WASI
2221
// Tracked via https://github.com/swiftwasm/swift/issues/5565
2322
#if !OG_TARGET_OS_WASI
@@ -27,7 +26,7 @@ OG::Graph::Graph() OG_NOEXCEPT {
2726
OG::Subgraph::make_current_subgraph_key();
2827
});
2928
#endif
30-
29+
3130
// TODO
3231
}
3332

@@ -48,10 +47,6 @@ void OG::Graph::stop_profiling() OG_NOEXCEPT {
4847
// TODO
4948
}
5049

51-
void OG::Graph::write_to_file(const Graph * _Nullable, const char * _Nullable) OG_NOEXCEPT {
52-
// TODO
53-
}
54-
5550
const bool OG::Graph::thread_is_updating() const OG_NOEXCEPT {
5651
void *current = pthread_getspecific(current_key());
5752
if (!current) {

Sources/OpenGraph_SPI/Graph/Graph.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
//
22
// Graph.hpp
3-
//
4-
//
5-
// Created by Kyle on 2024/1/18.
6-
//
3+
// OpenGraph_SPI
4+
75

86
#ifndef Graph_hpp
97
#define Graph_hpp
@@ -122,9 +120,11 @@ class Graph final {
122120
static void all_stop_profiling() OG_NOEXCEPT;
123121
void start_profiling(uint32_t) OG_NOEXCEPT;
124122
void stop_profiling() OG_NOEXCEPT;
125-
126-
static void write_to_file(const Graph * _Nullable, const char * _Nullable) OG_NOEXCEPT;
127-
123+
124+
#if OG_OBJC_FOUNDATION
125+
static void write_to_file(const Graph * _Nullable, const char * _Nullable, uint8_t) OG_NOEXCEPT;
126+
#endif
127+
128128
const bool thread_is_updating() const OG_NOEXCEPT;
129129
const bool is_context_updating(const OG::Graph::Context&) const OG_NOEXCEPT;
130130

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//
2+
// Graph.mm
3+
// OpenGraph_SPI
4+
5+
#include "Graph.hpp"
6+
#include "OGGraphDescription.h"
7+
8+
#if OG_OBJC_FOUNDATION
9+
#include <Foundation/Foundation.h>
10+
#include <zlib.h>
11+
12+
void OG::Graph::write_to_file(const Graph * _Nullable graph, const char * _Nullable name, uint8_t options) OG_NOEXCEPT {
13+
@autoreleasepool {
14+
NSDictionary <NSString *, id> *options_dict = @{
15+
(__bridge NSString *)OGDescriptionFormat: (__bridge NSString *)OGDescriptionFormatDictionary,
16+
(__bridge NSString *)OGDescriptionIncludeValues: @((options & 1) == 0),
17+
(__bridge NSString *)OGDescriptionAllGraphs: @(graph == nil)
18+
};
19+
NSString *description = (__bridge NSString *)Graph::description(graph, options_dict);
20+
if (description == nil) {
21+
return;
22+
}
23+
const char *cstring_name = name ?: "graph.ag-gzon";
24+
25+
NSData *data = [NSJSONSerialization dataWithJSONObject:description options:0 error:nil];
26+
NSString *file_path = [NSString stringWithUTF8String:cstring_name];
27+
if (cstring_name[0] != '/') {
28+
file_path = [NSTemporaryDirectory() stringByAppendingPathComponent:file_path];
29+
}
30+
NSError *error = nil;
31+
BOOL success = YES;
32+
if ([file_path.pathExtension isEqualToString:@"ag-gzon"]) {
33+
gzFile file = gzopen(file_path.fileSystemRepresentation, "wb");
34+
if (file != NULL) {
35+
const char *bytes = (const char *)[data bytes];
36+
NSUInteger remaining = [data length];
37+
while (remaining != 0) {
38+
int bytes_written = gzwrite(file, bytes, (unsigned int)remaining);
39+
if (bytes_written <= 0) {
40+
success = NO;
41+
break;
42+
}
43+
bytes += bytes_written;
44+
remaining -= bytes_written;
45+
}
46+
gzclose(file);
47+
}
48+
} else {
49+
success = [data writeToFile:file_path options:0 error:&error];
50+
}
51+
if (success) {
52+
fprintf(stderr, "Wrote graph data to \"%s\".\n", file_path.UTF8String);
53+
} else {
54+
fprintf(stderr, "Unable to write to \"%s\": %s\n", file_path.UTF8String, error.description.UTF8String);
55+
}
56+
}
57+
}
58+
59+
#endif /* OG_OBJC_FOUNDATION */

0 commit comments

Comments
 (0)