diff --git a/packages/autorest.go/README.md b/packages/autorest.go/README.md index 28b3abd86..c2f8352de 100644 --- a/packages/autorest.go/README.md +++ b/packages/autorest.go/README.md @@ -99,7 +99,7 @@ help-content: - key: honor-body-placement type: boolean description: When true, optional body parameters are treated as such for PATCH and PUT operations. - - key: remove-non-reference-schema + - key: remove-unreferenced-types type: boolean description: When true, non-reference schema will be removed from the generated code. - key: normalize-operation-name diff --git a/packages/typespec-go/.scripts/tspcompile.js b/packages/typespec-go/.scripts/tspcompile.js index 13d4de96d..fa3730306 100644 --- a/packages/typespec-go/.scripts/tspcompile.js +++ b/packages/typespec-go/.scripts/tspcompile.js @@ -117,16 +117,16 @@ function should_generate(name) { } const armcodesigning = pkgRoot + 'test/tsp/CodeSigning.Management'; -generate('armcodesigning', armcodesigning, 'test/armcodesigning'); +generate('armcodesigning', armcodesigning, 'test/armcodesigning', ['remove-unreferenced-types=true']); const armapicenter = pkgRoot + 'test/tsp/ApiCenter.Management'; -generate('armapicenter', armapicenter, 'test/armapicenter'); +generate('armapicenter', armapicenter, 'test/armapicenter', ['remove-unreferenced-types=true']); const armlargeinstance = pkgRoot + 'test/tsp/AzureLargeInstance.Management'; -generate('armlargeinstance', armlargeinstance, 'test/armlargeinstance', ['stutter=AzureLargeInstance']); +generate('armlargeinstance', armlargeinstance, 'test/armlargeinstance', ['stutter=AzureLargeInstance', 'remove-unreferenced-types=true']); const armdatabasewatcher = pkgRoot + `test/tsp/DatabaseWatcher.Management`; -generate('armdatabasewatcher', armdatabasewatcher, 'test/armdatabasewatcher'); +generate('armdatabasewatcher', armdatabasewatcher, 'test/armdatabasewatcher', ['remove-unreferenced-types=true']); for (const module in cadlRanch) { const values = cadlRanch[module]; diff --git a/packages/typespec-go/src/lib.ts b/packages/typespec-go/src/lib.ts index eb8267a8f..0ef5fc1d1 100644 --- a/packages/typespec-go/src/lib.ts +++ b/packages/typespec-go/src/lib.ts @@ -18,6 +18,7 @@ export interface GoEmitterOptions { 'slice-elements-byval'?: boolean; 'single-client'?: boolean; 'stutter'?: string; + 'remove-unreferenced-types'?: boolean; } const EmitterOptionsSchema: JSONSchemaType = { @@ -35,7 +36,8 @@ const EmitterOptionsSchema: JSONSchemaType = { 'rawjson-as-bytes': { type: 'boolean', nullable: true }, 'slice-elements-byval': { type: 'boolean', nullable: true }, 'single-client': { type: 'boolean', nullable: true }, - 'stutter': { type: 'string', nullable: true } + 'stutter': { type: 'string', nullable: true }, + 'remove-unreferenced-types': { type: 'boolean', nullable: true }, }, required: [], }; diff --git a/packages/typespec-go/src/tcgcadapter/adapter.ts b/packages/typespec-go/src/tcgcadapter/adapter.ts index 46854bbcb..0b4e3ac7f 100644 --- a/packages/typespec-go/src/tcgcadapter/adapter.ts +++ b/packages/typespec-go/src/tcgcadapter/adapter.ts @@ -44,7 +44,7 @@ export function tcgcToGoCodeModel(context: EmitContext): go.Co fixStutteringTypeNames(sdkContext.experimental_sdkPackage, codeModel, context.options.stutter); const ta = new typeAdapter(codeModel); - ta.adaptTypes(sdkContext); + ta.adaptTypes(sdkContext, context.options['remove-unreferenced-types'] === true); const ca = new clientAdapter(ta, context.options); ca.adaptClients(sdkContext.experimental_sdkPackage); diff --git a/packages/typespec-go/src/tcgcadapter/types.ts b/packages/typespec-go/src/tcgcadapter/types.ts index b3b47a7d5..18e92ee95 100644 --- a/packages/typespec-go/src/tcgcadapter/types.ts +++ b/packages/typespec-go/src/tcgcadapter/types.ts @@ -18,45 +18,32 @@ export class typeAdapter { // cache of previously created types/constant values private types: Map; private constValues: Map; + + // contains the names of referenced types + private unreferencedEnums: Set; + private unreferencedModels: Set; constructor(codeModel: go.CodeModel) { this.codeModel = codeModel; this.types = new Map(); this.constValues = new Map(); + this.unreferencedEnums = new Set(); + this.unreferencedModels = new Set(); } // converts all model/enum SDK types to Go code model types - adaptTypes(sdkContext: tcgc.SdkContext) { - const isOperationStatusModel = function(name: string): boolean { - return name.match(/^(?:Arm)?OperationStatus/) !== null; - }; + adaptTypes(sdkContext: tcgc.SdkContext, removeUnreferencedTypes: boolean) { + if (removeUnreferencedTypes) { + this.flagUnreferencedTypes(sdkContext); + } for (const enumType of sdkContext.experimental_sdkPackage.enums) { if (enumType.usage === tcgc.UsageFlags.ApiVersionEnum) { // we have a pipeline policy for controlling the api-version continue; - } else if (enumType.crossLanguageDefinitionId.match(/(?:Foundations|ResourceManager)\.(?:Resource)?ProvisioningState/)) { - // don't create constants for the LRO provisioning state if they aren't used - let found = false; - for (const model of sdkContext.experimental_sdkPackage.models) { - if (isOperationStatusModel(model.name)) { - // these types are discarded so exclude them from the check - continue; - } - for (const property of model.properties) { - if (property.type.kind === 'enum' && property.type.name === enumType.name) { - found = true; - break; - } - } - if (found) { - break; - } - } - if (!found) { - // enum isn't used, discard it - continue; - } + } else if (this.unreferencedEnums.has(enumType.name)) { + // skip unreferenced type + continue; } const constType = this.getConstantType(enumType); this.codeModel.constants.push(constType); @@ -69,8 +56,8 @@ export class typeAdapter { if (this.isFoundationsError(modelType)) { // don't create a model as we use azcore.ResponseError instead continue; - } else if (isOperationStatusModel(modelType.name)) { - // don't create a model for the LRO polling status as they aren't used + } else if (this.unreferencedModels.has(modelType.name)) { + // skip unreferenced type continue; } if (modelType.discriminatedSubtypes) { @@ -733,6 +720,75 @@ export class typeAdapter { // TODO: tcgc doesn't support duration as a literal value } + + // updates this.unreferencedEnums and this.unreferencedModels + private flagUnreferencedTypes(sdkContext: tcgc.SdkContext): void { + const referencedEnums = new Set(); + const referencedModels = new Set(); + + const recursiveAddReferencedType = function(type: tcgc.SdkType): void { + switch (type.kind) { + case 'array': + case 'dict': + recursiveAddReferencedType(type.valueType); + break; + case 'enum': + if (!referencedEnums.has(type.name)) { + referencedEnums.add(type.name); + } + break; + case 'enumvalue': + if (!referencedEnums.has(type.enumType.name)) { + referencedEnums.add(type.enumType.name); + } + break; + case 'model': + if (!referencedModels.has(type.name)) { + referencedModels.add(type.name); + const aggregateProps = aggregateProperties(type); + for (const prop of aggregateProps.props) { + recursiveAddReferencedType(prop.type); + } + if (type.discriminatedSubtypes) { + for (const subType of values(type.discriminatedSubtypes)) { + recursiveAddReferencedType(subType); + } + } + } + break; + } + }; + + // traverse all methods to find the set of referenced enums and models + for (const client of sdkContext.experimental_sdkPackage.clients) { + for (const method of client.methods) { + if (method.kind === 'clientaccessor') { + continue; + } + + for (const param of method.parameters) { + recursiveAddReferencedType(param.type); + } + + if (method.response.type) { + recursiveAddReferencedType(method.response.type); + } + } + } + + // now that we have the referenced set, update the unreferenced set + for (const sdkEnum of sdkContext.experimental_sdkPackage.enums) { + if (!referencedEnums.has(sdkEnum.name)) { + this.unreferencedEnums.add(sdkEnum.name); + } + } + + for (const model of sdkContext.experimental_sdkPackage.models) { + if (!referencedModels.has(model.name)) { + this.unreferencedModels.add(model.name); + } + } + } } function getPrimitiveType(kind: tcgc.SdkBuiltInKinds): 'bool' | 'float32' | 'float64' | 'int32' | 'int64' | 'string' { diff --git a/packages/typespec-go/test/armapicenter/zz_models.go b/packages/typespec-go/test/armapicenter/zz_models.go index 38422383f..bf02e6073 100644 --- a/packages/typespec-go/test/armapicenter/zz_models.go +++ b/packages/typespec-go/test/armapicenter/zz_models.go @@ -184,22 +184,6 @@ type APIVersionProperties struct { Title *string } -// ArmResource - Common properties for all Azure Resource Manager resources. -type ArmResource struct { - // REQUIRED; Fully qualified resource ID for the resource. Ex - /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{resourceProviderNamespace}/{resourceType}/{resourceName} - ID *string - - // REQUIRED; The type of the resource. E.g. "Microsoft.Compute/virtualMachines" or "Microsoft.Storage/storageAccounts" - Type *string - - // Azure Resource Manager metadata containing createdBy and modifiedBy information. - SystemData *SystemData -} - -// ArmResourceBase - Base class used for type definitions -type ArmResourceBase struct { -} - // Contact information type Contact struct { // Email address of the contact. @@ -490,18 +474,6 @@ type PagedOperation struct { NextLink *string } -// ProxyResourceBase - The base proxy resource. -type ProxyResourceBase struct { - // REQUIRED; Fully qualified resource ID for the resource. Ex - /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{resourceProviderNamespace}/{resourceType}/{resourceName} - ID *string - - // REQUIRED; The type of the resource. E.g. "Microsoft.Compute/virtualMachines" or "Microsoft.Storage/storageAccounts" - Type *string - - // Azure Resource Manager metadata containing createdBy and modifiedBy information. - SystemData *SystemData -} - // Service - The service entity. type Service struct { // REQUIRED; Fully qualified resource ID for the resource. Ex - /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{resourceProviderNamespace}/{resourceType}/{resourceName} @@ -580,24 +552,6 @@ type TermsOfService struct { URL *string } -// TrackedResourceBase - The resource model definition for an Azure Resource Manager tracked top level resource -type TrackedResourceBase struct { - // REQUIRED; Fully qualified resource ID for the resource. Ex - /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{resourceProviderNamespace}/{resourceType}/{resourceName} - ID *string - - // REQUIRED; The geo-location where the resource lives - Location *string - - // REQUIRED; The type of the resource. E.g. "Microsoft.Compute/virtualMachines" or "Microsoft.Storage/storageAccounts" - Type *string - - // Azure Resource Manager metadata containing createdBy and modifiedBy information. - SystemData *SystemData - - // Resource tags. - Tags map[string]*string -} - // UserAssignedIdentity - A managed identity assigned by the user. type UserAssignedIdentity struct { // The active directory client identifier for this principal. diff --git a/packages/typespec-go/test/armapicenter/zz_models_serde.go b/packages/typespec-go/test/armapicenter/zz_models_serde.go index 52e70b8a8..4bc9ada03 100644 --- a/packages/typespec-go/test/armapicenter/zz_models_serde.go +++ b/packages/typespec-go/test/armapicenter/zz_models_serde.go @@ -490,41 +490,6 @@ func (a *APIVersionProperties) UnmarshalJSON(data []byte) error { return nil } -// MarshalJSON implements the json.Marshaller interface for type ArmResource. -func (a ArmResource) MarshalJSON() ([]byte, error) { - objectMap := make(map[string]any) - populate(objectMap, "id", a.ID) - populate(objectMap, "systemData", a.SystemData) - populate(objectMap, "type", a.Type) - return json.Marshal(objectMap) -} - -// UnmarshalJSON implements the json.Unmarshaller interface for type ArmResource. -func (a *ArmResource) UnmarshalJSON(data []byte) error { - var rawMsg map[string]json.RawMessage - if err := json.Unmarshal(data, &rawMsg); err != nil { - return fmt.Errorf("unmarshalling type %T: %v", a, err) - } - for key, val := range rawMsg { - var err error - switch key { - case "id": - err = unpopulate(val, "ID", &a.ID) - delete(rawMsg, key) - case "systemData": - err = unpopulate(val, "SystemData", &a.SystemData) - delete(rawMsg, key) - case "type": - err = unpopulate(val, "Type", &a.Type) - delete(rawMsg, key) - } - if err != nil { - return fmt.Errorf("unmarshalling type %T: %v", a, err) - } - } - return nil -} - // MarshalJSON implements the json.Marshaller interface for type Contact. func (c Contact) MarshalJSON() ([]byte, error) { objectMap := make(map[string]any) @@ -1315,41 +1280,6 @@ func (p *PagedOperation) UnmarshalJSON(data []byte) error { return nil } -// MarshalJSON implements the json.Marshaller interface for type ProxyResourceBase. -func (p ProxyResourceBase) MarshalJSON() ([]byte, error) { - objectMap := make(map[string]any) - populate(objectMap, "id", p.ID) - populate(objectMap, "systemData", p.SystemData) - populate(objectMap, "type", p.Type) - return json.Marshal(objectMap) -} - -// UnmarshalJSON implements the json.Unmarshaller interface for type ProxyResourceBase. -func (p *ProxyResourceBase) UnmarshalJSON(data []byte) error { - var rawMsg map[string]json.RawMessage - if err := json.Unmarshal(data, &rawMsg); err != nil { - return fmt.Errorf("unmarshalling type %T: %v", p, err) - } - for key, val := range rawMsg { - var err error - switch key { - case "id": - err = unpopulate(val, "ID", &p.ID) - delete(rawMsg, key) - case "systemData": - err = unpopulate(val, "SystemData", &p.SystemData) - delete(rawMsg, key) - case "type": - err = unpopulate(val, "Type", &p.Type) - delete(rawMsg, key) - } - if err != nil { - return fmt.Errorf("unmarshalling type %T: %v", p, err) - } - } - return nil -} - // MarshalJSON implements the json.Marshaller interface for type Service. func (s Service) MarshalJSON() ([]byte, error) { objectMap := make(map[string]any) @@ -1568,49 +1498,6 @@ func (t *TermsOfService) UnmarshalJSON(data []byte) error { return nil } -// MarshalJSON implements the json.Marshaller interface for type TrackedResourceBase. -func (t TrackedResourceBase) MarshalJSON() ([]byte, error) { - objectMap := make(map[string]any) - populate(objectMap, "id", t.ID) - populate(objectMap, "location", t.Location) - populate(objectMap, "systemData", t.SystemData) - populate(objectMap, "tags", t.Tags) - populate(objectMap, "type", t.Type) - return json.Marshal(objectMap) -} - -// UnmarshalJSON implements the json.Unmarshaller interface for type TrackedResourceBase. -func (t *TrackedResourceBase) UnmarshalJSON(data []byte) error { - var rawMsg map[string]json.RawMessage - if err := json.Unmarshal(data, &rawMsg); err != nil { - return fmt.Errorf("unmarshalling type %T: %v", t, err) - } - for key, val := range rawMsg { - var err error - switch key { - case "id": - err = unpopulate(val, "ID", &t.ID) - delete(rawMsg, key) - case "location": - err = unpopulate(val, "Location", &t.Location) - delete(rawMsg, key) - case "systemData": - err = unpopulate(val, "SystemData", &t.SystemData) - delete(rawMsg, key) - case "tags": - err = unpopulate(val, "Tags", &t.Tags) - delete(rawMsg, key) - case "type": - err = unpopulate(val, "Type", &t.Type) - delete(rawMsg, key) - } - if err != nil { - return fmt.Errorf("unmarshalling type %T: %v", t, err) - } - } - return nil -} - // MarshalJSON implements the json.Marshaller interface for type UserAssignedIdentity. func (u UserAssignedIdentity) MarshalJSON() ([]byte, error) { objectMap := make(map[string]any) diff --git a/packages/typespec-go/test/armcodesigning/zz_models.go b/packages/typespec-go/test/armcodesigning/zz_models.go index b4b272d2b..81ffc4836 100644 --- a/packages/typespec-go/test/armcodesigning/zz_models.go +++ b/packages/typespec-go/test/armcodesigning/zz_models.go @@ -72,22 +72,6 @@ type AccountSKU struct { Name *SKUName } -// ArmResource - Common properties for all Azure Resource Manager resources. -type ArmResource struct { - // REQUIRED; Fully qualified resource ID for the resource. Ex - /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{resourceProviderNamespace}/{resourceType}/{resourceName} - ID *string - - // REQUIRED; The type of the resource. E.g. "Microsoft.Compute/virtualMachines" or "Microsoft.Storage/storageAccounts" - Type *string - - // Azure Resource Manager metadata containing createdBy and modifiedBy information. - SystemData *SystemData -} - -// ArmResourceBase - Base class used for type definitions -type ArmResourceBase struct { -} - // Certificate - Properties of the certificate. type Certificate struct { // Certificate created date. @@ -266,18 +250,6 @@ type PagedOperation struct { NextLink *string } -// ProxyResourceBase - The base proxy resource. -type ProxyResourceBase struct { - // REQUIRED; Fully qualified resource ID for the resource. Ex - /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{resourceProviderNamespace}/{resourceType}/{resourceName} - ID *string - - // REQUIRED; The type of the resource. E.g. "Microsoft.Compute/virtualMachines" or "Microsoft.Storage/storageAccounts" - Type *string - - // Azure Resource Manager metadata containing createdBy and modifiedBy information. - SystemData *SystemData -} - // Revocation details of the certificate. type Revocation struct { // The timestamp when the revocation is effective. @@ -337,21 +309,3 @@ type SystemData struct { // The type of identity that last modified the resource. LastModifiedByType *CreatedByType } - -// TrackedResourceBase - The resource model definition for an Azure Resource Manager tracked top level resource -type TrackedResourceBase struct { - // REQUIRED; Fully qualified resource ID for the resource. Ex - /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{resourceProviderNamespace}/{resourceType}/{resourceName} - ID *string - - // REQUIRED; The geo-location where the resource lives - Location *string - - // REQUIRED; The type of the resource. E.g. "Microsoft.Compute/virtualMachines" or "Microsoft.Storage/storageAccounts" - Type *string - - // Azure Resource Manager metadata containing createdBy and modifiedBy information. - SystemData *SystemData - - // Resource tags. - Tags map[string]*string -} diff --git a/packages/typespec-go/test/armcodesigning/zz_models_serde.go b/packages/typespec-go/test/armcodesigning/zz_models_serde.go index 1fd7cd22d..fb4e45f72 100644 --- a/packages/typespec-go/test/armcodesigning/zz_models_serde.go +++ b/packages/typespec-go/test/armcodesigning/zz_models_serde.go @@ -213,41 +213,6 @@ func (a *AccountSKU) UnmarshalJSON(data []byte) error { return nil } -// MarshalJSON implements the json.Marshaller interface for type ArmResource. -func (a ArmResource) MarshalJSON() ([]byte, error) { - objectMap := make(map[string]any) - populate(objectMap, "id", a.ID) - populate(objectMap, "systemData", a.SystemData) - populate(objectMap, "type", a.Type) - return json.Marshal(objectMap) -} - -// UnmarshalJSON implements the json.Unmarshaller interface for type ArmResource. -func (a *ArmResource) UnmarshalJSON(data []byte) error { - var rawMsg map[string]json.RawMessage - if err := json.Unmarshal(data, &rawMsg); err != nil { - return fmt.Errorf("unmarshalling type %T: %v", a, err) - } - for key, val := range rawMsg { - var err error - switch key { - case "id": - err = unpopulate(val, "ID", &a.ID) - delete(rawMsg, key) - case "systemData": - err = unpopulate(val, "SystemData", &a.SystemData) - delete(rawMsg, key) - case "type": - err = unpopulate(val, "Type", &a.Type) - delete(rawMsg, key) - } - if err != nil { - return fmt.Errorf("unmarshalling type %T: %v", a, err) - } - } - return nil -} - // MarshalJSON implements the json.Marshaller interface for type Certificate. func (c Certificate) MarshalJSON() ([]byte, error) { objectMap := make(map[string]any) @@ -647,41 +612,6 @@ func (p *PagedOperation) UnmarshalJSON(data []byte) error { return nil } -// MarshalJSON implements the json.Marshaller interface for type ProxyResourceBase. -func (p ProxyResourceBase) MarshalJSON() ([]byte, error) { - objectMap := make(map[string]any) - populate(objectMap, "id", p.ID) - populate(objectMap, "systemData", p.SystemData) - populate(objectMap, "type", p.Type) - return json.Marshal(objectMap) -} - -// UnmarshalJSON implements the json.Unmarshaller interface for type ProxyResourceBase. -func (p *ProxyResourceBase) UnmarshalJSON(data []byte) error { - var rawMsg map[string]json.RawMessage - if err := json.Unmarshal(data, &rawMsg); err != nil { - return fmt.Errorf("unmarshalling type %T: %v", p, err) - } - for key, val := range rawMsg { - var err error - switch key { - case "id": - err = unpopulate(val, "ID", &p.ID) - delete(rawMsg, key) - case "systemData": - err = unpopulate(val, "SystemData", &p.SystemData) - delete(rawMsg, key) - case "type": - err = unpopulate(val, "Type", &p.Type) - delete(rawMsg, key) - } - if err != nil { - return fmt.Errorf("unmarshalling type %T: %v", p, err) - } - } - return nil -} - // MarshalJSON implements the json.Marshaller interface for type Revocation. func (r Revocation) MarshalJSON() ([]byte, error) { objectMap := make(map[string]any) @@ -819,49 +749,6 @@ func (s *SystemData) UnmarshalJSON(data []byte) error { return nil } -// MarshalJSON implements the json.Marshaller interface for type TrackedResourceBase. -func (t TrackedResourceBase) MarshalJSON() ([]byte, error) { - objectMap := make(map[string]any) - populate(objectMap, "id", t.ID) - populate(objectMap, "location", t.Location) - populate(objectMap, "systemData", t.SystemData) - populate(objectMap, "tags", t.Tags) - populate(objectMap, "type", t.Type) - return json.Marshal(objectMap) -} - -// UnmarshalJSON implements the json.Unmarshaller interface for type TrackedResourceBase. -func (t *TrackedResourceBase) UnmarshalJSON(data []byte) error { - var rawMsg map[string]json.RawMessage - if err := json.Unmarshal(data, &rawMsg); err != nil { - return fmt.Errorf("unmarshalling type %T: %v", t, err) - } - for key, val := range rawMsg { - var err error - switch key { - case "id": - err = unpopulate(val, "ID", &t.ID) - delete(rawMsg, key) - case "location": - err = unpopulate(val, "Location", &t.Location) - delete(rawMsg, key) - case "systemData": - err = unpopulate(val, "SystemData", &t.SystemData) - delete(rawMsg, key) - case "tags": - err = unpopulate(val, "Tags", &t.Tags) - delete(rawMsg, key) - case "type": - err = unpopulate(val, "Type", &t.Type) - delete(rawMsg, key) - } - if err != nil { - return fmt.Errorf("unmarshalling type %T: %v", t, err) - } - } - return nil -} - func populate(m map[string]any, k string, v any) { if v == nil { return diff --git a/packages/typespec-go/test/armdatabasewatcher/zz_models.go b/packages/typespec-go/test/armdatabasewatcher/zz_models.go index 6adb75d99..1ac743326 100644 --- a/packages/typespec-go/test/armdatabasewatcher/zz_models.go +++ b/packages/typespec-go/test/armdatabasewatcher/zz_models.go @@ -6,22 +6,6 @@ package armdatabasewatcher import "time" -// ArmResource - Common properties for all Azure Resource Manager resources. -type ArmResource struct { - // REQUIRED; Fully qualified resource ID for the resource. Ex - /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{resourceProviderNamespace}/{resourceType}/{resourceName} - ID *string - - // REQUIRED; The type of the resource. E.g. "Microsoft.Compute/virtualMachines" or "Microsoft.Storage/storageAccounts" - Type *string - - // Azure Resource Manager metadata containing createdBy and modifiedBy information. - SystemData *SystemData -} - -// ArmResourceBase - Base class used for type definitions -type ArmResourceBase struct { -} - // Datastore - The properties of a data store. type Datastore struct { // REQUIRED; The Kusto cluster URI. @@ -108,18 +92,6 @@ type PagedOperation struct { NextLink *string } -// ProxyResourceBase - The base proxy resource. -type ProxyResourceBase struct { - // REQUIRED; Fully qualified resource ID for the resource. Ex - /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{resourceProviderNamespace}/{resourceType}/{resourceName} - ID *string - - // REQUIRED; The type of the resource. E.g. "Microsoft.Compute/virtualMachines" or "Microsoft.Storage/storageAccounts" - Type *string - - // Azure Resource Manager metadata containing createdBy and modifiedBy information. - SystemData *SystemData -} - // SQLDbElasticPoolTargetProperties - The properties specific to elastic pool in Azure SQL Database. type SQLDbElasticPoolTargetProperties struct { // REQUIRED; The Azure ResourceId of the anchor database used to connect to an elastic pool. @@ -396,24 +368,6 @@ type TargetProperties struct { // GetTargetProperties implements the TargetPropertiesClassification interface for type TargetProperties. func (t *TargetProperties) GetTargetProperties() *TargetProperties { return t } -// TrackedResourceBase - The resource model definition for an Azure Resource Manager tracked top level resource -type TrackedResourceBase struct { - // REQUIRED; Fully qualified resource ID for the resource. Ex - /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{resourceProviderNamespace}/{resourceType}/{resourceName} - ID *string - - // REQUIRED; The geo-location where the resource lives - Location *string - - // REQUIRED; The type of the resource. E.g. "Microsoft.Compute/virtualMachines" or "Microsoft.Storage/storageAccounts" - Type *string - - // Azure Resource Manager metadata containing createdBy and modifiedBy information. - SystemData *SystemData - - // Resource tags. - Tags map[string]*string -} - // UserAssignedIdentity - A managed identity assigned by the user. type UserAssignedIdentity struct { // The active directory client identifier for this principal. diff --git a/packages/typespec-go/test/armdatabasewatcher/zz_models_serde.go b/packages/typespec-go/test/armdatabasewatcher/zz_models_serde.go index 349209bc1..8b3dd400b 100644 --- a/packages/typespec-go/test/armdatabasewatcher/zz_models_serde.go +++ b/packages/typespec-go/test/armdatabasewatcher/zz_models_serde.go @@ -11,41 +11,6 @@ import ( "reflect" ) -// MarshalJSON implements the json.Marshaller interface for type ArmResource. -func (a ArmResource) MarshalJSON() ([]byte, error) { - objectMap := make(map[string]any) - populate(objectMap, "id", a.ID) - populate(objectMap, "systemData", a.SystemData) - populate(objectMap, "type", a.Type) - return json.Marshal(objectMap) -} - -// UnmarshalJSON implements the json.Unmarshaller interface for type ArmResource. -func (a *ArmResource) UnmarshalJSON(data []byte) error { - var rawMsg map[string]json.RawMessage - if err := json.Unmarshal(data, &rawMsg); err != nil { - return fmt.Errorf("unmarshalling type %T: %v", a, err) - } - for key, val := range rawMsg { - var err error - switch key { - case "id": - err = unpopulate(val, "ID", &a.ID) - delete(rawMsg, key) - case "systemData": - err = unpopulate(val, "SystemData", &a.SystemData) - delete(rawMsg, key) - case "type": - err = unpopulate(val, "Type", &a.Type) - delete(rawMsg, key) - } - if err != nil { - return fmt.Errorf("unmarshalling type %T: %v", a, err) - } - } - return nil -} - // MarshalJSON implements the json.Marshaller interface for type Datastore. func (d Datastore) MarshalJSON() ([]byte, error) { objectMap := make(map[string]any) @@ -249,41 +214,6 @@ func (p *PagedOperation) UnmarshalJSON(data []byte) error { return nil } -// MarshalJSON implements the json.Marshaller interface for type ProxyResourceBase. -func (p ProxyResourceBase) MarshalJSON() ([]byte, error) { - objectMap := make(map[string]any) - populate(objectMap, "id", p.ID) - populate(objectMap, "systemData", p.SystemData) - populate(objectMap, "type", p.Type) - return json.Marshal(objectMap) -} - -// UnmarshalJSON implements the json.Unmarshaller interface for type ProxyResourceBase. -func (p *ProxyResourceBase) UnmarshalJSON(data []byte) error { - var rawMsg map[string]json.RawMessage - if err := json.Unmarshal(data, &rawMsg); err != nil { - return fmt.Errorf("unmarshalling type %T: %v", p, err) - } - for key, val := range rawMsg { - var err error - switch key { - case "id": - err = unpopulate(val, "ID", &p.ID) - delete(rawMsg, key) - case "systemData": - err = unpopulate(val, "SystemData", &p.SystemData) - delete(rawMsg, key) - case "type": - err = unpopulate(val, "Type", &p.Type) - delete(rawMsg, key) - } - if err != nil { - return fmt.Errorf("unmarshalling type %T: %v", p, err) - } - } - return nil -} - // MarshalJSON implements the json.Marshaller interface for type SQLDbElasticPoolTargetProperties. func (s SQLDbElasticPoolTargetProperties) MarshalJSON() ([]byte, error) { objectMap := make(map[string]any) @@ -785,49 +715,6 @@ func (t *TargetProperties) UnmarshalJSON(data []byte) error { return nil } -// MarshalJSON implements the json.Marshaller interface for type TrackedResourceBase. -func (t TrackedResourceBase) MarshalJSON() ([]byte, error) { - objectMap := make(map[string]any) - populate(objectMap, "id", t.ID) - populate(objectMap, "location", t.Location) - populate(objectMap, "systemData", t.SystemData) - populate(objectMap, "tags", t.Tags) - populate(objectMap, "type", t.Type) - return json.Marshal(objectMap) -} - -// UnmarshalJSON implements the json.Unmarshaller interface for type TrackedResourceBase. -func (t *TrackedResourceBase) UnmarshalJSON(data []byte) error { - var rawMsg map[string]json.RawMessage - if err := json.Unmarshal(data, &rawMsg); err != nil { - return fmt.Errorf("unmarshalling type %T: %v", t, err) - } - for key, val := range rawMsg { - var err error - switch key { - case "id": - err = unpopulate(val, "ID", &t.ID) - delete(rawMsg, key) - case "location": - err = unpopulate(val, "Location", &t.Location) - delete(rawMsg, key) - case "systemData": - err = unpopulate(val, "SystemData", &t.SystemData) - delete(rawMsg, key) - case "tags": - err = unpopulate(val, "Tags", &t.Tags) - delete(rawMsg, key) - case "type": - err = unpopulate(val, "Type", &t.Type) - delete(rawMsg, key) - } - if err != nil { - return fmt.Errorf("unmarshalling type %T: %v", t, err) - } - } - return nil -} - // MarshalJSON implements the json.Marshaller interface for type UserAssignedIdentity. func (u UserAssignedIdentity) MarshalJSON() ([]byte, error) { objectMap := make(map[string]any) diff --git a/packages/typespec-go/test/armlargeinstance/zz_models.go b/packages/typespec-go/test/armlargeinstance/zz_models.go index 8ff9b7512..2d916f673 100644 --- a/packages/typespec-go/test/armlargeinstance/zz_models.go +++ b/packages/typespec-go/test/armlargeinstance/zz_models.go @@ -6,22 +6,6 @@ package armlargeinstance import "time" -// ArmResource - Common properties for all Azure Resource Manager resources. -type ArmResource struct { - // REQUIRED; Fully qualified resource ID for the resource. Ex - /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{resourceProviderNamespace}/{resourceType}/{resourceName} - ID *string - - // REQUIRED; The type of the resource. E.g. "Microsoft.Compute/virtualMachines" or "Microsoft.Storage/storageAccounts" - Type *string - - // Azure Resource Manager metadata containing createdBy and modifiedBy information. - SystemData *SystemData -} - -// ArmResourceBase - Base class used for type definitions -type ArmResourceBase struct { -} - // AzureLargeInstance - Azure Large Instance info on Azure (ARM properties and AzureLargeInstance // properties) type AzureLargeInstance struct { @@ -315,21 +299,3 @@ type TagsUpdate struct { // Resource tags. Tags map[string]*string } - -// TrackedResourceBase - The resource model definition for an Azure Resource Manager tracked top level resource -type TrackedResourceBase struct { - // REQUIRED; Fully qualified resource ID for the resource. Ex - /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{resourceProviderNamespace}/{resourceType}/{resourceName} - ID *string - - // REQUIRED; The geo-location where the resource lives - Location *string - - // REQUIRED; The type of the resource. E.g. "Microsoft.Compute/virtualMachines" or "Microsoft.Storage/storageAccounts" - Type *string - - // Azure Resource Manager metadata containing createdBy and modifiedBy information. - SystemData *SystemData - - // Resource tags. - Tags map[string]*string -} diff --git a/packages/typespec-go/test/armlargeinstance/zz_models_serde.go b/packages/typespec-go/test/armlargeinstance/zz_models_serde.go index 726c1594f..768173832 100644 --- a/packages/typespec-go/test/armlargeinstance/zz_models_serde.go +++ b/packages/typespec-go/test/armlargeinstance/zz_models_serde.go @@ -11,41 +11,6 @@ import ( "reflect" ) -// MarshalJSON implements the json.Marshaller interface for type ArmResource. -func (a ArmResource) MarshalJSON() ([]byte, error) { - objectMap := make(map[string]any) - populate(objectMap, "id", a.ID) - populate(objectMap, "systemData", a.SystemData) - populate(objectMap, "type", a.Type) - return json.Marshal(objectMap) -} - -// UnmarshalJSON implements the json.Unmarshaller interface for type ArmResource. -func (a *ArmResource) UnmarshalJSON(data []byte) error { - var rawMsg map[string]json.RawMessage - if err := json.Unmarshal(data, &rawMsg); err != nil { - return fmt.Errorf("unmarshalling type %T: %v", a, err) - } - for key, val := range rawMsg { - var err error - switch key { - case "id": - err = unpopulate(val, "ID", &a.ID) - delete(rawMsg, key) - case "systemData": - err = unpopulate(val, "SystemData", &a.SystemData) - delete(rawMsg, key) - case "type": - err = unpopulate(val, "Type", &a.Type) - delete(rawMsg, key) - } - if err != nil { - return fmt.Errorf("unmarshalling type %T: %v", a, err) - } - } - return nil -} - // MarshalJSON implements the json.Marshaller interface for type AzureLargeInstance. func (a AzureLargeInstance) MarshalJSON() ([]byte, error) { objectMap := make(map[string]any) @@ -821,49 +786,6 @@ func (t *TagsUpdate) UnmarshalJSON(data []byte) error { return nil } -// MarshalJSON implements the json.Marshaller interface for type TrackedResourceBase. -func (t TrackedResourceBase) MarshalJSON() ([]byte, error) { - objectMap := make(map[string]any) - populate(objectMap, "id", t.ID) - populate(objectMap, "location", t.Location) - populate(objectMap, "systemData", t.SystemData) - populate(objectMap, "tags", t.Tags) - populate(objectMap, "type", t.Type) - return json.Marshal(objectMap) -} - -// UnmarshalJSON implements the json.Unmarshaller interface for type TrackedResourceBase. -func (t *TrackedResourceBase) UnmarshalJSON(data []byte) error { - var rawMsg map[string]json.RawMessage - if err := json.Unmarshal(data, &rawMsg); err != nil { - return fmt.Errorf("unmarshalling type %T: %v", t, err) - } - for key, val := range rawMsg { - var err error - switch key { - case "id": - err = unpopulate(val, "ID", &t.ID) - delete(rawMsg, key) - case "location": - err = unpopulate(val, "Location", &t.Location) - delete(rawMsg, key) - case "systemData": - err = unpopulate(val, "SystemData", &t.SystemData) - delete(rawMsg, key) - case "tags": - err = unpopulate(val, "Tags", &t.Tags) - delete(rawMsg, key) - case "type": - err = unpopulate(val, "Type", &t.Type) - delete(rawMsg, key) - } - if err != nil { - return fmt.Errorf("unmarshalling type %T: %v", t, err) - } - } - return nil -} - func populate(m map[string]any, k string, v any) { if v == nil { return