Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RUMM-2266 Reorganise rum-models-generator package for Session Replay code generation #940

Merged
merged 2 commits into from
Jul 26, 2022

Conversation

ncreated
Copy link
Member

What and why?

📦 This PR reorganises rum-models-generator package so it can be later easily extended with Session Replay code generation pipeline. This is to clean up the package code and keep both generation tracks separate.

It doesn't yet generate Session Replay models - this requires abstracting some configuration (e.g. the access control in generated code) and will be added in next PR.

How?

The package is now composed of 3 targets:

  • CodeGeneration, which provides generation pipeline:
import CodeGeneration

let code: String = try ModelsGenerator()
   .generateCode(from: schemaURL)
   .decorate(using: codeDecorator) // optional phase
   .print(
      using: OutputTemplate(...), 
      and: SwiftPrinter() // or `ObjcInteropPrinter()`
   )
  • CodeDecoration, providing 2 distinct implementations of codeDecorator:
    • RUMCodeDecorator: CodeDecorator - applies RUM conventions to generated code
    • SRCodeDecorator: CodeDecorator - will apply SR conventions to generated code
  • rum-models-generator - small CLI wrapper

🎁 With this change, the CodeGeneration module becomes fully RUM- and SR-agnostic. It can be understood as general-purpose generator of Swift and Objc-interop code from JSON Schema.

Conventions required by RUM and SR products are only implemented in CodeDecoration module, by conformance to CodeGeneration.CodeDecorator interface:

/// A type decorating generated code.
/// Decoration can be used to adjust naming and structure in generated code before it is printed.
public protocol CodeDecorator {
    func decorate(code: GeneratedCode) throws -> GeneratedCode
}

/// Schema describing generated code.
public struct GeneratedCode {
    /// An array of Swift schemas describing root constructs in generated code.
    public let swiftTypes: [SwiftType]

    // ...
}

Review checklist

  • Feature or bugfix MUST have appropriate tests (unit, integration)
  • Make sure each commit and the PR mention the Issue number or JIRA reference
  • Add CHANGELOG entry for user facing changes

Custom CI job configuration (optional)

  • Run unit tests
  • Run integration tests
  • Run smoke tests

to prepare it for Session Replay generation track
@ncreated ncreated requested a review from a team as a code owner July 25, 2022 11:25
@ncreated ncreated self-assigned this Jul 25, 2022
Comment on lines +1 to +20
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2019-2020 Datadog, Inc.
*/

import Foundation

/// Type-safe Swift schema.
public protocol SwiftType {}

/// Swift primitive type.
public protocol SwiftPrimitiveType: SwiftType {}
/// An allowed value of Swift primitive type.
public protocol SwiftPrimitiveValue {}
/// An allowed default value of Swift property.
public protocol SwiftPropertyDefaultValue {}
/// An allowed value of Swift with no obj-c interoperability.
public protocol SwiftPrimitiveNoObjcInteropType: SwiftPrimitiveType {}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only changes in this file are internalpublic access modifiers for all pieces of Swift schema. This is to make it visible for external module which performs code decoration.

Comment on lines +14 to +22
// CLI wrapper
.target(
name: "rum-models-generator",
dependencies: [
"RUMModelsGeneratorCore",
"CodeGeneration",
"CodeDecoration",
.product(name: "ArgumentParser", package: "swift-argument-parser")
]),
]
),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it is no longer rum- models generator, we may want to rename the package. I don't do this, because it has consequences on other files in the repo (all places where we call it from). I didn't want to spend time on it right now, but it's IMO something we might want consider in the future.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's fine keep it for now 👍

Copy link
Member

@maxep maxep left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect 👏 👏

Comment on lines +14 to +22
// CLI wrapper
.target(
name: "rum-models-generator",
dependencies: [
"RUMModelsGeneratorCore",
"CodeGeneration",
"CodeDecoration",
.product(name: "ArgumentParser", package: "swift-argument-parser")
]),
]
),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's fine keep it for now 👍

Base automatically changed from ncreated/RUMM-2266-fix-problems-in-sr-models-generation to develop July 26, 2022 09:44
@ncreated ncreated merged commit 945adc4 into develop Jul 26, 2022
@ncreated ncreated deleted the ncreated/RUMM-2266-generate-session-replay-models branch July 26, 2022 10:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants