Upgrade to Pro — share decks privately, control downloads, hide ads and more …

jq at the Shortcuts

cockscomb
December 22, 2022

jq at the Shortcuts

Presented @ potatotips#80

cockscomb

December 22, 2022
Tweet

More Decks by cockscomb

Other Decks in Programming

Transcript

  1. jq

  2. gh Scripting with GitHub CLI | The GitHub Blog $

    gh api ... -- jq '.[].login' # = > "user1" # = > "user2" # = > ...
  3. URL

  4. jq

  5. package binding import ( "github.com/itchyny/gojq" _ "golang.org/x/mobile/bind" ) type Query

    struct { query *gojq.Query } func NewQuery(src string) (*Query, error) { query, err := gojq.Parse(src) if err != nil { return nil, err } return &Query{query: query}, nil } func (q *Query) Run(input []byte) (*Iterator, error) { ... }
  6. $ gomobile bind \ -target=ios,iossimulator,macos,maccatalyst \ -iosversion 14 \ -prefix

    GOJQ \ -o Frameworks/GOJQBinding.xcframework \ github.com/cockscomb/swift-gojq/binding
  7. import Foundation open class GOJQBindingIterator : NSObject, goSeqRefInterface { public

    init() open func next() throws -> Data } open class GOJQBindingQuery : NSObject, goSeqRefInterface { public init?(_ src: String?) open func run(_ input: Data?) throws -> GOJQBindingIterator } public func GOJQBindingNewQuery(_ src: String?, _ error: NSErrorPointer) -> GOJQBindingQuery?
  8. import GOJQBinding enum QueryError: Error { case unknown } public

    struct Query { private let binding: GOJQBindingQuery public init(_ query: String) throws { var error: NSError? guard let binding = GOJQBindingNewQuery(query, &error) else { throw error ?? QueryError.unknown } self.binding = binding } public func run(_ input: Data) throws -> AsyncThrowingStream<Data, any Error> { ... } }
  9. // swift-tools-version: 5.7 import PackageDescription let package = Package( name:

    "SwiftGoJq", platforms: [ .macOS(.v13), .macCatalyst(.v14), .iOS(.v14) ], products: [ .library(name: “SwiftGoJq”, targets: ["SwiftGoJq"]), ], dependencies: [], targets: [ .binaryTarget( name: "GOJQBinding", url: "https://github.com/cockscomb/swift-gojq/releases/download/0.1.0/ GOJQBinding.xcframework.zip", checksum: "1c45710de17fb7020dcfc75105344729725c5e3875e7058e98790e5f4e178162"), .target( name: "SwiftGoJq", dependencies: [ "GOJQBinding", ]), ] )
  10. import AppIntents import AsyncAlgorithms import SwiftGoJq struct JQIntent: AppIntent {

    static var title: LocalizedStringResource = "jq" @Parameter(title: "JSON") var input: String @Parameter(title: "Query") var query: String static var parameterSummary: some ParameterSummary { Summary("\(\.$input) | jq '\(\.$query)'") } func perform() async throws -> some IntentResult { let jq = try Query(query) let results = try jq.run(input) let array = try await Array(results) return .result(value: array) } }
  11. WEB+DB PRESS vol. 1 3 2 iOS 1 6 12/24

    3 id:cockscomb id:yutailang 01 19 id:kouki_dan