// Code generated by sidekick. DO NOT EDIT.
//
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import Foundation
import GoogleCloudWkt

/// Specification that applies to a routine. Valid only for
/// entries with the `ROUTINE` type.
public struct RoutineSpec: Codable, Equatable, GoogleCloudWkt._AnyPackable,
  Sendable
{
  /// The type of the routine.
  public var routineType: RoutineSpec.RoutineType = RoutineSpec.RoutineType()

  /// The language the routine is written in. The exact value depends on the
  /// source system. For BigQuery routines, possible values are:
  ///
  /// * `SQL`
  /// * `JAVASCRIPT`
  public var language: Swift.String = Swift.String()

  /// Arguments of the routine.
  public var routineArguments: [RoutineSpec.Argument] = []

  /// Return type of the argument. The exact value depends on the source system
  /// and the language.
  public var returnType: Swift.String = Swift.String()

  /// The body of the routine.
  public var definitionBody: Swift.String = Swift.String()

  /// Contains fields specific to the source system.
  public var systemSpec: OneOf_SystemSpec? = nil

  /// Initialize a new instance of `RoutineSpec`.
  public init() {}

  /// Use `config` to return a new instance of this object, with some fields updated.
  ///
  /// Commonly used to initialize the value, for example:
  ///
  /// ```
  /// let value = RoutineSpec().with { $0.routineType = ... }
  /// ```
  public func with(_ config: (inout Self) throws -> Swift.Void) rethrows -> Self {
    var copy = self
    try config(&copy)
    return copy
  }

  private enum CodingKeys: Swift.String, CodingKey {
    case routineType = "routineType"
    case language = "language"
    case routineArguments = "routineArguments"
    case returnType = "returnType"
    case definitionBody = "definitionBody"
    case bigqueryRoutineSpec = "bigqueryRoutineSpec"
  }

  public init(from decoder: Decoder) throws {
    let container = try decoder.container(keyedBy: CodingKeys.self)
    self.routineType = try container.decode(RoutineSpec.RoutineType.self, forKey: .routineType)
    self.language = try container.decode(Swift.String.self, forKey: .language)
    self.routineArguments = try container.decode(
      [RoutineSpec.Argument].self, forKey: .routineArguments)
    self.returnType = try container.decode(Swift.String.self, forKey: .returnType)
    self.definitionBody = try container.decode(Swift.String.self, forKey: .definitionBody)

    var systemSpec: OneOf_SystemSpec? = nil
    let systemSpecCheckAndSet = {
      if systemSpec != nil {
        throw DecodingError.dataCorrupted(
          DecodingError.Context(
            codingPath: decoder.codingPath,
            debugDescription: "Multiple values set for oneof 'systemSpec'"))
      }
      systemSpec = $0
    }
    if let bigqueryRoutineSpec = try container.decodeIfPresent(
      BigQueryRoutineSpec?.self, forKey: .bigqueryRoutineSpec)
    {
      try systemSpecCheckAndSet(.bigqueryRoutineSpec(bigqueryRoutineSpec))
    }
    self.systemSpec = systemSpec
  }

  public func encode(to encoder: Encoder) throws {
    var container = encoder.container(keyedBy: CodingKeys.self)
    try container.encode(self.routineType, forKey: .routineType)
    try container.encode(self.language, forKey: .language)
    try container.encode(self.routineArguments, forKey: .routineArguments)
    try container.encode(self.returnType, forKey: .returnType)
    try container.encode(self.definitionBody, forKey: .definitionBody)

    if let choice = self.systemSpec {
      switch choice {
      case .bigqueryRoutineSpec(let value):
        try container.encode(value, forKey: .bigqueryRoutineSpec)
      }
    }
  }

  /// Input or output argument of a function or stored procedure.
  public struct Argument: Codable, Equatable, GoogleCloudWkt._AnyPackable,
    Sendable
  {
    /// The name of the argument. A return argument of a function might not have
    /// a name.
    public var name: Swift.String = Swift.String()

    /// Specifies whether the argument is input or output.
    public var mode: RoutineSpec.Argument.Mode = RoutineSpec.Argument.Mode()

    /// Type of the argument. The exact value depends on the source system and
    /// the language.
    public var type: Swift.String = Swift.String()

    /// Initialize a new instance of `Argument`.
    public init() {}

    /// Use `config` to return a new instance of this object, with some fields updated.
    ///
    /// Commonly used to initialize the value, for example:
    ///
    /// ```
    /// let value = Argument().with { $0.name = ... }
    /// ```
    public func with(_ config: (inout Self) throws -> Swift.Void) rethrows -> Self {
      var copy = self
      try config(&copy)
      return copy
    }

    /// The input or output mode of the argument.
    public enum Mode: Codable, Equatable, Sendable {
      /// Unspecified mode.
      case unspecified
      /// The argument is input-only.
      case `in`
      /// The argument is output-only.
      case out
      /// The argument is both an input and an output.
      case `inout`
      /// Encodes an unknown integer value.
      ///
      /// The most common cause for an unknown values is for the service to send
      /// a value unknown to the library. We recommend you update your library to
      /// the latest version.
      case unknownIntValue(Int)
      /// Encodes an unknown string value.
      ///
      /// The most common cause for an unknown values is for the service to send
      /// a value unknown to the library. We recommend you update your library to
      /// the latest version.
      case unknownStringValue(String)

      public init() {
        self = .unspecified
      }

      /// Returns the integer value associated with the enumeration.
      ///
      /// If the enumeration was initialized with an unknown string value, this returns `nil`.
      public var intValue: Int? {
        switch self {
        case .unspecified: return 0
        case .`in`: return 1
        case .out: return 2
        case .`inout`: return 3
        case .unknownIntValue(let v): return v
        case .unknownStringValue: return nil
        }
      }

      /// Returns the string value (or name) associated with the enumeration.
      ///
      /// If the enumeration was initialized with an unknown integer value, this returns `nil`.
      public var stringValue: Swift.String? {
        switch self {
        case .unspecified: return "MODE_UNSPECIFIED"
        case .`in`: return "IN"
        case .out: return "OUT"
        case .`inout`: return "INOUT"
        case .unknownIntValue: return nil
        case .unknownStringValue(let v): return v
        }
      }

      /// Initialize from a string value.
      ///
      /// If the value is unknown, this initializes to [`unknownStringValue`](doc:Mode/unknownStringValue(_:)).
      public init(stringValue: Swift.String) {
        switch stringValue {
        case "MODE_UNSPECIFIED": self = .unspecified
        case "IN": self = .`in`
        case "OUT": self = .out
        case "INOUT": self = .`inout`
        default: self = .unknownStringValue(stringValue)
        }
      }

      /// Initialize from an integer value.
      ///
      /// If the value is unknown, this initializes to [`unknownIntValue`](doc:Mode/unknownIntValue(_:)).
      public init(intValue: Int) {
        switch intValue {
        case 0: self = .unspecified
        case 1: self = .`in`
        case 2: self = .out
        case 3: self = .`inout`
        default: self = .unknownIntValue(intValue)
        }
      }

      public init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        if let v = try? container.decode(Int.self) {
          self.init(intValue: v)
          return
        }
        if let s = try? container.decode(String.self) {
          if let v = Int(s) {
            self.init(intValue: v)
          } else {
            self.init(stringValue: s)
          }
          return
        }
        throw DecodingError.dataCorruptedError(
          in: container, debugDescription: "Expected enum value, must be integer or string.")
      }

      public func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        switch self {
        case .unspecified: return try container.encode(0)
        case .`in`: return try container.encode(1)
        case .out: return try container.encode(2)
        case .`inout`: return try container.encode(3)
        case .unknownIntValue(let v): return try container.encode(v)
        case .unknownStringValue(let v): return try container.encode(v)
        }
      }
    }

    public static var _anyTypeUrl: Swift.String {
      return "type.googleapis.com/google.cloud.datacatalog.v1.RoutineSpec.Argument"
    }
    public init(fromAny any: GoogleCloudWkt.`Any`) throws {
      self = try GoogleCloudWkt._slowAnyDeserialize(Self.self, from: any)
    }
    public func _pack() throws -> GoogleCloudWkt.Struct {
      return try GoogleCloudWkt._slowAnySerialize(message: self)
    }
  }

  /// The fine-grained type of the routine.
  public enum RoutineType: Codable, Equatable, Sendable {
    /// Unspecified type.
    case unspecified
    /// Non-builtin permanent scalar function.
    case scalarFunction
    /// Stored procedure.
    case procedure
    /// Encodes an unknown integer value.
    ///
    /// The most common cause for an unknown values is for the service to send
    /// a value unknown to the library. We recommend you update your library to
    /// the latest version.
    case unknownIntValue(Int)
    /// Encodes an unknown string value.
    ///
    /// The most common cause for an unknown values is for the service to send
    /// a value unknown to the library. We recommend you update your library to
    /// the latest version.
    case unknownStringValue(String)

    public init() {
      self = .unspecified
    }

    /// Returns the integer value associated with the enumeration.
    ///
    /// If the enumeration was initialized with an unknown string value, this returns `nil`.
    public var intValue: Int? {
      switch self {
      case .unspecified: return 0
      case .scalarFunction: return 1
      case .procedure: return 2
      case .unknownIntValue(let v): return v
      case .unknownStringValue: return nil
      }
    }

    /// Returns the string value (or name) associated with the enumeration.
    ///
    /// If the enumeration was initialized with an unknown integer value, this returns `nil`.
    public var stringValue: Swift.String? {
      switch self {
      case .unspecified: return "ROUTINE_TYPE_UNSPECIFIED"
      case .scalarFunction: return "SCALAR_FUNCTION"
      case .procedure: return "PROCEDURE"
      case .unknownIntValue: return nil
      case .unknownStringValue(let v): return v
      }
    }

    /// Initialize from a string value.
    ///
    /// If the value is unknown, this initializes to [`unknownStringValue`](doc:RoutineType/unknownStringValue(_:)).
    public init(stringValue: Swift.String) {
      switch stringValue {
      case "ROUTINE_TYPE_UNSPECIFIED": self = .unspecified
      case "SCALAR_FUNCTION": self = .scalarFunction
      case "PROCEDURE": self = .procedure
      default: self = .unknownStringValue(stringValue)
      }
    }

    /// Initialize from an integer value.
    ///
    /// If the value is unknown, this initializes to [`unknownIntValue`](doc:RoutineType/unknownIntValue(_:)).
    public init(intValue: Int) {
      switch intValue {
      case 0: self = .unspecified
      case 1: self = .scalarFunction
      case 2: self = .procedure
      default: self = .unknownIntValue(intValue)
      }
    }

    public init(from decoder: Decoder) throws {
      let container = try decoder.singleValueContainer()
      if let v = try? container.decode(Int.self) {
        self.init(intValue: v)
        return
      }
      if let s = try? container.decode(String.self) {
        if let v = Int(s) {
          self.init(intValue: v)
        } else {
          self.init(stringValue: s)
        }
        return
      }
      throw DecodingError.dataCorruptedError(
        in: container, debugDescription: "Expected enum value, must be integer or string.")
    }

    public func encode(to encoder: Encoder) throws {
      var container = encoder.singleValueContainer()
      switch self {
      case .unspecified: return try container.encode(0)
      case .scalarFunction: return try container.encode(1)
      case .procedure: return try container.encode(2)
      case .unknownIntValue(let v): return try container.encode(v)
      case .unknownStringValue(let v): return try container.encode(v)
      }
    }
  }

  /// Contains fields specific to the source system.
  public enum OneOf_SystemSpec: Codable, Equatable, Sendable {
    /// Fields specific for BigQuery routines.
    indirect case bigqueryRoutineSpec(BigQueryRoutineSpec?)
  }

  public static var _anyTypeUrl: Swift.String {
    return "type.googleapis.com/google.cloud.datacatalog.v1.RoutineSpec"
  }
  public init(fromAny any: GoogleCloudWkt.`Any`) throws {
    self = try GoogleCloudWkt._slowAnyDeserialize(Self.self, from: any)
  }
  public func _pack() throws -> GoogleCloudWkt.Struct {
    return try GoogleCloudWkt._slowAnySerialize(message: self)
  }
}
