Skip to content

Commit

Permalink
Move modulesToImport
Browse files Browse the repository at this point in the history
  • Loading branch information
Viir committed Aug 13, 2023
1 parent 0c77893 commit 7584a72
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ mapJsonConvertersModuleText { originalSourceModules, sourceDirs } ( sourceFiles,
|> Result.mapError (Elm.Syntax.Node.Node functionToReplace.declarationRange)
)
(addImportsInElmModuleText
(List.map (Tuple.pair >> (|>) Nothing) modulesToImport)
(modulesToImport |> Set.toList |> List.map (Tuple.pair >> (|>) Nothing))
moduleText
|> Result.mapError (Elm.Syntax.Node.Node (syntaxRangeCoveringCompleteString moduleText))
)
Expand All @@ -800,14 +800,18 @@ mapAppFilesToSupportJsonConverters :
-> List ElmTypeAnnotation
-> Dict.Dict String ElmChoiceTypeStruct
-> AppFiles
-> ( AppFiles, { generatedModuleName : List String, modulesToImport : List (List String) } )
-> ( AppFiles, { generatedModuleName : List String, modulesToImport : Set.Set (List String) } )
mapAppFilesToSupportJsonConverters { generatedModuleNamePrefix, sourceDirs } typeAnnotationsBeforeDeduplicating choiceTypes appFilesBefore =
let
generatedFunctions =
buildJsonConverterFunctionsForMultipleRoots typeAnnotationsBeforeDeduplicating choiceTypes

generatedModuleModulesToImport =
encodingModuleImportBase64 :: List.map (Tuple.pair >> (|>) Nothing) generatedFunctions.modulesToImport
encodingModuleImportBase64
:: (generatedFunctions.modulesToImport
|> Set.toList
|> List.map (Tuple.pair >> (|>) Nothing)
)

appFilesAfterExposingChoiceTypesInModules =
generatedFunctions.modulesToImportForChoiceTypes
Expand Down Expand Up @@ -850,7 +854,7 @@ mapAppFilesToSupportJsonConverters { generatedModuleNamePrefix, sourceDirs } typ
in
( appFiles
, { generatedModuleName = generatedModuleName
, modulesToImport = generatedModuleName :: generatedFunctions.modulesToImport
, modulesToImport = Set.insert generatedModuleName generatedFunctions.modulesToImport
}
)

Expand All @@ -860,11 +864,23 @@ buildJsonConverterFunctionsForMultipleRoots :
-> Dict.Dict String ElmChoiceTypeStruct
->
{ generatedFunctions : List { functionName : String, functionText : String }
, modulesToImport : List (List String)
, modulesToImport : Set.Set (List String)
, modulesToImportForChoiceTypes : List (List String)
}
buildJsonConverterFunctionsForMultipleRoots typeAnnotations choiceTypes =
let
defaultModulesToImportForFunction =
[ [ "Dict" ]
, [ "Set" ]
, [ "Array" ]
, [ "Json", "Decode" ]
, [ "Json", "Encode" ]
, [ "Bytes" ]
, [ "Bytes", "Decode" ]
, [ "Bytes", "Encode" ]
]
|> Set.fromList

generatedFunctionsForTypes =
generateFunctionsForMultipleTypes
{ generateFromTypeAnnotation =
Expand All @@ -873,7 +889,10 @@ buildJsonConverterFunctionsForMultipleRoots typeAnnotations choiceTypes =
[ functionsForType.encodeFunction, functionsForType.decodeFunction ]
|> List.map
(\function ->
{ functionName = function.name, functionText = function.text }
{ functionName = function.name
, functionText = function.text
, modulesToImport = defaultModulesToImportForFunction
}
)
)
, generateFromChoiceType =
Expand All @@ -889,24 +908,48 @@ buildJsonConverterFunctionsForMultipleRoots typeAnnotations choiceTypes =
[ functionsForType.encodeFunction, functionsForType.decodeFunction ]
|> List.map
(\function ->
{ functionName = function.name, functionText = function.text }
{ functionName = function.name
, functionText = function.text
, modulesToImport = defaultModulesToImportForFunction
}
)
)
}
typeAnnotations
choiceTypes

generatedFunctions =
generatedFunctionsForTypes.generatedFunctions ++ generalSupportingFunctionsTexts
(generatedFunctionsForTypes.generatedFunctions
|> List.map
(\function ->
{ functionName = function.functionName
, functionText = function.functionText
}
)
)
++ generalSupportingFunctionsTexts

modulesToImport =
generatedFunctionsForTypes.generatedFunctions
|> List.map .modulesToImport
|> List.foldl Set.union Set.empty
in
{ generatedFunctionsForTypes
| generatedFunctions = generatedFunctions
{ generatedFunctions = generatedFunctions
, modulesToImportForChoiceTypes = generatedFunctionsForTypes.modulesToImportForChoiceTypes
, modulesToImport = modulesToImport
}


type alias GenerateFunctionsFromTypesConfig =
{ generateFromTypeAnnotation : ElmTypeAnnotation -> List { functionName : String, functionText : String }
, generateFromChoiceType : ( String, ElmChoiceTypeStruct ) -> List { functionName : String, functionText : String }
{ generateFromTypeAnnotation : ElmTypeAnnotation -> List GenerateFunctionFromTypeResult
, generateFromChoiceType : ( String, ElmChoiceTypeStruct ) -> List GenerateFunctionFromTypeResult
}


type alias GenerateFunctionFromTypeResult =
{ functionName : String
, functionText : String
, modulesToImport : Set.Set (List String)
}


Expand All @@ -915,8 +958,7 @@ generateFunctionsForMultipleTypes :
-> List ElmTypeAnnotation
-> Dict.Dict String ElmChoiceTypeStruct
->
{ generatedFunctions : List { functionName : String, functionText : String }
, modulesToImport : List (List String)
{ generatedFunctions : List GenerateFunctionFromTypeResult
, modulesToImportForChoiceTypes : List (List String)
}
generateFunctionsForMultipleTypes config typeAnnotationsBeforeDeduplicating choiceTypes =
Expand All @@ -929,18 +971,6 @@ generateFunctionsForMultipleTypes config typeAnnotationsBeforeDeduplicating choi
|> Set.toList
|> List.map (String.split ".")

modulesToImport =
[ [ "Dict" ]
, [ "Set" ]
, [ "Array" ]
, [ "Json", "Decode" ]
, [ "Json", "Encode" ]
, [ "Bytes" ]
, [ "Bytes", "Decode" ]
, [ "Bytes", "Encode" ]
]
++ modulesToImportForChoiceTypes

generatedFunctionsFromTypeAnnotations =
typeAnnotationsBeforeDeduplicating
|> List.Extra.unique
Expand All @@ -955,7 +985,6 @@ generateFunctionsForMultipleTypes config typeAnnotationsBeforeDeduplicating choi
generatedFunctionsFromTypeAnnotations ++ generatedFunctionsFromChoiceTypes
in
{ generatedFunctions = generatedFunctions
, modulesToImport = modulesToImport
, modulesToImportForChoiceTypes = modulesToImportForChoiceTypes
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ loweredForAppInStateManagementShim sourceDirs stateShimConfig config sourceFiles
jsonConvertedTypesDependencies

modulesToImport =
generateSerializersResult.modulesToImport
Set.toList generateSerializersResult.modulesToImport
++ stateShimConfig.modulesToImport
++ List.map .moduleName (Dict.values supportingTypes.modules)

Expand Down

0 comments on commit 7584a72

Please sign in to comment.