From 42bf0696d6aea71ea087eed17233b3f6621ad7f9 Mon Sep 17 00:00:00 2001 From: Ryan McGuire Date: Tue, 26 Nov 2024 11:46:15 -0500 Subject: [PATCH] Implement func mapper for parser reflection --- Makefile | 9 +- api/eiaapi_funcmap.gen.go | 284 +++++++++++++++++++ api/funcmapper/funcmapper.go | 71 +++++ cmd/eia-client/internal/util/util_reflect.go | 66 ++++- main.go | 1 + 5 files changed, 416 insertions(+), 15 deletions(-) create mode 100644 api/eiaapi_funcmap.gen.go create mode 100644 api/funcmapper/funcmapper.go diff --git a/Makefile b/Makefile index cb7bd7e..ee38a75 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,7 @@ CLIENT_PKG := ./cmd/eia-client CLIENT_GEN_FILE := ./api/eiaapi.gen.go GO_FORMATTER := gofumpt +IS_GNU_SED := $(shell sed --version >/dev/null 2>&1 && echo true || echo false) .PHONY: all generate build install clean @@ -11,9 +12,15 @@ all: generate build # Generate code generate: go generate ./... - # Fix errors in generated code +ifeq ($(IS_GNU_SED), true) + # Fix errors in generated code with GNU sed + sed -E -i 's/Total[[:space:]]+\*int/Total *string/g' $(CLIENT_GEN_FILE) + sed -E -i 's/Command[[:space:]]+\*\[\]string/Command *string/g' $(CLIENT_GEN_FILE) +else + # Fix errors in generated code with BSD sed sed -E -i '' 's/Total[[:space:]]+\*int/Total *string/g' $(CLIENT_GEN_FILE) sed -E -i '' 's/Command[[:space:]]+\*\[\]string/Command *string/g' $(CLIENT_GEN_FILE) +endif # Pretty it up $(GO_FORMATTER) -w $(CLIENT_GEN_FILE) diff --git a/api/eiaapi_funcmap.gen.go b/api/eiaapi_funcmap.gen.go new file mode 100644 index 0000000..9f59329 --- /dev/null +++ b/api/eiaapi_funcmap.gen.go @@ -0,0 +1,284 @@ +package eiaapi + +// Generated map of Parse functions to support +// dynamic reflection upon parser functions +var ParseFunctionsMap = map[string]interface{}{ + "ParseGetV2Response": ParseGetV2Response, + "ParseGetV2AeoResponse": ParseGetV2AeoResponse, + "ParseGetV2AeoRoute1Response": ParseGetV2AeoRoute1Response, + "ParseGetV2AeoRoute1DataResponse": ParseGetV2AeoRoute1DataResponse, + "ParsePostV2AeoRoute1DataResponse": ParsePostV2AeoRoute1DataResponse, + "ParseGetV2AeoRoute1FacetResponse": ParseGetV2AeoRoute1FacetResponse, + "ParseGetV2AeoRoute1FacetFacetIdResponse": ParseGetV2AeoRoute1FacetFacetIdResponse, + "ParseGetV2Co2EmissionsResponse": ParseGetV2Co2EmissionsResponse, + "ParseGetV2Co2EmissionsCo2EmissionsAggregatesResponse": ParseGetV2Co2EmissionsCo2EmissionsAggregatesResponse, + "ParseGetV2Co2EmissionsCo2EmissionsAggregatesDataResponse": ParseGetV2Co2EmissionsCo2EmissionsAggregatesDataResponse, + "ParsePostV2Co2EmissionsCo2EmissionsAggregatesDataResponse": ParsePostV2Co2EmissionsCo2EmissionsAggregatesDataResponse, + "ParseGetV2Co2EmissionsCo2EmissionsAggregatesFacetResponse": ParseGetV2Co2EmissionsCo2EmissionsAggregatesFacetResponse, + "ParseGetV2Co2EmissionsCo2EmissionsAggregatesFacetFacetIdResponse": ParseGetV2Co2EmissionsCo2EmissionsAggregatesFacetFacetIdResponse, + "ParseGetV2Co2EmissionsCo2EmissionsAndCarbonCoefficientsResponse": ParseGetV2Co2EmissionsCo2EmissionsAndCarbonCoefficientsResponse, + "ParseGetV2Co2EmissionsCo2EmissionsAndCarbonCoefficientsDataResponse": ParseGetV2Co2EmissionsCo2EmissionsAndCarbonCoefficientsDataResponse, + "ParsePostV2Co2EmissionsCo2EmissionsAndCarbonCoefficientsDataResponse": ParsePostV2Co2EmissionsCo2EmissionsAndCarbonCoefficientsDataResponse, + "ParseGetV2Co2EmissionsCo2EmissionsAndCarbonCoefficientsFacetResponse": ParseGetV2Co2EmissionsCo2EmissionsAndCarbonCoefficientsFacetResponse, + "ParseGetV2Co2EmissionsCo2EmissionsAndCarbonCoefficientsFacetFacetIdResponse": ParseGetV2Co2EmissionsCo2EmissionsAndCarbonCoefficientsFacetFacetIdResponse, + "ParseGetV2CoalResponse": ParseGetV2CoalResponse, + "ParseGetV2CoalAggregateProductionResponse": ParseGetV2CoalAggregateProductionResponse, + "ParseGetV2CoalAggregateProductionDataResponse": ParseGetV2CoalAggregateProductionDataResponse, + "ParsePostV2CoalAggregateProductionDataResponse": ParsePostV2CoalAggregateProductionDataResponse, + "ParseGetV2CoalAggregateProductionFacetResponse": ParseGetV2CoalAggregateProductionFacetResponse, + "ParseGetV2CoalAggregateProductionFacetFacetIdResponse": ParseGetV2CoalAggregateProductionFacetFacetIdResponse, + "ParseGetV2CoalConsumptionAndQualityResponse": ParseGetV2CoalConsumptionAndQualityResponse, + "ParseGetV2CoalConsumptionAndQualityDataResponse": ParseGetV2CoalConsumptionAndQualityDataResponse, + "ParsePostV2CoalConsumptionAndQualityDataResponse": ParsePostV2CoalConsumptionAndQualityDataResponse, + "ParseGetV2CoalConsumptionAndQualityFacetResponse": ParseGetV2CoalConsumptionAndQualityFacetResponse, + "ParseGetV2CoalConsumptionAndQualityFacetFacetIdResponse": ParseGetV2CoalConsumptionAndQualityFacetFacetIdResponse, + "ParseGetV2CoalExportsImportsQuantityPriceResponse": ParseGetV2CoalExportsImportsQuantityPriceResponse, + "ParseGetV2CoalExportsImportsQuantityPriceDataResponse": ParseGetV2CoalExportsImportsQuantityPriceDataResponse, + "ParsePostV2CoalExportsImportsQuantityPriceDataResponse": ParsePostV2CoalExportsImportsQuantityPriceDataResponse, + "ParseGetV2CoalExportsImportsQuantityPriceFacetResponse": ParseGetV2CoalExportsImportsQuantityPriceFacetResponse, + "ParseGetV2CoalExportsImportsQuantityPriceFacetFacetIdResponse": ParseGetV2CoalExportsImportsQuantityPriceFacetFacetIdResponse, + "ParseGetV2CoalMarketSalesPriceResponse": ParseGetV2CoalMarketSalesPriceResponse, + "ParseGetV2CoalMarketSalesPriceDataResponse": ParseGetV2CoalMarketSalesPriceDataResponse, + "ParsePostV2CoalMarketSalesPriceDataResponse": ParsePostV2CoalMarketSalesPriceDataResponse, + "ParseGetV2CoalMarketSalesPriceFacetResponse": ParseGetV2CoalMarketSalesPriceFacetResponse, + "ParseGetV2CoalMarketSalesPriceFacetFacetIdResponse": ParseGetV2CoalMarketSalesPriceFacetFacetIdResponse, + "ParseGetV2CoalMineProductionResponse": ParseGetV2CoalMineProductionResponse, + "ParseGetV2CoalMineProductionDataResponse": ParseGetV2CoalMineProductionDataResponse, + "ParsePostV2CoalMineProductionDataResponse": ParsePostV2CoalMineProductionDataResponse, + "ParseGetV2CoalMineProductionFacetResponse": ParseGetV2CoalMineProductionFacetResponse, + "ParseGetV2CoalMineProductionFacetFacetIdResponse": ParseGetV2CoalMineProductionFacetFacetIdResponse, + "ParseGetV2CoalPriceByRankResponse": ParseGetV2CoalPriceByRankResponse, + "ParseGetV2CoalPriceByRankDataResponse": ParseGetV2CoalPriceByRankDataResponse, + "ParsePostV2CoalPriceByRankDataResponse": ParsePostV2CoalPriceByRankDataResponse, + "ParseGetV2CoalPriceByRankFacetResponse": ParseGetV2CoalPriceByRankFacetResponse, + "ParseGetV2CoalPriceByRankFacetFacetIdResponse": ParseGetV2CoalPriceByRankFacetFacetIdResponse, + "ParseGetV2CoalReservesCapacityResponse": ParseGetV2CoalReservesCapacityResponse, + "ParseGetV2CoalReservesCapacityDataResponse": ParseGetV2CoalReservesCapacityDataResponse, + "ParsePostV2CoalReservesCapacityDataResponse": ParsePostV2CoalReservesCapacityDataResponse, + "ParseGetV2CoalReservesCapacityFacetResponse": ParseGetV2CoalReservesCapacityFacetResponse, + "ParseGetV2CoalReservesCapacityFacetFacetIdResponse": ParseGetV2CoalReservesCapacityFacetFacetIdResponse, + "ParseGetV2CoalShipmentsResponse": ParseGetV2CoalShipmentsResponse, + "ParseGetV2CoalShipmentsByMineByPlantResponse": ParseGetV2CoalShipmentsByMineByPlantResponse, + "ParseGetV2CoalShipmentsByMineByPlantDataResponse": ParseGetV2CoalShipmentsByMineByPlantDataResponse, + "ParsePostV2CoalShipmentsByMineByPlantDataResponse": ParsePostV2CoalShipmentsByMineByPlantDataResponse, + "ParseGetV2CoalShipmentsByMineByPlantFacetResponse": ParseGetV2CoalShipmentsByMineByPlantFacetResponse, + "ParseGetV2CoalShipmentsByMineByPlantFacetFacetIdResponse": ParseGetV2CoalShipmentsByMineByPlantFacetFacetIdResponse, + "ParseGetV2CoalShipmentsMineAggregatesResponse": ParseGetV2CoalShipmentsMineAggregatesResponse, + "ParseGetV2CoalShipmentsMineAggregatesDataResponse": ParseGetV2CoalShipmentsMineAggregatesDataResponse, + "ParsePostV2CoalShipmentsMineAggregatesDataResponse": ParsePostV2CoalShipmentsMineAggregatesDataResponse, + "ParseGetV2CoalShipmentsMineAggregatesFacetResponse": ParseGetV2CoalShipmentsMineAggregatesFacetResponse, + "ParseGetV2CoalShipmentsMineAggregatesFacetFacetIdResponse": ParseGetV2CoalShipmentsMineAggregatesFacetFacetIdResponse, + "ParseGetV2CoalShipmentsMineStateAggregatesResponse": ParseGetV2CoalShipmentsMineStateAggregatesResponse, + "ParseGetV2CoalShipmentsMineStateAggregatesDataResponse": ParseGetV2CoalShipmentsMineStateAggregatesDataResponse, + "ParsePostV2CoalShipmentsMineStateAggregatesDataResponse": ParsePostV2CoalShipmentsMineStateAggregatesDataResponse, + "ParseGetV2CoalShipmentsMineStateAggregatesFacetResponse": ParseGetV2CoalShipmentsMineStateAggregatesFacetResponse, + "ParseGetV2CoalShipmentsMineStateAggregatesFacetFacetIdResponse": ParseGetV2CoalShipmentsMineStateAggregatesFacetFacetIdResponse, + "ParseGetV2CoalShipmentsPlantAggregatesResponse": ParseGetV2CoalShipmentsPlantAggregatesResponse, + "ParseGetV2CoalShipmentsPlantAggregatesDataResponse": ParseGetV2CoalShipmentsPlantAggregatesDataResponse, + "ParsePostV2CoalShipmentsPlantAggregatesDataResponse": ParsePostV2CoalShipmentsPlantAggregatesDataResponse, + "ParseGetV2CoalShipmentsPlantAggregatesFacetResponse": ParseGetV2CoalShipmentsPlantAggregatesFacetResponse, + "ParseGetV2CoalShipmentsPlantAggregatesFacetFacetIdResponse": ParseGetV2CoalShipmentsPlantAggregatesFacetFacetIdResponse, + "ParseGetV2CoalShipmentsPlantStateAggregatesResponse": ParseGetV2CoalShipmentsPlantStateAggregatesResponse, + "ParseGetV2CoalShipmentsPlantStateAggregatesDataResponse": ParseGetV2CoalShipmentsPlantStateAggregatesDataResponse, + "ParsePostV2CoalShipmentsPlantStateAggregatesDataResponse": ParsePostV2CoalShipmentsPlantStateAggregatesDataResponse, + "ParseGetV2CoalShipmentsPlantStateAggregatesFacetResponse": ParseGetV2CoalShipmentsPlantStateAggregatesFacetResponse, + "ParseGetV2CoalShipmentsPlantStateAggregatesFacetFacetIdResponse": ParseGetV2CoalShipmentsPlantStateAggregatesFacetFacetIdResponse, + "ParseGetV2CoalShipmentsReceiptsResponse": ParseGetV2CoalShipmentsReceiptsResponse, + "ParseGetV2CoalShipmentsReceiptsDataResponse": ParseGetV2CoalShipmentsReceiptsDataResponse, + "ParsePostV2CoalShipmentsReceiptsDataResponse": ParsePostV2CoalShipmentsReceiptsDataResponse, + "ParseGetV2CoalShipmentsReceiptsFacetResponse": ParseGetV2CoalShipmentsReceiptsFacetResponse, + "ParseGetV2CoalShipmentsReceiptsFacetFacetIdResponse": ParseGetV2CoalShipmentsReceiptsFacetFacetIdResponse, + "ParseGetV2CrudeOilImportsResponse": ParseGetV2CrudeOilImportsResponse, + "ParseGetV2CrudeOilImportsDataResponse": ParseGetV2CrudeOilImportsDataResponse, + "ParsePostV2CrudeOilImportsDataResponse": ParsePostV2CrudeOilImportsDataResponse, + "ParseGetV2CrudeOilImportsFacetResponse": ParseGetV2CrudeOilImportsFacetResponse, + "ParseGetV2CrudeOilImportsFacetFacetIdResponse": ParseGetV2CrudeOilImportsFacetFacetIdResponse, + "ParseGetV2DensifiedBiomassResponse": ParseGetV2DensifiedBiomassResponse, + "ParseGetV2DensifiedBiomassCapacityByRegionResponse": ParseGetV2DensifiedBiomassCapacityByRegionResponse, + "ParseGetV2DensifiedBiomassCapacityByRegionDataResponse": ParseGetV2DensifiedBiomassCapacityByRegionDataResponse, + "ParsePostV2DensifiedBiomassCapacityByRegionDataResponse": ParsePostV2DensifiedBiomassCapacityByRegionDataResponse, + "ParseGetV2DensifiedBiomassCapacityByRegionFacetResponse": ParseGetV2DensifiedBiomassCapacityByRegionFacetResponse, + "ParseGetV2DensifiedBiomassCapacityByRegionFacetFacetIdResponse": ParseGetV2DensifiedBiomassCapacityByRegionFacetFacetIdResponse, + "ParseGetV2DensifiedBiomassCharacteristicsByRegionResponse": ParseGetV2DensifiedBiomassCharacteristicsByRegionResponse, + "ParseGetV2DensifiedBiomassCharacteristicsByRegionDataResponse": ParseGetV2DensifiedBiomassCharacteristicsByRegionDataResponse, + "ParsePostV2DensifiedBiomassCharacteristicsByRegionDataResponse": ParsePostV2DensifiedBiomassCharacteristicsByRegionDataResponse, + "ParseGetV2DensifiedBiomassCharacteristicsByRegionFacetResponse": ParseGetV2DensifiedBiomassCharacteristicsByRegionFacetResponse, + "ParseGetV2DensifiedBiomassCharacteristicsByRegionFacetFacetIdResponse": ParseGetV2DensifiedBiomassCharacteristicsByRegionFacetFacetIdResponse, + "ParseGetV2DensifiedBiomassExportSalesAndPriceResponse": ParseGetV2DensifiedBiomassExportSalesAndPriceResponse, + "ParseGetV2DensifiedBiomassExportSalesAndPriceDataResponse": ParseGetV2DensifiedBiomassExportSalesAndPriceDataResponse, + "ParsePostV2DensifiedBiomassExportSalesAndPriceDataResponse": ParsePostV2DensifiedBiomassExportSalesAndPriceDataResponse, + "ParseGetV2DensifiedBiomassExportSalesAndPriceFacetResponse": ParseGetV2DensifiedBiomassExportSalesAndPriceFacetResponse, + "ParseGetV2DensifiedBiomassExportSalesAndPriceFacetFacetIdResponse": ParseGetV2DensifiedBiomassExportSalesAndPriceFacetFacetIdResponse, + "ParseGetV2DensifiedBiomassFeedstocksAndCostResponse": ParseGetV2DensifiedBiomassFeedstocksAndCostResponse, + "ParseGetV2DensifiedBiomassFeedstocksAndCostDataResponse": ParseGetV2DensifiedBiomassFeedstocksAndCostDataResponse, + "ParsePostV2DensifiedBiomassFeedstocksAndCostDataResponse": ParsePostV2DensifiedBiomassFeedstocksAndCostDataResponse, + "ParseGetV2DensifiedBiomassFeedstocksAndCostFacetResponse": ParseGetV2DensifiedBiomassFeedstocksAndCostFacetResponse, + "ParseGetV2DensifiedBiomassFeedstocksAndCostFacetFacetIdResponse": ParseGetV2DensifiedBiomassFeedstocksAndCostFacetFacetIdResponse, + "ParseGetV2DensifiedBiomassInventoriesByRegionResponse": ParseGetV2DensifiedBiomassInventoriesByRegionResponse, + "ParseGetV2DensifiedBiomassInventoriesByRegionDataResponse": ParseGetV2DensifiedBiomassInventoriesByRegionDataResponse, + "ParsePostV2DensifiedBiomassInventoriesByRegionDataResponse": ParsePostV2DensifiedBiomassInventoriesByRegionDataResponse, + "ParseGetV2DensifiedBiomassInventoriesByRegionFacetResponse": ParseGetV2DensifiedBiomassInventoriesByRegionFacetResponse, + "ParseGetV2DensifiedBiomassInventoriesByRegionFacetFacetIdResponse": ParseGetV2DensifiedBiomassInventoriesByRegionFacetFacetIdResponse, + "ParseGetV2DensifiedBiomassProductionByRegionResponse": ParseGetV2DensifiedBiomassProductionByRegionResponse, + "ParseGetV2DensifiedBiomassProductionByRegionDataResponse": ParseGetV2DensifiedBiomassProductionByRegionDataResponse, + "ParsePostV2DensifiedBiomassProductionByRegionDataResponse": ParsePostV2DensifiedBiomassProductionByRegionDataResponse, + "ParseGetV2DensifiedBiomassProductionByRegionFacetResponse": ParseGetV2DensifiedBiomassProductionByRegionFacetResponse, + "ParseGetV2DensifiedBiomassProductionByRegionFacetFacetIdResponse": ParseGetV2DensifiedBiomassProductionByRegionFacetFacetIdResponse, + "ParseGetV2DensifiedBiomassSalesAndPriceByRegionResponse": ParseGetV2DensifiedBiomassSalesAndPriceByRegionResponse, + "ParseGetV2DensifiedBiomassSalesAndPriceByRegionDataResponse": ParseGetV2DensifiedBiomassSalesAndPriceByRegionDataResponse, + "ParsePostV2DensifiedBiomassSalesAndPriceByRegionDataResponse": ParsePostV2DensifiedBiomassSalesAndPriceByRegionDataResponse, + "ParseGetV2DensifiedBiomassSalesAndPriceByRegionFacetResponse": ParseGetV2DensifiedBiomassSalesAndPriceByRegionFacetResponse, + "ParseGetV2DensifiedBiomassSalesAndPriceByRegionFacetFacetIdResponse": ParseGetV2DensifiedBiomassSalesAndPriceByRegionFacetFacetIdResponse, + "ParseGetV2DensifiedBiomassWoodPelletPlantsResponse": ParseGetV2DensifiedBiomassWoodPelletPlantsResponse, + "ParseGetV2DensifiedBiomassWoodPelletPlantsDataResponse": ParseGetV2DensifiedBiomassWoodPelletPlantsDataResponse, + "ParsePostV2DensifiedBiomassWoodPelletPlantsDataResponse": ParsePostV2DensifiedBiomassWoodPelletPlantsDataResponse, + "ParseGetV2DensifiedBiomassWoodPelletPlantsFacetResponse": ParseGetV2DensifiedBiomassWoodPelletPlantsFacetResponse, + "ParseGetV2DensifiedBiomassWoodPelletPlantsFacetFacetIdResponse": ParseGetV2DensifiedBiomassWoodPelletPlantsFacetFacetIdResponse, + "ParseGetV2ElectricityResponse": ParseGetV2ElectricityResponse, + "ParseGetV2ElectricityElectricPowerOperationalDataResponse": ParseGetV2ElectricityElectricPowerOperationalDataResponse, + "ParseGetV2ElectricityElectricPowerOperationalDataDataResponse": ParseGetV2ElectricityElectricPowerOperationalDataDataResponse, + "ParsePostV2ElectricityElectricPowerOperationalDataDataResponse": ParsePostV2ElectricityElectricPowerOperationalDataDataResponse, + "ParseGetV2ElectricityElectricPowerOperationalDataFacetResponse": ParseGetV2ElectricityElectricPowerOperationalDataFacetResponse, + "ParseGetV2ElectricityElectricPowerOperationalDataFacetFacetIdResponse": ParseGetV2ElectricityElectricPowerOperationalDataFacetFacetIdResponse, + "ParseGetV2ElectricityFacilityFuelResponse": ParseGetV2ElectricityFacilityFuelResponse, + "ParseGetV2ElectricityFacilityFuelDataResponse": ParseGetV2ElectricityFacilityFuelDataResponse, + "ParsePostV2ElectricityFacilityFuelDataResponse": ParsePostV2ElectricityFacilityFuelDataResponse, + "ParseGetV2ElectricityFacilityFuelFacetResponse": ParseGetV2ElectricityFacilityFuelFacetResponse, + "ParseGetV2ElectricityFacilityFuelFacetFacetIdResponse": ParseGetV2ElectricityFacilityFuelFacetFacetIdResponse, + "ParseGetV2ElectricityOperatingGeneratorCapacityResponse": ParseGetV2ElectricityOperatingGeneratorCapacityResponse, + "ParseGetV2ElectricityOperatingGeneratorCapacityDataResponse": ParseGetV2ElectricityOperatingGeneratorCapacityDataResponse, + "ParsePostV2ElectricityOperatingGeneratorCapacityDataResponse": ParsePostV2ElectricityOperatingGeneratorCapacityDataResponse, + "ParseGetV2ElectricityOperatingGeneratorCapacityFacetResponse": ParseGetV2ElectricityOperatingGeneratorCapacityFacetResponse, + "ParseGetV2ElectricityOperatingGeneratorCapacityFacetFacetIdResponse": ParseGetV2ElectricityOperatingGeneratorCapacityFacetFacetIdResponse, + "ParseGetV2ElectricityRetailSalesResponse": ParseGetV2ElectricityRetailSalesResponse, + "ParseGetV2ElectricityRetailSalesDataResponse": ParseGetV2ElectricityRetailSalesDataResponse, + "ParsePostV2ElectricityRetailSalesDataResponse": ParsePostV2ElectricityRetailSalesDataResponse, + "ParseGetV2ElectricityRetailSalesFacetResponse": ParseGetV2ElectricityRetailSalesFacetResponse, + "ParseGetV2ElectricityRetailSalesFacetFacetIdResponse": ParseGetV2ElectricityRetailSalesFacetFacetIdResponse, + "ParseGetV2ElectricityRtoResponse": ParseGetV2ElectricityRtoResponse, + "ParseGetV2ElectricityRtoDailyFuelTypeDataResponse": ParseGetV2ElectricityRtoDailyFuelTypeDataResponse, + "ParseGetV2ElectricityRtoDailyFuelTypeDataDataResponse": ParseGetV2ElectricityRtoDailyFuelTypeDataDataResponse, + "ParsePostV2ElectricityRtoDailyFuelTypeDataDataResponse": ParsePostV2ElectricityRtoDailyFuelTypeDataDataResponse, + "ParseGetV2ElectricityRtoDailyFuelTypeDataFacetResponse": ParseGetV2ElectricityRtoDailyFuelTypeDataFacetResponse, + "ParseGetV2ElectricityRtoDailyFuelTypeDataFacetFacetIdResponse": ParseGetV2ElectricityRtoDailyFuelTypeDataFacetFacetIdResponse, + "ParseGetV2ElectricityRtoDailyInterchangeDataResponse": ParseGetV2ElectricityRtoDailyInterchangeDataResponse, + "ParseGetV2ElectricityRtoDailyInterchangeDataDataResponse": ParseGetV2ElectricityRtoDailyInterchangeDataDataResponse, + "ParsePostV2ElectricityRtoDailyInterchangeDataDataResponse": ParsePostV2ElectricityRtoDailyInterchangeDataDataResponse, + "ParseGetV2ElectricityRtoDailyInterchangeDataFacetResponse": ParseGetV2ElectricityRtoDailyInterchangeDataFacetResponse, + "ParseGetV2ElectricityRtoDailyInterchangeDataFacetFacetIdResponse": ParseGetV2ElectricityRtoDailyInterchangeDataFacetFacetIdResponse, + "ParseGetV2ElectricityRtoDailyRegionDataResponse": ParseGetV2ElectricityRtoDailyRegionDataResponse, + "ParseGetV2ElectricityRtoDailyRegionDataDataResponse": ParseGetV2ElectricityRtoDailyRegionDataDataResponse, + "ParsePostV2ElectricityRtoDailyRegionDataDataResponse": ParsePostV2ElectricityRtoDailyRegionDataDataResponse, + "ParseGetV2ElectricityRtoDailyRegionDataFacetResponse": ParseGetV2ElectricityRtoDailyRegionDataFacetResponse, + "ParseGetV2ElectricityRtoDailyRegionDataFacetFacetIdResponse": ParseGetV2ElectricityRtoDailyRegionDataFacetFacetIdResponse, + "ParseGetV2ElectricityRtoDailyRegionSubBaDataResponse": ParseGetV2ElectricityRtoDailyRegionSubBaDataResponse, + "ParseGetV2ElectricityRtoDailyRegionSubBaDataDataResponse": ParseGetV2ElectricityRtoDailyRegionSubBaDataDataResponse, + "ParsePostV2ElectricityRtoDailyRegionSubBaDataDataResponse": ParsePostV2ElectricityRtoDailyRegionSubBaDataDataResponse, + "ParseGetV2ElectricityRtoDailyRegionSubBaDataFacetResponse": ParseGetV2ElectricityRtoDailyRegionSubBaDataFacetResponse, + "ParseGetV2ElectricityRtoDailyRegionSubBaDataFacetFacetIdResponse": ParseGetV2ElectricityRtoDailyRegionSubBaDataFacetFacetIdResponse, + "ParseGetV2ElectricityRtoFuelTypeDataResponse": ParseGetV2ElectricityRtoFuelTypeDataResponse, + "ParseGetV2ElectricityRtoFuelTypeDataDataResponse": ParseGetV2ElectricityRtoFuelTypeDataDataResponse, + "ParsePostV2ElectricityRtoFuelTypeDataDataResponse": ParsePostV2ElectricityRtoFuelTypeDataDataResponse, + "ParseGetV2ElectricityRtoFuelTypeDataFacetResponse": ParseGetV2ElectricityRtoFuelTypeDataFacetResponse, + "ParseGetV2ElectricityRtoFuelTypeDataFacetFacetIdResponse": ParseGetV2ElectricityRtoFuelTypeDataFacetFacetIdResponse, + "ParseGetV2ElectricityRtoInterchangeDataResponse": ParseGetV2ElectricityRtoInterchangeDataResponse, + "ParseGetV2ElectricityRtoInterchangeDataDataResponse": ParseGetV2ElectricityRtoInterchangeDataDataResponse, + "ParsePostV2ElectricityRtoInterchangeDataDataResponse": ParsePostV2ElectricityRtoInterchangeDataDataResponse, + "ParseGetV2ElectricityRtoInterchangeDataFacetResponse": ParseGetV2ElectricityRtoInterchangeDataFacetResponse, + "ParseGetV2ElectricityRtoInterchangeDataFacetFacetIdResponse": ParseGetV2ElectricityRtoInterchangeDataFacetFacetIdResponse, + "ParseGetV2ElectricityRtoRegionDataResponse": ParseGetV2ElectricityRtoRegionDataResponse, + "ParseGetV2ElectricityRtoRegionDataDataResponse": ParseGetV2ElectricityRtoRegionDataDataResponse, + "ParsePostV2ElectricityRtoRegionDataDataResponse": ParsePostV2ElectricityRtoRegionDataDataResponse, + "ParseGetV2ElectricityRtoRegionDataFacetResponse": ParseGetV2ElectricityRtoRegionDataFacetResponse, + "ParseGetV2ElectricityRtoRegionDataFacetFacetIdResponse": ParseGetV2ElectricityRtoRegionDataFacetFacetIdResponse, + "ParseGetV2ElectricityRtoRegionSubBaDataResponse": ParseGetV2ElectricityRtoRegionSubBaDataResponse, + "ParseGetV2ElectricityRtoRegionSubBaDataDataResponse": ParseGetV2ElectricityRtoRegionSubBaDataDataResponse, + "ParsePostV2ElectricityRtoRegionSubBaDataDataResponse": ParsePostV2ElectricityRtoRegionSubBaDataDataResponse, + "ParseGetV2ElectricityRtoRegionSubBaDataFacetResponse": ParseGetV2ElectricityRtoRegionSubBaDataFacetResponse, + "ParseGetV2ElectricityRtoRegionSubBaDataFacetFacetIdResponse": ParseGetV2ElectricityRtoRegionSubBaDataFacetFacetIdResponse, + "ParseGetV2ElectricitySepResponse": ParseGetV2ElectricitySepResponse, + "ParseGetV2ElectricityStateElectricityProfilesCapabilityResponse": ParseGetV2ElectricityStateElectricityProfilesCapabilityResponse, + "ParseGetV2ElectricityStateElectricityProfilesCapabilityDataResponse": ParseGetV2ElectricityStateElectricityProfilesCapabilityDataResponse, + "ParsePostV2ElectricityStateElectricityProfilesCapabilityDataResponse": ParsePostV2ElectricityStateElectricityProfilesCapabilityDataResponse, + "ParseGetV2ElectricityStateElectricityProfilesCapabilityFacetResponse": ParseGetV2ElectricityStateElectricityProfilesCapabilityFacetResponse, + "ParseGetV2ElectricityStateElectricityProfilesCapabilityFacetFacetIdResponse": ParseGetV2ElectricityStateElectricityProfilesCapabilityFacetFacetIdResponse, + "ParseGetV2ElectricityStateElectricityProfilesEmissionsByStateByFuelResponse": ParseGetV2ElectricityStateElectricityProfilesEmissionsByStateByFuelResponse, + "ParseGetV2ElectricityStateElectricityProfilesEmissionsByStateByFuelDataResponse": ParseGetV2ElectricityStateElectricityProfilesEmissionsByStateByFuelDataResponse, + "ParsePostV2ElectricityStateElectricityProfilesEmissionsByStateByFuelDataResponse": ParsePostV2ElectricityStateElectricityProfilesEmissionsByStateByFuelDataResponse, + "ParseGetV2ElectricityStateElectricityProfilesEmissionsByStateByFuelFacetResponse": ParseGetV2ElectricityStateElectricityProfilesEmissionsByStateByFuelFacetResponse, + "ParseGetV2ElectricityStateElectricityProfilesEmissionsByStateByFuelFacetFacetIdResponse": ParseGetV2ElectricityStateElectricityProfilesEmissionsByStateByFuelFacetFacetIdResponse, + "ParseGetV2ElectricityStateElectricityProfilesEnergyEfficiencyResponse": ParseGetV2ElectricityStateElectricityProfilesEnergyEfficiencyResponse, + "ParseGetV2ElectricityStateElectricityProfilesEnergyEfficiencyDataResponse": ParseGetV2ElectricityStateElectricityProfilesEnergyEfficiencyDataResponse, + "ParsePostV2ElectricityStateElectricityProfilesEnergyEfficiencyDataResponse": ParsePostV2ElectricityStateElectricityProfilesEnergyEfficiencyDataResponse, + "ParseGetV2ElectricityStateElectricityProfilesEnergyEfficiencyFacetResponse": ParseGetV2ElectricityStateElectricityProfilesEnergyEfficiencyFacetResponse, + "ParseGetV2ElectricityStateElectricityProfilesEnergyEfficiencyFacetFacetIdResponse": ParseGetV2ElectricityStateElectricityProfilesEnergyEfficiencyFacetFacetIdResponse, + "ParseGetV2ElectricityStateElectricityProfilesMetersResponse": ParseGetV2ElectricityStateElectricityProfilesMetersResponse, + "ParseGetV2ElectricityStateElectricityProfilesMetersDataResponse": ParseGetV2ElectricityStateElectricityProfilesMetersDataResponse, + "ParsePostV2ElectricityStateElectricityProfilesMetersDataResponse": ParsePostV2ElectricityStateElectricityProfilesMetersDataResponse, + "ParseGetV2ElectricityStateElectricityProfilesMetersFacetResponse": ParseGetV2ElectricityStateElectricityProfilesMetersFacetResponse, + "ParseGetV2ElectricityStateElectricityProfilesMetersFacetFacetIdResponse": ParseGetV2ElectricityStateElectricityProfilesMetersFacetFacetIdResponse, + "ParseGetV2ElectricityStateElectricityProfilesNetMeteringResponse": ParseGetV2ElectricityStateElectricityProfilesNetMeteringResponse, + "ParseGetV2ElectricityStateElectricityProfilesNetMeteringDataResponse": ParseGetV2ElectricityStateElectricityProfilesNetMeteringDataResponse, + "ParsePostV2ElectricityStateElectricityProfilesNetMeteringDataResponse": ParsePostV2ElectricityStateElectricityProfilesNetMeteringDataResponse, + "ParseGetV2ElectricityStateElectricityProfilesNetMeteringFacetResponse": ParseGetV2ElectricityStateElectricityProfilesNetMeteringFacetResponse, + "ParseGetV2ElectricityStateElectricityProfilesNetMeteringFacetFacetIdResponse": ParseGetV2ElectricityStateElectricityProfilesNetMeteringFacetFacetIdResponse, + "ParseGetV2ElectricityStateElectricityProfilesSourceDispositionResponse": ParseGetV2ElectricityStateElectricityProfilesSourceDispositionResponse, + "ParseGetV2ElectricityStateElectricityProfilesSourceDispositionDataResponse": ParseGetV2ElectricityStateElectricityProfilesSourceDispositionDataResponse, + "ParsePostV2ElectricityStateElectricityProfilesSourceDispositionDataResponse": ParsePostV2ElectricityStateElectricityProfilesSourceDispositionDataResponse, + "ParseGetV2ElectricityStateElectricityProfilesSourceDispositionFacetResponse": ParseGetV2ElectricityStateElectricityProfilesSourceDispositionFacetResponse, + "ParseGetV2ElectricityStateElectricityProfilesSourceDispositionFacetFacetIdResponse": ParseGetV2ElectricityStateElectricityProfilesSourceDispositionFacetFacetIdResponse, + "ParseGetV2ElectricityStateElectricityProfilesSummaryResponse": ParseGetV2ElectricityStateElectricityProfilesSummaryResponse, + "ParseGetV2ElectricityStateElectricityProfilesSummaryDataResponse": ParseGetV2ElectricityStateElectricityProfilesSummaryDataResponse, + "ParsePostV2ElectricityStateElectricityProfilesSummaryDataResponse": ParsePostV2ElectricityStateElectricityProfilesSummaryDataResponse, + "ParseGetV2ElectricityStateElectricityProfilesSummaryFacetResponse": ParseGetV2ElectricityStateElectricityProfilesSummaryFacetResponse, + "ParseGetV2ElectricityStateElectricityProfilesSummaryFacetFacetIdResponse": ParseGetV2ElectricityStateElectricityProfilesSummaryFacetFacetIdResponse, + "ParseEIAAPIControllersDatasetAeoIeoIeoControllerIeoResponse": ParseEIAAPIControllersDatasetAeoIeoIeoControllerIeoResponse, + "ParseEIAAPIControllersDatasetAeoIeoIeoControllerConstructResponse": ParseEIAAPIControllersDatasetAeoIeoIeoControllerConstructResponse, + "ParseEIAAPIControllersDatasetAeoIeoIeoControllerGetDataResponse": ParseEIAAPIControllersDatasetAeoIeoIeoControllerGetDataResponse, + "ParseEIAAPIControllersDatasetAeoIeoIeoControllerPostDataResponse": ParseEIAAPIControllersDatasetAeoIeoIeoControllerPostDataResponse, + "ParseEIAAPIControllersDatasetAeoIeoIeoControllerRouteFacetResponse": ParseEIAAPIControllersDatasetAeoIeoIeoControllerRouteFacetResponse, + "ParseEIAAPIControllersDatasetAeoIeoIeoControllerRouteFacetIdResponse": ParseEIAAPIControllersDatasetAeoIeoIeoControllerRouteFacetIdResponse, + "ParseGetV2InternationalResponse": ParseGetV2InternationalResponse, + "ParseGetV2InternationalDataResponse": ParseGetV2InternationalDataResponse, + "ParsePostV2InternationalDataResponse": ParsePostV2InternationalDataResponse, + "ParseGetV2InternationalFacetResponse": ParseGetV2InternationalFacetResponse, + "ParseGetV2InternationalFacetFacetIdResponse": ParseGetV2InternationalFacetFacetIdResponse, + "ParseGetV2NaturalGasResponse": ParseGetV2NaturalGasResponse, + "ParseGetV2NaturalGasRoute1Response": ParseGetV2NaturalGasRoute1Response, + "ParseGetV2NaturalGasRoute1Route2Response": ParseGetV2NaturalGasRoute1Route2Response, + "ParseGetV2NaturalGasRoute1Route2DataResponse": ParseGetV2NaturalGasRoute1Route2DataResponse, + "ParsePostV2NaturalGasRoute1Route2DataResponse": ParsePostV2NaturalGasRoute1Route2DataResponse, + "ParseGetV2NaturalGasRoute1Route2FacetResponse": ParseGetV2NaturalGasRoute1Route2FacetResponse, + "ParseGetV2NaturalGasRoute1Route2FacetFacetIdResponse": ParseGetV2NaturalGasRoute1Route2FacetFacetIdResponse, + "ParseGetV2NuclearOutagesResponse": ParseGetV2NuclearOutagesResponse, + "ParseGetV2NuclearOutagesFacilityNuclearOutagesResponse": ParseGetV2NuclearOutagesFacilityNuclearOutagesResponse, + "ParseGetV2NuclearOutagesFacilityNuclearOutagesDataResponse": ParseGetV2NuclearOutagesFacilityNuclearOutagesDataResponse, + "ParsePostV2NuclearOutagesFacilityNuclearOutagesDataResponse": ParsePostV2NuclearOutagesFacilityNuclearOutagesDataResponse, + "ParseGetV2NuclearOutagesFacilityNuclearOutagesFacetResponse": ParseGetV2NuclearOutagesFacilityNuclearOutagesFacetResponse, + "ParseGetV2NuclearOutagesFacilityNuclearOutagesFacetFacetIdResponse": ParseGetV2NuclearOutagesFacilityNuclearOutagesFacetFacetIdResponse, + "ParseGetV2NuclearOutagesGeneratorNuclearOutagesResponse": ParseGetV2NuclearOutagesGeneratorNuclearOutagesResponse, + "ParseGetV2NuclearOutagesGeneratorNuclearOutagesDataResponse": ParseGetV2NuclearOutagesGeneratorNuclearOutagesDataResponse, + "ParsePostV2NuclearOutagesGeneratorNuclearOutagesDataResponse": ParsePostV2NuclearOutagesGeneratorNuclearOutagesDataResponse, + "ParseGetV2NuclearOutagesGeneratorNuclearOutagesFacetResponse": ParseGetV2NuclearOutagesGeneratorNuclearOutagesFacetResponse, + "ParseGetV2NuclearOutagesGeneratorNuclearOutagesFacetFacetIdResponse": ParseGetV2NuclearOutagesGeneratorNuclearOutagesFacetFacetIdResponse, + "ParseGetV2NuclearOutagesUsNuclearOutagesResponse": ParseGetV2NuclearOutagesUsNuclearOutagesResponse, + "ParseGetV2NuclearOutagesUsNuclearOutagesDataResponse": ParseGetV2NuclearOutagesUsNuclearOutagesDataResponse, + "ParsePostV2NuclearOutagesUsNuclearOutagesDataResponse": ParsePostV2NuclearOutagesUsNuclearOutagesDataResponse, + "ParseGetV2NuclearOutagesUsNuclearOutagesFacetResponse": ParseGetV2NuclearOutagesUsNuclearOutagesFacetResponse, + "ParseGetV2NuclearOutagesUsNuclearOutagesFacetFacetIdResponse": ParseGetV2NuclearOutagesUsNuclearOutagesFacetFacetIdResponse, + "ParseGetV2SedsResponse": ParseGetV2SedsResponse, + "ParseGetV2SedsDataResponse": ParseGetV2SedsDataResponse, + "ParsePostV2SedsDataResponse": ParsePostV2SedsDataResponse, + "ParseGetV2SedsFacetResponse": ParseGetV2SedsFacetResponse, + "ParseGetV2SedsFacetFacetIdResponse": ParseGetV2SedsFacetFacetIdResponse, + "ParseGetV2SteoResponse": ParseGetV2SteoResponse, + "ParseGetV2SteoDataResponse": ParseGetV2SteoDataResponse, + "ParsePostV2SteoDataResponse": ParsePostV2SteoDataResponse, + "ParseGetV2SteoFacetResponse": ParseGetV2SteoFacetResponse, + "ParseGetV2SteoFacetFacetIdResponse": ParseGetV2SteoFacetFacetIdResponse, + "ParseGetV2TotalEnergyResponse": ParseGetV2TotalEnergyResponse, + "ParseGetV2TotalEnergyDataResponse": ParseGetV2TotalEnergyDataResponse, + "ParsePostV2TotalEnergyDataResponse": ParsePostV2TotalEnergyDataResponse, + "ParseGetV2TotalEnergyFacetResponse": ParseGetV2TotalEnergyFacetResponse, + "ParseGetV2TotalEnergyFacetFacetIdResponse": ParseGetV2TotalEnergyFacetFacetIdResponse, +} diff --git a/api/funcmapper/funcmapper.go b/api/funcmapper/funcmapper.go new file mode 100644 index 0000000..f7b7653 --- /dev/null +++ b/api/funcmapper/funcmapper.go @@ -0,0 +1,71 @@ +package main + +import ( + "fmt" + "go/ast" + "go/parser" + "go/token" + "os" + "strings" + "text/template" +) + +// The package path you want to scan for functions +const ( + packagePath = "api/" // Update this to your package path + packageName = "eiaapi" + mapperFile = "api/eiaapi_funcmap.gen.go" +) + +const tmplParseFuncs = `package ` + packageName + ` + +// Generated map of Parse functions to support +// dynamic reflection upon parser functions +var ParseFunctionsMap = map[string]interface{}{ +{{- range .}} + "{{ . }}": {{ . }}, +{{- end}} +} +` + +func main() { + // Parse the package + fset := token.NewFileSet() + node, err := parser.ParseDir(fset, packagePath, nil, parser.ParseComments) + if err != nil { + fmt.Println("Error parsing package:", err) + return + } + + // Collect function names that start with "Parse" + var functions []string + for _, pkg := range node { + for _, file := range pkg.Files { + for _, decl := range file.Decls { + if funcDecl, ok := decl.(*ast.FuncDecl); ok { + if strings.HasPrefix(funcDecl.Name.Name, "Parse") { + // Add function name to the list + functions = append(functions, funcDecl.Name.Name) + } + } + } + } + } + + // Generate Go code for the map + mapFile, err := os.Create(mapperFile) + if err != nil { + fmt.Println("Error creating file:", err) + return + } + defer mapFile.Close() + + // Execute the template and write to the file + t := template.Must(template.New("mapTemplate").Parse(tmplParseFuncs)) + if err := t.Execute(mapFile, functions); err != nil { + fmt.Println("Error generating Go code:", err) + return + } + + fmt.Printf("%s has been generated successfully!\n", mapperFile) +} diff --git a/cmd/eia-client/internal/util/util_reflect.go b/cmd/eia-client/internal/util/util_reflect.go index de0d516..ffbf31d 100644 --- a/cmd/eia-client/internal/util/util_reflect.go +++ b/cmd/eia-client/internal/util/util_reflect.go @@ -14,7 +14,7 @@ import ( eiaapi "gitea.libretechconsulting.com/50W/eia-api-go/api" ) -func GetFacets(cmd *cobra.Command, route string) (any, error) { +func GetFacets(cmd *cobra.Command, route string) (*eiaapi.FacetOptionList, error) { client, err := Client(cmd) if err != nil { return nil, err @@ -65,28 +65,66 @@ func GetFacets(cmd *cobra.Command, route string) (any, error) { var resp *http.Response var ok bool - err = nil - if resp, ok = results[0].Interface().(*http.Response); !ok { return nil, errors.New("no or invalid response received from call") } - - if results[1].IsValid() && !results[1].IsNil() { - if err, ok = results[1].Interface().(error); !ok { - return nil, errors.New("unexpected call response") - } - } - - if err != nil { + if err := checkCallErr(results[1]); err != nil { return nil, err } - parser := reflect.ValueOf(eiaapi).MethodByName(fmt.Sprintf("Parse%sResponse", route)) + parserFunc, exists := eiaapi.ParseFunctionsMap[fmt.Sprintf("Parse%sResponse", route)] + if !exists { + return nil, fmt.Errorf("parser func for %s not found", route) + } + + parser := reflect.ValueOf(parserFunc) if !parser.IsValid() { return nil, errors.New("unable to find parser for facet response") } - parser.Call([]reflect.Value{reflect.ValueOf(resp)}) + results = parser.Call([]reflect.Value{reflect.ValueOf(resp)}) + if len(results) != 2 { + return nil, errors.New("unexpected response while parsing facet response") + } + if err := checkCallErr(results[1]); err != nil { + return nil, err + } - return resp, nil + result := results[0] + if result.Kind() == reflect.Ptr { + result = result.Elem() + } + + if result.Kind() != reflect.Struct { + return nil, fmt.Errorf("unexpected parse result kind %s", result.Kind().String()) + } + + field := result.FieldByName("JSON200") + if !field.IsValid() { + return nil, errors.New("invalid facet data field in response") + } + + facetOptions, ok := field.Interface().(*eiaapi.FacetOptionListContainer) + if !ok { + return nil, errors.New("response does not contain facet options") + } + + if facetOptions == nil { + return nil, errors.New("no facet options found for facet request") + } + + return facetOptions.Response, nil +} + +func checkCallErr(val reflect.Value) error { + var err error + var ok bool + + if val.IsValid() && !val.IsNil() { + if err, ok = val.Interface().(error); !ok { + return errors.New("unexpected call response") + } + } + + return err } diff --git a/main.go b/main.go index 9756751..16f245c 100644 --- a/main.go +++ b/main.go @@ -1,3 +1,4 @@ package main //go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=api/genclient.yaml schema/eia-api-swagger.yaml +//go:generate go run api/funcmapper/funcmapper.go